Python operators are special symbols that perform operations on operands. Python supports a variety of operators, including arithmetic, comparison, logical, bitwise, assignment, identity, and membership operators. Here’s an explanation of each type with examples:
1. Arithmetic Operators
These operators are used to perform basic arithmetic operations.
Addition (+
)
a = 5
b = 3
result = a + b # result is 8
Subtraction (-
)
result = a - b # result is 2
Multiplication (*
)
result = a * b # result is 15
Division (/
)
result = a / b # result is 1.6666666666666667
Floor Division (//
)
result = a // b # result is 1
Modulus (%
)
result = a % b # result is 2
Exponentiation (**
)
result = a ** b # result is 125
2. Comparison Operators
These operators compare the values of two operands and return a boolean value.
Equal to (==
)
result = (a == b) # result is False
Not equal to (!=
)
result = (a != b) # result is True
Greater than (>
)
result = (a > b) # result is True
Less than (<
)
result = (a < b) # result is False
Greater than or equal to (>=
)
result = (a >= b) # result is True
Less than or equal to (<=
)
result = (a <= b) # result is False
3. Logical Operators
These operators are used to combine conditional statements.
Logical AND (and
)
result = (a > 1 and b < 10) # result is True
Logical OR (or
)
result = (a < 1 or b > 1) # result is True
Logical NOT (not
)
result = not(a > 1) # result is False
4. Bitwise Operators
These operators perform bitwise operations on integers.
Bitwise AND (&
)
result = a & b # result is 1 (binary AND)
Bitwise OR (|
)
result = a | b # result is 7 (binary OR)
Bitwise XOR (^
)
result = a ^ b # result is 6 (binary XOR)
Bitwise NOT (~
)
result = ~a # result is -6 (binary NOT)
Bitwise Left Shift (<<
)
result = a << 1 # result is 10 (binary left shift)
Bitwise Right Shift (>>
)
result = a >> 1 # result is 2 (binary right shift)
5. Assignment Operators
These operators are used to assign values to variables.
Simple Assignment (=
)
c = 10 # assigns 10 to c
Add and Assign (+=
)
c += 5 # c is now 15
Subtract and Assign (-=
)
c -= 3 # c is now 12
Multiply and Assign (*=
)
c *= 2 # c is now 24
Divide and Assign (/=
)
c /= 4 # c is now 6.0
Floor Divide and Assign (//=
)
c //= 2 # c is now 3.0
Modulus and Assign (%=
)
c %= 2 # c is now 1.0
Exponent and Assign (**=
)
c **= 3 # c is now 1.0
Bitwise AND and Assign (&=
)
c &= 1 # c is now 1
Bitwise OR and Assign (|=
)
c |= 2 # c is now 3
Bitwise XOR and Assign (^=
)
c ^= 1 # c is now 2
Bitwise Left Shift and Assign (<<=
)
c <<= 1 # c is now 4
Bitwise Right Shift and Assign (>>=
)
c >>= 1 # c is now 2
6. Identity Operators
These operators compare the memory locations of two objects.
is
x = [1, 2, 3]
y = x
result = (x is y) # result is True
is not
z = [1, 2, 3]
result = (x is not z) # result is True
7. Membership Operators
These operators test for membership in a sequence (like lists, strings, or tuples).
in
fruits = ["apple", "banana", "cherry"]
result = ("banana" in fruits) # result is True
not in
result = ("grape" not in fruits) # result is True
Examples of Using Operators
# Arithmetic Operators
a = 10
b = 3
print(a + b) # 13
print(a - b) # 7
print(a * b) # 30
print(a / b) # 3.3333333333333335
print(a // b) # 3
print(a % b) # 1
print(a ** b) # 1000
# Comparison Operators
print(a == b) # False
print(a != b) # True
print(a > b) # True
print(a < b) # False
print(a >= b) # True
print(a <= b) # False
# Logical Operators
print(a > 1 and b < 5) # True
print(a < 1 or b < 5) # True
print(not(a > 1)) # False
# Bitwise Operators
print(a & b) # 2
print(a | b) # 11
print(a ^ b) # 9
print(~a) # -11
print(a << 1) # 20
print(a >> 1) # 5
# Assignment Operators
c = 10
c += 5
print(c) # 15
# Identity Operators
x = [1, 2, 3]
y = x
z = [1, 2, 3]
print(x is y) # True
print(x is not z) # True
# Membership Operators
fruits = ["apple", "banana", "cherry"]
print("banana" in fruits) # True
print("grape" not in fruits) # True