Python Operators
Master the building blocks of Python programming with operators
🔧 Understanding Operators
Operators are special symbols that perform operations on one or more operands (values or variables). They are the building blocks of any programming language, allowing you to manipulate data and make decisions.
# Examples of Python operators
x = 10 # Assignment operator
y = 5
sum = x + y # Arithmetic operator
is_greater = x > y # Comparison operator
result = x and y # Logical operator
z = x | y # Bitwise operator
Operator Types
Arithmetic
Mathematical operations
x = 10 + 5 # Addition
y = 10 - 5 # Subtraction
z = 10 * 5 # Multiplication
Assignment
Assign values to variables
x = 10 # Basic assignment
x += 5 # Add and assign
x *= 2 # Multiply and assign
Comparison
Compare values and return boolean
x == y # Equal to
x != y # Not equal to
x > y # Greater than
Logical
Combine conditional statements
x and y # Both must be True
x or y # At least one True
not x # Opposite of x
🔹 Arithmetic Operators
Perform mathematical operations on numbers
x = 15
y = 4
print(f"x + y = {x + y}") # Addition: 19
print(f"x - y = {x - y}") # Subtraction: 11
print(f"x * y = {x * y}") # Multiplication: 60
print(f"x / y = {x / y}") # Division: 3.75
print(f"x // y = {x // y}") # Floor division: 3
print(f"x % y = {x % y}") # Modulus: 3
print(f"x ** y = {x ** y}") # Exponentiation: 50625
🔹 Assignment Operators
Assign values to variables with optional operations
a = 10
print(f"Initial a: {a}")
a += 5 # Same as: a = a + 5
print(f"a += 5: {a}") # 15
a -= 3 # Same as: a = a - 3
print(f"a -= 3: {a}") # 12
a *= 2 # Same as: a = a * 2
print(f"a *= 2: {a}") # 24
a /= 4 # Same as: a = a / 4
print(f"a /= 4: {a}") # 6.0
🔹 Comparison Operators
Compare values and return True or False
x = 10
y = 12
print(f"x == y: {x == y}") # Equal to: False
print(f"x != y: {x != y}") # Not equal: True
print(f"x > y: {x > y}") # Greater than: False
print(f"x < y: {x < y}") # Less than: True
print(f"x >= y: {x >= y}") # Greater or equal: False
print(f"x <= y: {x <= y}") # Less or equal: True
🔹 Logical Operators
Combine conditional statements
a = True
b = False
c = 5
d = 10
# and - returns True if both statements are true
print(f"a and b: {a and b}") # False
print(f"c < 10 and d > 5: {c < 10 and d > 5}") # True
# or - returns True if one of the statements is true
print(f"a or b: {a or b}") # True
print(f"c > 10 or d < 5: {c > 10 or d < 5}") # False
# not - reverses the result
print(f"not a: {not a}") # False
print(f"not b: {not b}") # True
🔹 Identity and Membership
Check object identity and membership in sequences
# Identity operators
list1 = [1, 2, 3]
list2 = [1, 2, 3]
list3 = list1
print(f"list1 is list2: {list1 is list2}") # False (different objects)
print(f"list1 is list3: {list1 is list3}") # True (same object)
# Membership operators
my_list = [10, 20, 30, 40, 50]
my_string = "Hello Python"
print(f"30 in my_list: {30 in my_list}") # True
print(f"'Python' in my_string: {'Python' in my_string}") # True
print(f"60 not in my_list: {60 not in my_list}") # True
🔹 Bitwise Operators
Operate on binary representations of numbers
a = 10 # Binary: 1010
b = 4 # Binary: 0100
print(f"a & b = {a & b}") # Bitwise AND: 0
print(f"a | b = {a | b}") # Bitwise OR: 14
print(f"a ^ b = {a ^ b}") # Bitwise XOR: 14
print(f"~a = {~a}") # Bitwise NOT: -11
print(f"a << 2 = {a << 2}") # Left shift: 40
print(f"a >> 2 = {a >> 2}") # Right shift: 2
🔹 Operator Precedence
Order in which operators are evaluated
# Without parentheses
result1 = 2 + 3 * 4
print(f"2 + 3 * 4 = {result1}") # 14 (multiplication first)
# With parentheses
result2 = (2 + 3) * 4
print(f"(2 + 3) * 4 = {result2}") # 20 (addition first)
# Complex expression
result3 = 2 ** 3 * 4 + 1
print(f"2 ** 3 * 4 + 1 = {result3}") # 33 (exponentiation, then multiplication, then addition)