Python Menu

Operators in Python


The operator can be defined as a symbol which is responsible for a particular operation between two operands.
Python provides a variety of operators described as follows.

  • Arithmetic operators
  • Assignment Operators
  • Comparison operators
  • Bitwise Operators
  • Identity Operators
  • Logical Operators
  • Membership Operators

1. Arithmetic operators

Arithmetic operators are used to perform arithmetic operations between two operands.

Operator Description
+ (addition) Add two operands
- (subtraction) Subtract right operand from the left
*(multiplication) Multiply two operands
/ (divide) Divide left operand by the right one (always results into float)
%( reminder) Modulus - remainder of the division of left operand by the right
// (floor division) Floor division - division that results into whole number adjusted to the left in the number line
** (exponent) Exponent - left operand raised to the power of right

 Python Example Program - Arithmetic operators in Python


Example:
x = 20
y = 10
print('x + y =',x+y)
print('x - y =',x-y)
print('x * y =',x*y)
print('x / y =',x/y)
print('x // y =',x//y)
print('x ** y =',x**y)
Output:
x + y = 30
x - y = 10
x * y = 200
x / y = 2
x // y = 0
x ** y = 8

2. Assignment Operators

The assignment operators are used to assign the value of the right expression to the left operand.

Operator Description
= (Assigns to) Assigns values from right side operands to left side operand.
+= (Assignment after Addition) It adds right operand to the left operand and assign the result to left operand.
-= (Assignment after Subtraction) It subtracts right operand from the left operand and assign the result to left operand.
*= (Assignment after Multiplication) It multiplies right operand with the left operand and assign the result to left operand.
/= (Assignment after Division) It divides left operand with the right operand and assign the result to left operand.
%= (Assignment after Modulus) It takes modulus using two operands and assign the result to left operand.
**= (Assignment after Exponent) Performs exponential (power) calculation on operators and assign value to the left operand.
//= (Assignment after floor division) It performs floor division on operators and assign value to the left operand.

 Python Example Program - Assignment operators in Python


Example:
# assign  a value to variable
x = 10   
y = 6 
# x = x + y assignment after addition   
x += y   
# print result  
print( "Addition = ", x)

# x = x - y assignment after subtraction   
x -= y   
# print result 
print( "Subtraction = ", x)

# x = x * y assignment after multiplication   
x *= y   
# print result  
print( "Multiplication = ", x)

# x = x / y assignment after division   
x /= y   
# print result  
print( "Division = ", x)

# x = x % y assignment after modulus   
x %= y   
# print result  
print( "Modulus = ", x)

# x = x ** y Assignment after Exponent   
x **= y   
# print result  
print( "Exponent = ", x)

# x = x // y Assignment after floor division   
x //= y   
# print result  
print( "Floor division = ", x)
		
Output:
Addition =  16
Subtraction =  10
Multiplication =  60
Division =  10.0
Modulus =  4.0
Exponent =  4096.0
Floor division =  682.0
		

3. Comparison operators

Comparison operators are used to comparing the value of the two operands and returns boolean true or false accordingly.

Operator Description
== (Equal to) Equal to - True if both operands are equal.
!= (Not equal to) Not equal to - True if operands are not equal.
<= (Less than or equal) Less than or equal to - True if left operand is less than or equal to the right
>= (Greater than or equal) Greater than or equal to - True if left operand is greater than or equal to the right
< (Less than) Less that - True if left operand is less than the right
> (Greater than) Greater that - True if left operand is greater than the right

 Python Example Program - Comparison operators in Python


Example:
x = 20
y = 10
print('x > y  is',x > y)
print('x < y  is',x < y)
print('x == y is',x == y)
print('x != y is',x != y)
print('x >= y is',x >= y)
print('x <= y is',x <= y)
		
Output:
x > y  is True
x < y  is False
x == y is False
x != y is True
x >= y is True
x >= y is False
		

4. Bitwise Operators

The bitwise operators perform bit by bit operation on the values of the two operands.

Operator Description
& (binary and) Sets each bit to 1 if both bits are 1
| (binary or) Sets each bit to 1 if one of two bits is 1
^ (binary xor) Sets each bit to 1 if only one of two bits is 1
~ (negation) Inverts all the bits
<< (left shift) Shift left by pushing zeros in from the right and let the leftmost bits fall off
>> (right shift) Shift right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off

 Python Example Program - Bitwise operators in Python


Example:
a = 9       # equal to 1001
b = 12      # equal to 1100

print ("a & b = ",a & b)
print ("a | b = ",a | b)
print("a ^ b = ",a ^ b)
print("~a = ",~a)
print("a << 2 = ",a << 2)
print("a >> 2 = ",a >> 2)
Output:
a & b = 8
a | b = 13
a ^ b = 5
~a = -10
a << 2 = 36
a >> 2 = 2

5. Identity Operators

Identity operators are used to compare the objects, not if they are equal, but if they are actually the same object, with the same memory location.

Operator Description
is Returns true if both variables are the same object ( if id(x) equals id(y) )
is not Returns true if both variables are not the same object

 Python Example Program - Identity operators in Python


Example:
x=10
y=10
print(x is y)
print("id(x)= %d id(y)= %d" %(id(x),id(y)))
y=20
print(x is not y)
print("id(x)= %d id(y)= %d" %(id(x),id(y)))
Output:
True
id(x)= 10105376 id(y)= 10105376
True
id(x)= 10105376 id(y)= 10105696

6. Logical Operators

The logical operators are used primarily in the expression evaluation to make a decision.

Operator Description
and (logical and) Returns True if both statements are true
or (logical or) Returns True if one of the statements is true
not (logical not) Reverse the result, returns False if the result is true

 Python Example Program - Logical operators in Python


Example:
x = True
y = False
print('x and y is',x and y)
print('x or y is',x or y)
print('not x is',not x)
Output:
x and y is False
x or y is True
not x is False

7. Membership Operators

The logical operators are used primarily in the expression evaluation to make a decision.

Operator Description
in it Returns True if a sequence with the specified value is present in the object (list, tuple, or dictionary)
not in ) it Returns True if a sequence with the specified value is not present in the object (list, tuple, or dictionary)

 Python Example Program - Membership operators in Python


Example:
x = 'Hello world'
print('H' in x)
print('hello' not in x)
Output:
True
True

Next Topic :Conditional Statements in Python