Python Programming

Operators in Python

In Programming, Python by Baqir Ali

Operators are the building blocks of an expression or a mathematical formula. such as 3*10 here 3 and 10 are operands and the * sign is the operator. The same is the case with python while building expressions we need different operators that functions differently.

The following are the operators that python supports.

  • Basic mathematic/arithmetic operator
  • Relational operator
  • Assignment operator
  • Arithmetic assignment operator
  • Logical operator

Arithmetic Operator in Python

Operator NameHow it works
+ AdditionThe value of two operands are added
– SubtractionThe value of the two operands are subtracted
* MultiplicationThe value of the two operands are multiplied
/ DivisionIt divides the left-hand operand by the right hand
% Modules or Remainder It divides the left-hand operand by the right hand but returns the remainder
** Exponent It Performs exponential (power) calculation on operators

Example 1:

a=10
b=20
sum=a+b
print("the sum is ",sum)

Output:

the sum is 30

Example 2:

a=20
b=3
div=a/b
print("the answer is  ",div)

Output:

the answer is 6.666666666666667

Example 3:

a=20
b=3
div=a%b
print("the remainder is  ",div)

Outpu:

the remainder is 2

Relational Operator in Python

Relational Operator is used to comparing values between two operands and returns true or false value. Relational operators are mostly used in loops and conditional statements.

Operator NameHow it works
< less than operatorchecks if the left value is less than the right value
> greater than operator checks if the left value is greater than the right value
<= less than and equal to operator checks if the left value is less than and equal to the right value
>= greater than and equal to operator checks if the left value is greater than and equal to the right value
== equal operatorchecks if both left and right values are equal
!= or (<>) not equal to operator checks if both left and right values are not equal

Example 1:

a=20
b=3
print("the answer is   ",a<b)

Output:

the answer is False

Example 2:

a=20
b=20
print("the answer is   ",a==b)

Output:

the answer is True+

Assignment Operator in Python

Assignment operators are used to assigning values to the variables.

Operator Name How it works
=It assigns a value from either side such as a=10

Arithmetic Assignment Operator in Python

Here assignment operator is used with combination to an arithmetic operator to form an expression, such as a+=10

Operator Name SampleDescription
+=a+=10adds 10 value to the existing value of a and stores back to a variable. a+=10 is equal to a=a+10
-=a-=10 subtracts 10 value to the existing value of a and stores back to a variable. a-=10 is equal to a=a-10
*=a*=10 multiplies 10 value to the existing value of a and stores back to a variable. a*=10 is equal to a=a*10
/=a*=10 dives a by 10 and stores the answer back to a. a/=10 is equal to a=a/10

Example 1:

count=10
count+=1
print(count)
count*=2;
print(count)

Output:

11
22

Logical Operator in Python

Effects on relational expression’s results also AND, OR combines two or more Relational expressions,  Results will be returned either TRUE( 1 ) or FALSE ( 0 ) regards with specific situations.

Operator Name Description
AND A< b AND c > d
OR a <  b OR c > d
NOT NOT ( a < b )

Example 1:

a=10
b=20
c=15
print((a<b) and (b<c))

Output:

False

Explaining the above code, a=10 b=20, c=15 the first condition a<b which is 10<20 true, second condition b<c which is 20<15 false,in the caes of AND true and false will result to false.

Support us by sharing this post