Operators in Python are special symbols used to perform operations on variables and values. Python supports various types of operators, including arithmetic, comparison, logical, bitwise, assignment, identity, and membership operators.
1. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Operator | Description | Example |
---|---|---|
+ | Addition | x + y |
- | Subtraction | x - y |
* | Multiplication | x * y |
/ | Division | x / y |
// | Floor Division | x // y |
% | Modulus (Remainder) | x % y |
** | Exponentiation | x ** y |
Example:
x = 10
y = 3
print(x + y) # 13
print(x - y) # 7
print(x * y) # 30
print(x / y) # 3.3333
print(x // y) # 3
print(x % y) # 1
print(x ** y) # 1000
2. Comparison Operators
Comparison operators compare values and return a Boolean result (True
or False
).
Operator | Description | Example |
---|---|---|
== | Equal to | x == y |
!= | Not equal to | x != y |
> | Greater than | x > y |
< | Less than | x < y |
>= | Greater than or equal to | x >= y |
<= | Less than or equal to | x <= y |
Example:
x = 10
y = 5
print(x == y) # False
print(x != y) # True
print(x > y) # True
print(x < y) # False
print(x >= y) # True
print(x <= y) # False
3. Logical Operators
Logical operators are used to combine conditional statements.
Operator | Description | Example |
---|---|---|
and | Returns True if both conditions are true | x > 5 and x < 15 |
or | Returns True if at least one condition is true | x > 5 or x < 3 |
not | Reverses the logical state | not(x > 5) |
Example:
x = 10
y = 5
print(x > 5 and y < 10) # True
print(x > 5 or y > 10) # True
print(not(x > 5)) # False
4. Assignment Operators
Assignment operators assign values to variables.
Operator | Example | Equivalent to |
---|---|---|
= | x = 5 | x = 5 |
+= | x += 3 | x = x + 3 |
-= | x -= 2 | x = x - 2 |
*= | x *= 4 | x = x * 4 |
/= | x /= 2 | x = x / 2 |
//= | x //= 2 | x = x // 2 |
%= | x %= 3 | x = x % 3 |
**= | x **= 2 | x = x ** 2 |
Example:
x = 10
x += 5 # Equivalent to x = x + 5
print(x) # 15
5. Identity Operators
Identity operators compare the memory locations of two objects.
Operator | Description | Example |
---|---|---|
is | Returns True if both variables point to the same object | x is y |
is not | Returns True if variables point to different objects | x is not y |
Example:
a = [1, 2, 3]
b = a
c = [1, 2, 3]
print(a is b) # True (same memory location)
print(a is c) # False (different objects with same value)
6. Membership Operators
Membership operators check if a value exists in a sequence (list, tuple, string, etc.).
Operator | Description | Example |
---|---|---|
in | Returns True if value is in sequence | 'a' in 'apple' |
not in | Returns True if value is not in sequence | 5 not in [1, 2, 3] |
Example:
fruits = ['apple', 'banana', 'cherry']
print('apple' in fruits) # True
print('grape' not in fruits) # True
7. Bitwise Operators
Bitwise operators perform operations on binary numbers.
Operator | Description | Example |
---|---|---|
& | AND | x & y |
` | ` | OR |
^ | XOR | x ^ y |
~ | NOT | ~x |
<< | Left Shift | x << 2 |
>> | Right Shift | x >> 2 |
Example:
x = 5 # Binary: 0101
y = 3 # Binary: 0011
print(x & y) # 1 (0001)
print(x | y) # 7 (0111)
print(x ^ y) # 6 (0110)
print(~x) # -6 (inverts bits)
Summary
✅ Arithmetic operators perform mathematical calculations.
✅ Comparison operators compare values.
✅ Logical operators are used for conditions.
✅ Assignment operators modify values.
✅ Identity and membership operators check object identity and membership.
✅ Bitwise operators manipulate binary values.