8. Python Operators
What you will learn
- Assignment, comparison, and logical operators
- Identity and membership operators
- Chained comparisons
- Operator precedence
Assignment operators
Python code
x = 10
x += 5
x *= 2
x -= 4
x //= 3
print(x)
Output
8
Steps: 10, then 15, 30, 26, and 26 // 3 is 8. Python has no ++ or --. Use x += 1.
Comparison operators
| Operator | Meaning |
|---|---|
== |
Equal |
!= |
Not equal |
> < >= <= |
Greater, less, and so on |
Python code
print(5 == 5.0)
print(5 == "5")
print("a" < "b")
print([1, 2] == [1, 2])
Output
True False True True
Unlike JavaScript, Python does not convert "5" to a number when comparing, so 5 == "5" is false.
Chained comparisons
Python lets you write a range test the way you would in maths.
Python code
age = 25
print(18 <= age < 65)
print(0 < age < 18)
Output
True False
Logical operators
Python uses words: and, or, and not.
Python code
age = 20
has_ticket = False
print(age >= 18 and has_ticket)
print(age >= 18 or has_ticket)
print(not has_ticket)
Output
False True True
and and or stop as soon as the answer is known (called short-circuiting), and they return one of their operands, not always True or False:
Python code
name = ""
print(name or "Guest")
print("Sam" and "Ali")
Output
Guest Ali
name or "Guest" is a common way to give a default when a value is empty.
Membership: in and not in
Python code
courses = ["html", "css", "python"]
print("css" in courses)
print("php" not in courses)
print("tho" in "Python")
Output
True True True
Identity: is and is not
== asks “do they have the same value?”. is asks “are they the very same object?”.
Python code
a = [1, 2]
b = [1, 2]
c = a
print(a == b)
print(a is b)
print(a is c)
Output
True False True
Use is only for None (x is None). For everything else, use ==.
The conditional expression
Python code
age = 16
status = "adult" if age >= 18 else "minor"
print(status)
Output
minor
Precedence
From highest to lowest: **, then * / // %, then + -, then comparisons, then not, and, or. Use brackets to make your meaning clear.
Python code
print(2 + 3 * 4)
print((2 + 3) * 4)
print(2 ** 3 ** 2)
print(not True or True)
Output
14 20 512 True
2 ** 3 ** 2 works from the right: 3 ** 2 is 9, then 2 ** 9 is 512.
Common mistakes
- Using
=(assign) instead of==(compare) in a condition. - Using
&&,||, or!from other languages. - Using
isto compare numbers or text. - Writing
x == 1 or 2, which is always true. Writex == 1 or x == 2, orx in (1, 2). - Expecting
x++to work.
Exercise
Given score = 72, print True if it is from 50 to 89 inclusive using a chained comparison. Then print nickname or "Guest" where nickname = "".
Show solution
score = 72
nickname = ""
print(50 <= score <= 89)
print(nickname or "Guest")
Output: True then Guest
Knowledge check
1. Which words does Python use for logic?
and, or, not.
2. What is the difference between == and is?
== compares values. is checks whether two names refer to the same object.
3. Does Python have x++?
No. Use x += 1.
Summary
- Use
==,!=, chained comparisons, and the wordsand,or,not. intests membership, andisis forNone.- Use brackets to make precedence clear.