Operators in Python¶
An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Python language is rich in built-in operators and provides following types of operators.
Types of Operators¶
We have the following types of operators in Python programming −
- Arithmetic operators
- Assignment operators
- Relational/Comparison Operators
- Logical Operators
- Bitwise Operators
- Identity Operator
- Membership Operator
Arithmetic Operator¶
Arithmetic operators are used with numeric values to perform common mathematical operations:
| Operator | Name |
|---|---|
| + | Addition |
| – | Subtraction |
| * | Multiplication |
| / | Division |
| ** | Exponent |
| % | Modulus (Remainder from division) |
| // | Floor Division |
a=int(input("Enter First Number:"))
b=int(input("Enter First Number:"))
print("Addition:",a+b)
print("Subtraction:",a-b)
print("Multiplication:",a*b)
print("Division",a/b)
print("Floor Division:",a//b)
print("Modulus",a%b)
print("Exponentiation:",a**b)
Python Assignment Operators¶
Assignment operators are used to assign values to variables:
| Operator | Example | Same As |
|---|---|---|
| = | x = 5 | x = 5 |
| += | x += 5 | x = x + 5 |
| -= | x -= 5 | x = x – 5 |
| *= | x *= 5 | x = x * 5 |
| /= | x /= 5 | x = x / 5 |
| //= | x //= 5 | x = x // 5 |
| %= | x %= 5 | x = x % 5 |
| **= | x **= 5 | x = x ** 5 |
a = 10
print(a)
a = 10
b = 20
a = a + b
print(a)
a = 10
b = 20
a += b
print(a)
Python Comparison/Relational Operators¶
Comparison operators are used to compare two values:
| Operator | Description |
|---|---|
| < | Less than |
| > | Greater than |
| <= | Less than or equal to |
| >= | Greater than or equal to |
| == | Equal to |
| != | Not equal to |
a=10
b=20
c = a < b
print(c)
a=10
b=20
c = a > b
print(c)
a=10
b=20
c = a <= b
print(c)
a=10
b=20
c = a >= b
print(c)
a=10
b=20
c = a == b
print(c)
a=10
b=20
c = a != b
print(c)
Python Logical Operators¶
Logical operators are used to combine conditional statements:
| Operator | Description | Example |
|---|---|---|
| and | Returns True if both statements are true | x < 5 and x < 10 |
| or | Returns True if one of the statements is true | x < 5 or x < 4 |
| not | Reverse the result, returns False if the result is true | not(x < 5 and x < 10) |
Logial AND¶
x = 1
print(x < 5 and x < 10)
print(10 and 20)
Logical OR¶
x = 1
print(x < 5 or x > 4)
print(10 or 20)
Logical NOT¶
x = 1
print(not(x < 5 or x > 4))
a=0
print(not a)
Python Bitwise Operators¶
Bitwise operators are used to compare (binary) numbers:
| Operator | Description |
|---|---|
| & | Bitwise AND |
| l | Bitwise OR |
| ^ | Bitwise XOR |
| << | Left Shift |
| >> | Right Shift |
| ~ | 1’s Complement |
a=10
b=6
print("Bitwise AND:",a&b)
print("Bitwise OR:",a|b)
print("Bitwise XOR:",a^b)
print("Left Shift:",a<<2)
print("Right Shift:",a>>2)
print("1's Complement:",~a)
Python 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 | identical – Returns True if both variables are the same object |
| is not | not identical – Returns True if both variables are not the same object |
l1=[1,2,3]
l2=[1,2,3]
print(l1==l2)
print(l1 is l2)
l3=l2
print(l2)
print(l3)
print(l2==l3)
print(l2 is l3)
del l2
print(l3)
Membership Operators¶
Membership operators are used to test if a sequence is presented in an object:
- in – Evaluates to true if it finds a variable in the specified sequence
- not in – Evaluates to true if it does not finds a variable in the specified sequence
| Operator | Description |
|---|---|
| in | Returns True if a sequence with the specified value is present in the object |
| not in | Returns True if a sequence with the specified value is not present in the object |
L=[12,353,545,2,45,67,89,23]
n=2
print(n in L)
print(n not in L)
Machine Learning Tutorials, Courses and Certifications