Arithmetic Operators
a, b = 10, 3
print(a + b) # 13 โ addition
print(a - b) # 7 โ subtraction
print(a * b) # 30 โ multiplication
print(a / b) # 3.3333... โ true division (always float)
print(a // b) # 3 โ floor division (integer result)
print(a % b) # 1 โ modulo (remainder)
print(a ** b) # 1000 โ exponentiation
Comparison Operators
x, y = 5, 10
print(x == y) # False โ equal
print(x != y) # True โ not equal
print(x < y) # True โ less than
print(x > y) # False โ greater than
print(x <= 5) # True โ less than or equal
print(x >= 5) # True โ greater than or equal
Comparisons return
TrueorFalse.
Logical Operators
a = True
b = False
print(a and b) # False โ both must be True
print(a or b) # True โ at least one must be True
print(not a) # False โ negation
# Short-circuit evaluation:
print(False and 1/0) # False (1/0 never evaluated)
print(True or 1/0) # True (1/0 never evaluated)
Truthy Logical Returns
# 'and' returns first falsy value, or last value
print(0 and 5) # 0
print(3 and 5) # 5
print("" and "hi") # ""
# 'or' returns first truthy value, or last value
print(0 or 5) # 5
print(3 or 5) # 3
print("" or "hi") # "hi"
Assignment Operators
x = 10
x += 5 # x = x + 5 โ 15
x -= 3 # x = x - 3 โ 12
x *= 2 # x = x * 2 โ 24
x /= 4 # x = x / 4 โ 6.0
x //= 2 # x = x // 2 โ 3.0
x **= 3 # x = x ** 3 โ 27.0
x %= 5 # x = x % 5 โ 2.0
Membership Operators
fruits = ["apple", "banana", "cherry"]
print("apple" in fruits) # True
print("grape" not in fruits) # True
print("x" in "Python") # True โ works on strings too
Identity Operators
a = [1, 2, 3]
b = a # same object
c = [1, 2, 3] # different object, same value
print(a is b) # True โ same identity (same object in memory)
print(a is c) # False โ different objects
print(a == c) # True โ same value
# Use 'is' only to compare to None:
x = None
print(x is None) # True (preferred over x == None)
Bitwise Operators
a = 0b1010 # 10
b = 0b1100 # 12
print(a & b) # 0b1000 = 8 โ AND
print(a | b) # 0b1110 = 14 โ OR
print(a ^ b) # 0b0110 = 6 โ XOR
print(~a) # -11 โ NOT (complement)
print(a << 1) # 20 โ left shift
print(a >> 1) # 5 โ right shift
Operator Precedence
From highest to lowest:
()โ parentheses**โ exponentiation+x,-x,~xโ unary*,/,//,%+,-<<,>>&,^,|- Comparison:
==,!=,<,>, etc. notandor
result = 2 + 3 * 4 ** 2 - 1 # 2 + 3*16 - 1 = 49
print(result) # 49
Exercises
Exercise 1: Arithmetic
Write a program that calculates and prints:
- The area of a circle with radius 7 (use
3.14159) - The remainder when 2**32 is divided by 1000
Solution:
radius = 7
area = 3.14159 * radius ** 2
print(f"Area: {area:.2f}") # 153.94
remainder = 2**32 % 1000
print(f"Remainder: {remainder}") # 296
Exercise 2: Even or Odd
Using only the modulo operator, determine if 127 is even or odd.
Solution:
n = 127
is_even = n % 2 == 0
print("Even" if is_even else "Odd") # Odd
Exercise 3: Chained Comparison
Python supports chained comparisons. Predict the output:
x = 5
print(1 < x < 10)
print(1 < x < 4)
print(x == 5 == 5.0)
Solution:
True # 1 < 5 AND 5 < 10
False # 1 < 5 AND 5 < 4 โ False
True # 5 == 5 AND 5 == 5.0
Exercise 4: Swap with XOR
Swap two variables using only XOR (bitwise) without a temp variable.
Solution:
a = 15
b = 27
a = a ^ b
b = a ^ b
a = a ^ b
print(a, b) # 27 15