Operators in Python are special symbols or keywords used to perform operations on variables and values. They are the building blocks of any programming language and enable developers to manipulate data and variables effectively.
Types of Operators in Python
Python provides a rich set of operators categorized as follows:
- Arithmetic Operators
- Comparison (Relational) Operators
- Assignment Operators
- Logical Operators
- Bitwise Operators
- Membership Operators
- Identity Operators
Each type serves a unique purpose in programming, and understanding them is essential for writing efficient code.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical operations like addition, subtraction, multiplication, etc.
Operator | Description | Example |
---|---|---|
+ | Addition | 5 + 3 → 8 |
– | Subtraction | 5 – 3 → 2 |
* | Multiplication | 5 * 3 → 15 |
/ | Division | 5 / 2 → 2.5 |
% | Modulus (Remainder) | 5 % 2 → 1 |
** | Exponentiation | 5 ** 3 → 125 |
// | Floor Division | 5 // 2 → 2 |
Example:
x = 10
y = 3
print(x + y) # Output: 13
print(x - y) # Output: 7
print(x * y) # Output: 30
print(x / y) # Output: 3.3333333333333335
print(x % y) # Output: 1
print(x ** y) # Output: 1000
print(x // y) # Output: 3
2. Comparison Operators
Comparison operators compare two values and return a Boolean value (True or False).
Operator | Description | Example |
---|---|---|
== | Equal to | 5 == 5 → True |
!= | Not equal to | 5 != 3 → True |
> | Greater than | 5 > 3 → True |
< | Less than | 5 < 3 → False |
>= | Greater than or equal to | 5 >= 5 → True |
<= | Less than or equal to | 5 <= 3 → False |
Example:
a = 10
b = 20
print(a > b) # Output: False
print(a == b) # Output: False
print(a <= b) # Output: True
3. Assignment Operators
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assign | x = 5 |
+= | Add and assign | x += 3 → x = x + 3 |
-= | Subtract and assign | x -= 3 → x = x – 3 |
*= | Multiply and assign | x *= 3 → x = x * 3 |
/= | Divide and assign | x /= 3 → x = x / 3 |
%= | Modulus and assign | x %= 3 → x = x % 3 |
**= | Exponent and assign | x **= 3 → x = x ** 3 |
//= | Floor division and assign | x //= 3 → x = x // 3 |
Example:
x = 5
x += 3
print(x) # Output: 8
x *= 2
print(x) # Output: 16
4. Logical Operators
Logical operators are used to combine conditional statements.
Operator | Description | Example |
---|---|---|
and | Returns True if both are True | True and False → False |
or | Returns True if one is True | True or False → True |
not | Reverses the Boolean value | not True → False |
Example:
x = 10
y = 5
print(x > 5 and y < 10) # Output: True
print(x > 10 or y < 10) # Output: True
print(not (x > y)) # Output: False
5. Bitwise Operators
Bitwise operators work on binary representations of numbers.
Operator | Description | Example |
---|---|---|
& | AND | 5 & 3 → 1 |
` | ` | OR |
^ | XOR | 5 ^ 3 → 6 |
~ | NOT | ~5 → -6 |
<< | Left Shift | 5 << 1 → 10 |
>> | Right Shift | 5 >> 1 → 2 |
Example:
x = 5 # Binary: 0101
y = 3 # Binary: 0011
print(x & y) # Output: 1 (Binary: 0001)
print(x | y) # Output: 7 (Binary: 0111)
print(x ^ y) # Output: 6 (Binary: 0110)
6. Membership Operators
Membership operators check whether a value exists in a sequence (e.g., string, list, tuple).
Operator | Description | Example |
---|---|---|
in | Returns True if present | ‘a’ in ‘apple’ → True |
not in | Returns True if not present | ‘z’ not in ‘apple’ → True |
Example:
fruits = ['apple', 'banana', 'cherry']
print('apple' in fruits) # Output: True
print('grape' not in fruits) # Output: True
7. Identity Operators
Identity operators compare memory locations of two objects.
Operator | Description | Example |
---|---|---|
is | Returns True if same object | x is y |
is not | Returns True if not same object | x is not y |
Example:
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # Output: True
print(x is not z) # Output: True