Python Operators & Booleans
Operators are the symbols that do the work in an expression: they add numbers, compare values and combine conditions. This lesson covers all seven groups of Python operators and the booleans they produce.
- Arithmetic and assignment operators
- Comparison operators and chaining
- and, or, not with short-circuiting
- is vs == and the in operator
Arithmetic operators
a, b = 14, 4print(a + b, a - b, a * b)print(a / b, a // b, a % b, a ** 2)18 10 56 3.5 3 2 196
+ and * also work on strings and lists:
print("ab" + "cd", "-" * 10)print([1, 2] + [3], [0] * 3)abcd ---------- [1, 2, 3] [0, 0, 0]
The Numbers & Math lesson explains //, % and rounding in detail.
Assignment operators
Augmented assignment updates a variable using its current value.
| Operator | Same as |
|---|---|
x += 3 | x = x + 3 |
x -= 3 | x = x - 3 |
x *= 3 | x = x * 3 |
x /= 3 | x = x / 3 |
x //= 3 | x = x // 3 |
x %= 3 | x = x % 3 |
x **= 3 | x = x ** 3 |
balance = 1000balance -= 250 # withdrawalbalance *= 1.05 # 5% interestprint(balance)787.5
Comparison operators
Comparisons return a boolean: True or False.
| Operator | Meaning |
|---|---|
== | equal |
!= | not equal |
> / < | greater than / less than |
>= / <= | greater or equal / less or equal |
score = 72print(score == 72, score != 72)print(score > 50, score <= 60)print("apple" < "banana") # strings compare alphabeticallyprint(10 == 10.0) # int and float with the same value are equalTrue False True False True True
Python lets you chain comparisons, just like in maths:
age = 25print(18 <= age < 60)True
Logical operators: and, or, not
| Expression | True when |
|---|---|
a and b | both are true |
a or b | at least one is true |
not a | a is false |
has_ticket = Trueis_adult = Falseprint(has_ticket and is_adult)print(has_ticket or is_adult)print(not is_adult)False True True
Short-circuiting
and stops at the first falsy value and or stops at the first truthy value. They return that value itself, not always True/False. This gives two handy idioms:
name = ""display = name or "Guest" # default when name is emptyprint(display) items = []print(len(items) > 0 and items[0]) # items[0] is never evaluatedGuest False
Booleans and truthiness
True and False are the two boolean values, and bool is a subclass of int. In an if or while, any value is treated as true or false:
print(True + True, True * 10)for value in [0, 3, "", "text", [], [1], None]: print(repr(value), "->", "truthy" if value else "falsy")2 10 0 -> falsy 3 -> truthy '' -> falsy 'text' -> truthy [] -> falsy [1] -> truthy None -> falsy
Identity operators: is, is not
== asks “do these have the same value?” is asks “are these the very same object?”
a = [1, 2]b = [1, 2]c = aprint(a == b, a is b)print(a is c)True False True
Use is only to compare with None, True or False: if result is None:. For everything else use ==.
Membership operators: in, not in
roles = ["admin", "tester", "viewer"]print("tester" in roles)print("guest" not in roles)print("py" in "python") # substring checkprint("id" in {"id": 7, "name": "x"}) # checks dict keysTrue True True True
Bitwise operators
Bitwise operators work on the binary digits of integers. You will meet them in permissions, flags and low-level code.
a, b = 6, 3 # 110 and 011 in binaryprint(a & b, a | b, a ^ b)print(~a, a << 1, a >> 1)2 7 5 -7 12 3
Operator precedence
From highest to lowest (operators on the same row are evaluated left to right, except **):
| Precedence | Operators |
|---|---|
| 1 | () parentheses |
| 2 | ** |
| 3 | +x, -x, ~x |
| 4 | *, /, //, % |
| 5 | +, - |
| 6 | bitwise: << and >>, then &, then ^, then bitwise OR |
| 7 | comparisons, in, not in, is, is not |
| 8 | not |
| 9 | and |
| 10 | or |
print(True or False and False) # and binds tighter: True or (False and False)print(not 1 == 2) # not (1 == 2)True True
Check your understanding
Predict the output
x = 5print(x > 3 and x < 10, x > 3 or 1 / 0, 0 or "" or "last")True True last
The first is True and True. In the second, or stops at the truthy x > 3, so 1 / 0 never runs. The third returns the first truthy value, and since none of the earlier ones are, it returns the last value.
Quick quiz
1. What does 10 != 10.0 return?
10 == 10.0 is True, so != gives False.
2. What does "" or "default" return?
or returns the first truthy value.
3. Which checks whether x is None?
is None is the recommended, fastest and safest check.
4. What does 1 < 3 < 2 evaluate to?
It means 1 < 3 and 3 < 2, and 3 < 2 is False.
Exercise: leap year check
A year is a leap year if it is divisible by 4, except years divisible by 100, unless they are also divisible by 400. Print each year with True or False.
1900, 2000, 2024, 2026
1900 False 2000 True 2024 True 2026 False
Hint 1
Combine % with and and or.
Hint 2
(year % 4 == 0 and year % 100 != 0) or year % 400 == 0
for year in [1900, 2000, 2024, 2026]: is_leap = (year % 4 == 0 and year % 100 != 0) or year % 400 == 0 print(year, is_leap)1900 False 2000 True 2024 True 2026 False
1900 is divisible by 100 but not 400, so it is not a leap year. 2000 passes the 400 rule. The parentheses make the grouping obvious even though and already binds tighter than or.
Program Practice
40 Python Basic Programs
Every program has a step-by-step explanation, colour-coded code and its real output.
Python operators FAQ
What is the difference between == and is in Python?
== compares values; is compares identity (whether two names refer to the same object). Two separate lists with the same items are == but not is.
Does Python have && and || operators?
No. Python uses the words and, or and not.
Does Python have ++ and — operators?
No. Use x += 1 and x -= 1.
What does the % operator do in Python?
It returns the remainder of a division (17 % 5 is 2). It is also used in old-style string formatting such as "%d items" % 3.