Operators are special symbols in Python that perform specific operations on one or more operands. Python supports a variety of operators, which are categorized as follows:
Arithmetic operators are used to perform mathematical operations such as addition, subtraction, multiplication, and division.
Operator | Description | Example |
---|---|---|
+ | Addition | a + b |
- | Subtraction | a - b |
* | Multiplication | a * b |
/ | Division | a / b |
// | Floor Division | a // b |
% | Modulus (remainder of division) | a % b |
** | Exponentiation (power) | a ** b |
Examples:
a = 10
b = 3
print(a + b) # Output: 13
print(a - b) # Output: 7
print(a * b) # Output: 30
print(a / b) # Output: 3.3333333333333335
print(a // b) # Output: 3
print(a % b) # Output: 1
print(a ** b) # Output: 1000
Comparison operators compare the values of two operands and return a boolean value (True or False).
Operator | Description | Example |
---|---|---|
== | Equal to | a == b |
!= | Not equal to | a != b |
> | Greater than | a > b |
< | Less than | a < b |
>= | Greater than or equal to | a >= b |
<= | Less than or equal to | a <= b |
Examples:
a = 10
b = 3
print(a == b) # Output: False
print(a != b) # Output: True
print(a > b) # Output: True
print(a < b) # Output: False
print(a >= b) # Output: True
print(a <= b) # Output: False
Logical operators are used to combine conditional statements.
Operator | Description | Example |
---|---|---|
and | Logical AND (both conditions must be true) | a and b |
or | Logical OR (one of the conditions must be true) | a or b |
not | Logical NOT (inverts the boolean value) | not a |
Examples:
a = True
b = False
print(a and b) # Output: False
print(a or b) # Output: True
print(not a) # Output: False
Bitwise operators perform bit-level operations on binary representations of integers.
Operator | Description | Example |
---|---|---|
& | Bitwise AND | a & b |
` | ` | Bitwise OR |
^ | Bitwise XOR | a ^ b |
~ | Bitwise NOT | ~a |
<< | Bitwise left shift | a << b |
>> | Bitwise right shift | a >> b |
Examples:
a = 10 # Binary: 1010
b = 4 # Binary: 0100
print(a & b) # Output: 0 (Binary: 0000)
print(a | b) # Output: 14 (Binary: 1110)
print(a ^ b) # Output: 14 (Binary: 1110)
print(~a) # Output: -11 (Binary: -(1010 + 1))
print(a << 2) # Output: 40 (Binary: 101000)
print(a >> 2) # Output: 2 (Binary: 10)
Assignment operators are used to assign values to variables.
Operator | Description | Example |
---|---|---|
= | Assignment | a = 5 |
+= | Addition assignment | a += 3 (a = a + 3) |
-= | Subtraction assignment | a -= 3 (a = a - 3) |
*= | Multiplication assignment | a *= 3 (a = a * 3) |
/= | Division assignment | a /= 3 (a = a / 3) |
//= | Floor division assignment | a //= 3 (a = a // 3) |
%= | Modulus assignment | a %= 3 (a = a % 3) |
**= | Exponentiation assignment | a **= 3 (a = a ** 3) |
&= | Bitwise AND assignment | a &= 3 (a = a & 3) |
` | =` | Bitwise OR assignment |
^= | Bitwise XOR assignment | a ^= 3 (a = a ^ 3) |
<<= | Bitwise left shift assignment | a <<= 3 (a = a << 3) |
>>= | Bitwise right shift assignment | a >>= 3 (a = a >> 3) |
Examples:
a = 10
a += 3 # Equivalent to: a = a + 3
print(a) # Output: 13
a -= 3 # Equivalent to: a = a - 3
print(a) # Output: 10
a *= 3 # Equivalent to: a = a * 3
print(a) # Output: 30
a /= 3 # Equivalent to: a = a / 3
print(a) # Output: 10.0
a //= 3 # Equivalent to: a = a // 3
print(a) # Output: 3.0
a %= 3 # Equivalent to: a = a % 3
print(a) # Output: 0.0
a **= 3 # Equivalent to: a = a ** 3
print(a) # Output: 0.0
a = 10
a &= 3 # Equivalent to: a = a & 3
print(a) # Output: 2
a |= 3 # Equivalent to: a = a | 3
print(a) # Output: 3
a ^= 3 # Equivalent to: a = a ^ 3
print(a) # Output: 0
a <<= 3 # Equivalent to: a = a << 3
print(a) # Output: 0
a >>= 3 # Equivalent to: a = a >> 3
print(a) # Output: 0