Operators
Operators are symbols and keywords that perform an action on values, such as adding numbers, comparing scores or combining conditions. You've already met some; this page gathers all the main groups in one place so you can read and write any expression with confidence.
Arithmetic operators
These do maths. They're covered in depth on Numbers & Arithmetic.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ - * | Add, subtract, multiply | 6 * 7 | 42 |
/ | Division | 7 / 2 | 3.5 |
// | Floor division | 7 // 2 | 3 |
% | Remainder | 7 % 2 | 1 |
** | Power | 7 ** 2 | 49 |
+ and * also work on strings: "ab" + "cd" joins them and "ab" * 3 repeats them.
Assignment operators
= assigns a value. The compound assignment operators update a variable using its current value, so x += 5 is short for x = x + 5.
x = 10 x += 5 # 15 x *= 2 # 30 x //= 4 # 7 x %= 4 # 3 x **= 2 # 9 print(x) wallet = 1000 wallet -= 350 # bought lunch and fare print(wallet)
There are compound versions for every arithmetic operator: +=, -=, *=, /=, //=, %= and **=. Python has no ++ or -- operators; write count += 1 instead.
Comparison operators
Comparisons ask a question and always answer with True or False.
| Operator | Meaning | Example | Result |
|---|---|---|---|
== | Equal to | 5 == 5 | True |
!= | Not equal to | 5 != 3 | True |
> | Greater than | 5 > 8 | False |
< | Less than | 5 < 8 | True |
>= | Greater than or equal to | 5 >= 5 | True |
<= | Less than or equal to | 8 <= 5 | False |
age = 20
print(18 <= age <= 25) # chained: is age between 18 and 25?
print("apple" < "banana") # strings compare alphabetically
print("Zebra" < "apple") # capitals come before lowercase
print(0.1 + 0.2 == 0.3) # float rounding strikes again
Python lets you chain comparisons as you would in maths: 18 <= age <= 25 means 18 <= age and age <= 25. Strings compare character by character using their Unicode codes, and every capital letter has a smaller code than every lowercase letter, which is why "Zebra" < "apple" is True.
Confusing = with ==. One equals sign assigns; two compare. Writing if score = 100: is a SyntaxError. And never test floats for exact equality: compare round(a, 2) == round(b, 2) or use math.isclose(a, b).
Logical operators
and, or and not combine or flip Boolean values.
| Operator | Result is True when… | Example | Result |
|---|---|---|---|
and | both sides are true | True and False | False |
or | at least one side is true | True or False | True |
not | the value is false | not True | False |
age = 19 has_ticket = True is_banned = False print(age >= 18 and has_ticket) print(age < 18 or has_ticket) print(not is_banned)
Short-circuiting
Python stops evaluating as soon as it knows the answer. In a and b, if a is false, b is never checked; in a or b, if a is true, b is skipped. In fact, and and or return one of their operands rather than strictly True or False, which gives a neat way to supply a default:
name = "" display_name = name or "Guest" print(display_name) count = 0 print(count != 0 and 10 / count > 1) # safe: the division never runs
Identity operators: is and is not
== asks "do these have the same value?", while is asks "are these the very same object in memory?". Use is mainly to check for None.
phone = None print(phone is None) print(phone is not None) a = [1, 2] # a list: an ordered collection b = [1, 2] print(a == b) # same contents print(a is b) # but two separate lists
Membership operators: in and not in
in checks whether a value appears inside a string or collection.
print("a" in "Kampala")
print("z" not in "Kampala")
print("beans" in ["maize", "beans", "rice"])
Operator precedence
When an expression mixes operators, Python applies them in this order, from highest to lowest priority:
- Brackets
( ) - Power
** - Unary minus
-x *,/,//,%+,-- Comparisons,
in,is notandor
print(True or False and False) # and runs first print(not 5 > 3) # comparison runs before not print(10 - 4 > 2 * 3) # 6 > 6
Python also has bitwise operators (&, |, ^, ~, <<, >>) that work on the binary digits of integers. You'll see &, | and ^ again with sets. When a long expression is hard to read, add brackets even if they aren't strictly needed.
Key points
- Arithmetic operators do maths; compound assignments like
+=update a variable in place. - Comparisons return
True/Falseand can be chained, as in0 < x < 10. and,orandnotcombine conditions and short-circuit.- Use
isforNonechecks and==for comparing values;intests membership. - Follow precedence rules, and use brackets to make complex expressions clear.
Tip: use the ← and → keys to move between pages.
