19
DecOperators in Python
Operators are foundation of any programming language and same applicable for Python. Operators are special symbol which help to carry out mathematical and logical computations. The values on which operators operate are know as operands.
For Example>>> 2 + 6 >>>8
Here ‘+’ is an arithmetic operator. In above example 2 and 6 are the operands and ‘+’ is the operator.
Python Arithmetic operators
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication etc. Arithmetic operators available in Python are : ‘+’ , ‘-’, ‘/’, ‘*’, ‘%’, ‘//’, ‘**’
Examples of Arithmetic Operator
x = 10 y = 5 # Addition of values add = x + y # Subtraction of values sub = x - y # Multiplication of values mult = x * y # Division(float) of values div_float = x / y # Division(floor) of values div_floor = x // y # Modulo of both values modu = x % y # print results print(“Addition result :- ”,add) print(“Subtraction result :- ”, sub) print(“Multiplication result :- ”,mult) print(“Division float result :- ”, div_float) print(“Division floor result :- ”, div_floor) print(“Modulo result :- ”, modu)Output
Addition result :- 15 Subtraction result :- 5 Multiplication result :- 50 Division float result :- 2.0 Division floor result :- 2 Modulo result :- 0
Python Comparison operators
Comparison operators are also called relational operators as they decides the relation between two operands. It returns either a “True” or “False” according to condition. Arithmetic operators available in Python are : ‘<’ , ‘>’, ‘==’, ‘!=’, ‘<=’, ‘>=’
Examples of Comparison Operator
x = 20 y = 30 # x > y is False print(x > y) # x < y is True print(x < y) # x == y is False print(x == y) # x != y is True print(x != y) # x >= y is False print(x >= y) # x <= y is True print(x <= y)Output
False True False True False True
Python Logical operators
Logical operators performs operations like Logical AND, Logical OR and Logical NOT. Logical operators available in Python are : ‘AND’ , ‘OR’, ‘NOT’. Logical Operators are also used to combine multiple conditions.
Examples of Logical Operator
X = True Y = False # AND operation print(X AND Y) # OR operation print(X OR Y) # NOT Operation print(NOT Y)Output
False True True
Python Bitwise operators
Bitwise operators works on bits and perform bit by bit operation on operands. Bitwise operators available in Python are : ‘&’ , ‘|’, ‘~’, ‘^’, ‘>>’, ‘<<’.
Examples of Bitwise Operator
x = 10 y = 5 # Bitwise AND operation print(x & y) # Bitwise OR operation print(x | y) # Bitwise complement print(~x) # Bitwise XOR operator print(x ^ y) # Bitwise Right shift operator print(x>> 2) # Bitwise Left shift operator print(x<< 2)Output
0 15 -11 15 2 40
Python Assignment operators
Assignment operators are used assign a value to operand. Assignment operators available in Python are : ‘=’, ‘+=’ , ‘-=’, ‘/=’, ‘*=’, ‘%=’, ‘//=’, ‘**=’, ‘&=’, ‘|=’, ‘^=’, ‘>>=’, ‘<<=’.
Examples of Assignment Operator
# Assignment x = 5 print(“ Assignment :-”, x) # Addition AND x += 3 print(“ Addition :-”, x) #Subtraction AND x -= 3 print(“ Subtraction :-”, x) #Multiplication AND x *= 3 print(“ Multiplication :-”, x) #Division AND x /= 3 print(“ Division :-”, x) #Exponent AND x **= 3 print(“ Exponent :-”, x) #Division Floor x = 125 x //= 3 print(“ Division floor :-”, x) #Modulus AND x %= 3 print(“Modulus :-”, x) #Bitwise AND x = 5 x &= 3 print(“ Bitwise AND :-”, x) #Bitwise OR x = 5 x |= 3 print(“ Bitwise OR :-”, x) #Bitwise XOR x = 5 x ^= 3 print(“ Bitwise XOR :-”, x) #Bitwise right shift x = 5 x>>= 3 print(“ Bitwise right shift :-”, x) #Bitwise left shift x = 5 x <<= 3 print(“ Bitwise left shift :-”, x)Output
Assignment :- 5 Addition :- 8 Subtraction :- 5 Multiplication :- 15 Division :- 5.0 Exponent :- 125.0 Division floor :- 41 Modulus :- 2 Bitwise AND :- 1 Bitwise OR :- 7 Bitwise XOR :- 6 Bitwise right shift :- 0 Bitwise left shift :- 40
Python Special operators
Identity Operator “is” and “is not” are identity operators in Python. Identity operators are used to check if objects resides in same memory locations. Values is same doesn’t mean they resides in same memory location
Examples of Identity operators
x = 5 y = 5 a = “dotnettricks” b = “dotnettricks” d = [1,2,3] e = [1,2,3] print(x is not y) print(a is b) print(d is e) # False since lists are mutableOutput
False True False
Membership Operator - Membership operators are used to test whether a value or variable is in present in sequence. ‘in’ and ‘not in’ are membership operators available in Python.
Examples of membership operators
a = “dotnettricks” b = {2: ‘a’, 3: ‘b’} d = [1,2,3] print("dot" in a) print("net" not in a) print(2 in b) # print(3 in d)Output
True False True True
Operators Precedence in Python
Below is lists of all operators from highest precedence to lowest.
Summary
Python has all the operators which are available in other languages. There are more ways and different syntax are available to implement those operators in Python and some operators might have different meanings in other programming languages and somewhat different in Python.
Take our free skill tests to evaluate your skill!

In less than 5 minutes, with our skill test, you can identify your knowledge gaps and strengths.