Operators
Python operators include some Java/C# don't have as language keywords: // for floor division, ** for power, and and/or/not instead of symbols.
a, b = 17, 5
print(a + b) # 22
print(a - b) # 12
print(a * b) # 85
print(a / b) # 3.4 — always float in Python 3
print(a // b) # 3 — floor division (truncates)
print(a % b) # 2 — remainder
print(a ** 2) # 289 — power (no Math.pow needed)
# Integer division pitfall from Java/C#: NOT an issue in Python 3
# Python 3: 7 / 2 == 3.5 (use // for integer division)
Logical Operators — Words not Symbols
Python uses English words for logical operators, not symbols like Java's && and ||. This makes conditions read like English.
age = 22
has_matric = True
has_laptop = False
# and, or, not — not &&, ||, !
if age >= 18 and has_matric:
print("Eligible for the programme")
if has_laptop or age > 20:
print("Can attend in-person or remotely")
if not has_laptop:
print("Please request a loaned device")
# Short-circuit: Python stops as soon as outcome is determined
result = False and heavy_computation() # heavy_computation() never called
result = True or heavy_computation() # heavy_computation() never called
Identity and Membership
courses = ["Java", "Python", "SQL"]
# in — membership test (works on lists, strings, dicts, sets, tuples)
print("Python" in courses) # True
print("Kotlin" in courses) # False
print("P" in "Python") # True
# is — identity (same object in memory)
# Use only for None checks, not equality:
x = None
if x is None:
print("x is None")
# is vs ==:
a = [1, 2, 3]
b = [1, 2, 3]
print(a == b) # True — same value
print(a is b) # False — different objects
c = a
print(a is c) # True — same object
Augmented Assignment
score = 50
score += 10 # 60
score -= 5 # 55
score *= 2 # 110
score //= 11 # 10
score **= 2 # 100
print(score) # 100
Practice Task
Your Turn
Given marks 65, 72, 80 as ints: (1) calculate average with / — is it a float? (2) use // to get the integer average, (3) use % to check if the total is divisible by 3, (4) use ** to compute the product of all three marks to the power of 1/3 (the geometric mean), (5) check if all three are above 60 with a single and expression.
Common Mistakes
- / always returns float in Python 3 — use // for integer division.
- and/or not &&/|| — the Java/C# symbols are not logical operators in Python.
- is for identity, == for value equality — never use is for comparing strings or numbers.
- ** is power, not XOR — ^ is bitwise XOR in Python.
Professional Tip
The in operator is one of Python's best features. Checking membership in a list is O(n), in a set is O(1). When performance matters for membership tests, use a set.
Mini Quiz
What does the // operator do in Python?