In Python, operators are symbols and used to perform a specific operation on given values. Python has a rich collection of operators, we will learn here.
Types of Operators in Python
- Arithmetic Operators
- Assignment Operators
- Relational Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Arithmetic Operators (Mathematical Operators)
Arithmetic or Mathematical operators in Python are used to perform mathematical tasks.
|%||Remainder of Division (modulus)|
Example 1: Arithmetic operators in Python.
#Example of Arithmetic operators in Python a=25 b=3 c=a+b print("Addition =",c) c=a-b print("Subtract =",c) c=a*b print("Multiply =",c) c=a/b print("Divison =",c) c=a//b print("Floor Divison =",c) c=a%b print("Remainder =",c) c=a**b print("Power =",c)
Assign a value to a variable, the assignment operator is used.
|=||a=5, 5 assign to variable a|
|+=||a=a+b can also write as a+=b|
|-=||a=a-b can also write as a-=b|
|*=||a=a*b can also write as a*=b|
|/=||a=a/b can also write as a/=b|
|//=||a=a//b can also write as a//=b|
|%=||a=a%b can also write as a%=b|
|**=||a=a**b can also write as a**=b|
Relational Operator (Comparison Operator)
A relational operator compares or check the relation of two operands and return Boolean value as a result.
|<=||Less then or Equal|
|>=||Greater then or equal|
|!=||Not equal to|
Example 2: Relational operators in Python
#Example of Relational operators in Python a=25 b=25 print(a<b) print(a>b) print(a<=b) print(a>=b) print(a==b) print(a!=b)
Logical operators in Python are used to combine relational expression and return boolean value as a result.
|and||Logical AND||Return TRUE if all expression is true, otherwise FALSE.|
|or||Logical OR||Return TRUE if all or any one expression is true, otherwise FALSE.|
|not||Logical NOT||Reverse the state of the expression(True expression to False and False to True).|
Example 3: Logical operators in Python
#Example of Logical operators in Python a=10 print(a>0 and a<100) print(a>0 and a>100) print(a>0 or a<100) print(a>0 or a>100) print(a>0 and a<100) print(not a>0)
Bitwise Operators in Python
Bitwise operators are used to working with binary numbers. Decimal numbers are converted into binary first, then bitwise operator works on each bit
Example 4: Bitwise operators in Python
#Example of Bitwise operators in Python a=11 b=3 print("Bitwise And",a & b) # 00001011 & 00000011, result 00000011 ( 3 as Decimal) print("Bitwise Or",a | b) # 00001011 | 00000011, result 00001011 ( 11 as Decimal) print("Bitwise XOR",a ^ b) # 00001011 ^ 00000011, result 00001000 ( 8 as Decimal) print("Bitwise Complement",~a) # 2's Complement print("Left Shift",a << b) # 00001011 << 3, result 01011000 ( 88 as Decimal), remove 3 digit from left and add three 0 to right print("Right Shift",a >> b) # 00001011 >> 3, result 00000001 ( 1 as Decimal), remove 3 digit from right and add three 0 to left
Membership Operators in Python is used test value is in object or not. It returns a boolean value as a result.
|in||Returns True, if the value or variable is present in sequence.|
|not in||Returns True, if the value or variable is not present in sequence.|
Example 5: Membership operators in Python
#Example of Membership operators in Python n=[11,22,33,44] print(11 in n) print(11 not in n)
Identity operators in Python are used to compare the objects and return boolean value as a result.
|is||Returns True, if both the value or variable is the same object.|
|is not||Returns True, if both the value or variable is not the same object.|
Example 6: Identity operators in Python
#Example of Identity operators in Python n=10 print(n is 10) print(n is not 10)