← Back to Core Python
Lesson 8 · Operators and Control Flow

Operators

Arithmetic, comparison, logical, identity, membership, and bitwise.

Beginner35 min

What you will be able to do

  • Name the seven operator families and say what each one is for
  • Use the augmented assignment operators, and know what they do to mutable objects
  • Explain the difference between == and is, and why is sometimes appears to work
  • Combine conditions with and, or, not - and use chained comparisons
  • Explain short-circuit evaluation and the None guard it makes possible
  • Recognise bitwise operators and the permission-flag pattern they appear in

The idea, in plain English

An operator tells Python to do something with one or two values. In 18 <= age, the values (18 and age) are the operands, <= is the operator, and the whole thing is an expression that gives back a result - here True or False.

Python has seven families of operators. Arithmetic was Lesson 4. This lesson covers the rest - the ones you need for decisions: assignment shortcuts, comparison, logic, identity, membership, and briefly bitwise. It comes before if statements because a condition is just an expression that gives True or False. Once you can read is_active and role in ("admin", "trainer"), the if around it is the easy part.

Three things here surprise almost everyone: is is not ==, and/or do not always give back True or False, and Python stops working out an expression the moment it knows the answer. All three matter in real code. Every output below comes from running the code in Python 3.14.

Worked example: Deciding whether a learner can enrol, from four separate conditions.

An everyday example: the cinema entry gate

A guard at a cinema gate checks every person. Each check is one operator family:

  • Adds one to the count of people let in. (Assignment: count += 1.)
  • Is the ticket’s show time the same as now? Is the age 18 or over? (Comparison: ==, >=.)
  • Has a ticket AND an ID? Is a member OR has paid? (Logic: and, or, not.)
  • Is this the SAME person who bought the ticket - or just someone with the same name? (Identity vs equality: is vs ==.)
  • Is the seat number in the list of free seats? (Membership: in.)
  • No ticket? The guard stops right there and does not even ask for an ID. (Short-circuiting.)
  • A pass card with holes punched for each extra - popcorn, parking, 3D glasses. (Bitwise flags.)
The story -> the operators
Counting people inAugmented assignment.
Right show? Old enough?Comparison - always True or False.
Ticket and IDLogical operators.
The same person, or the same name?is (same object) vs == (same value).
Seat in the free list?Membership.
No ticket - stop askingShort-circuit: the second check never runs.
Holes punched in a passBitwise flags.

Augmented assignment: +=, -=, and the rest

  • count += 5 is a short way to write count = count + 5. Every arithmetic operator has one: -=, *=, /=, //=, %=, **=.
  • /= always gives a float, just like / does (Lesson 4).
  • On a list there is a difference. a += [3] changes the SAME list, so every other name for it sees the change. a = a + [3] builds a NEW list, and other names keep the old one.
  • For numbers and strings there is no difference - they cannot be changed in place (Lesson 3).
Augmented assignment
count = 10 count += 5 # count = count + 5 count -= 3 # 12 count *= 2 # 24 count //= 5 # 4 count **= 2 # 16 print(count) # 16 value = 100 value /= 4 print(value) # 25.0 <- /= always gives a float # On a list, += changes the list in place; + builds a new one a = [1, 2] b = a a += [3] # same object, so b sees it print(b) # [1, 2, 3] a = [1, 2] b = a a = a + [3] # new object, so b does not print(b) # [1, 2]

Comparison, and chained comparisons

  • == equal, != not equal, < > <= >= - every comparison gives back True or False.
  • Strings compare character by character, and case matters: "Python" == "python" is False.
  • Comparisons can be chained, the way you write them in maths: 18 <= age <= 60 means 18 <= age and age <= 60.
  • In a chain the middle value is worked out only once.
  • A number and a string are never equal - 10 == "10" is False. Asking which is SMALLER is an error: 10 < "20" raises TypeError.
Comparison, including chains
print(34 == 34, 34 != 30) # True True print("apple" < "banana") # True - character by character print("Python" == "python") # False - case matters # Chained, which reads the way the maths does age = 25 print(18 <= age <= 60) # True print(0 <= 85 <= 100) # True # Equality across types is False, ordering is an error print(10 == "10") # False print(10 < "20") # TypeError: '<' not supported between instances of 'int' and 'str'

== compares values, is compares identity

  • == asks: "do these two have the same VALUE?"
  • is asks: "are these two names pointing at the SAME OBJECT?"
  • Two lists with the same contents are == but not is - there are two lists. (The names-and-objects model from Lesson 2.)
  • The danger: is sometimes SEEMS to work. Python reuses some small numbers and short strings, so x is y can be True for 256 and False for 257 - and it even changes between a script and the REPL.
  • Code that passes your test and fails later is worse than code that always fails.
  • So use is for one job only: checking for None, True or False. Everything else uses ==.
== against is
a = [1, 2, 3] b = [1, 2, 3] c = a print(a == b) # True same contents print(a is b) # False two separate list objects print(a is c) # True one object, two names # The trap: 'is' can look correct for numbers x = 256; y = 256 print(x is y) # True - Python keeps one shared 256 x = 257; y = 257 print(x is y) # True in a script, False typed into the REPL - that is the point # The only identity checks worth writing result = None print(result is None) # True print(result is not None) # False
Which one to reach for
x == ySame value. The default choice for comparing anything.
x is NoneThe correct None check. There is only one None, so "same object" is exactly the question.
x is not NoneThe opposite. Note the word order - not `not x is None`.
x is TrueAlmost never what you want. Write `if x:` instead.
a is b on listsTrue only when both names point at ONE list. Two equal lists are == but not is.
x is 256A trap. It may be True by accident, and Python warns about it.

Watch out: Write `is` against a number or string and Python warns you: SyntaxWarning: "is" with 'int' literal. Did you mean "=="? - you did.

and / or give back one of their operands

This surprises everyone once:

  • "Python" and "Java" gives "Java" - not True.
  • "" or "Python" gives "Python" - not True.
  • The rule for a and b: if a is falsy, the answer is a; otherwise it is b.
  • The rule for a or b: if a is truthy, the answer is a; otherwise it is b.
  • With real True/False values you get True/False back - which is why nobody notices for a while.
  • not is different: it always gives back a real True or False.
  • This gives a handy idiom: name or "Anonymous" uses the default when name is empty.
  • And a bug: 0, "" and [] are falsy too. port = configured or 8080 quietly replaces a deliberate port of 0. When only None should be replaced, write the check out in full.
Logic that gives back operands
print("Python" and "Java") # Java - not True print("" or "Python") # Python - the default-value idiom print(not "Python") # False - not does give a boolean name = "" print(name or "Anonymous") # Anonymous # ...and the bug hiding in that idiom configured_port = 0 print(configured_port or 8080) # 8080 - overrode a deliberate 0 port = configured_port if configured_port is not None else 8080 print(port) # 0 - correct
What these expressions actually give back
"a" and "b""b" - the first is truthy, so the second is the answer.
"" and "b""" - the first is falsy, so it is the answer. "b" is never even looked at.
"" or "b""b" - the first is falsy, so the second is the answer. The default-value idiom.
"a" or "b""a" - the first is truthy, so "b" is never looked at.
not "a"False - not always gives a real bool.
0 or 80808080 - and that is the bug, if 0 was set on purpose.

Short-circuiting, and the guard it makes possible

  • Python stops as soon as the answer is certain.
  • In a and b: if a is falsy, the answer is already known - b is never run.
  • In a or b: if a is truthy, the same - b is never run.
  • This is not just a speed trick. Code DEPENDS on it: if user is not None and user.is_active is safe only because the second half never runs when user is None.
  • Swap the two halves and it crashes with AttributeError.
  • The same shape protects against empty lists (items and items[0] > 10) and dividing by zero (divisor != 0 and 100 / divisor > 1).
Short-circuiting as a guard
user = None # Safe: the second half never runs when the first is False if user is not None and user.is_active: print("active") else: print("no user") # no user items = [] if items and items[0] > 10: # items[0] would raise on an empty list print("big") divisor = 0 if divisor != 0 and 100 / divisor > 1: # never divides by zero print("ok")

Tip: Put the safe check first. The condition that PREVENTS the error must come before the one that would CAUSE it.

Membership: in and not in

  • x in collection asks "is x inside this?" - and gives True or False. not in is the opposite.
  • On a string, in looks for a piece of text: "Py" in "Python" is True.
  • On a dictionary, in looks at the KEYS only. To search the values, use d.values().
  • role in ("admin", "trainer") is cleaner than role == "admin" or role == "trainer" - and stays right when you add a third role.
Membership
skills = ["Python", "React", "Node.js"] print("Python" in skills) # True print("Java" not in skills) # True print("Py" in "Python") # True - a piece of text, for strings # On a dict, 'in' looks at the keys user = {"name": "Chandu", "role": "Developer"} print("name" in user) # True print("Chandu" in user) # False - that is a value print("Chandu" in user.values()) # True # The clean way to test several options role = "trainer" print(role in ("admin", "trainer")) # True - better than role == "admin" or role == "trainer"

Bitwise operators, and where you meet them

  • These work on the binary digits (bits) of whole numbers. You will not need them often.
  • Where they DO appear: permission systems, file modes, and anywhere a set of on/off flags is packed into one number.
  • The pattern: give each flag a power of two - READ = 1, WRITE = 2, DELETE = 4.
  • Combine flags with |: READ | WRITE is 3, meaning read and write.
  • Test one flag with &: permissions & WRITE is non-zero exactly when write is on.
  • The odd one: ~5 is -6. For Python’s whole numbers, ~x is always -x - 1.
Bitwise flags
READ, WRITE, DELETE = 1, 2, 4 # powers of two permissions = READ | WRITE # combine print(permissions) # 3 -> 0b011 print(bool(permissions & READ)) # True - granted print(bool(permissions & DELETE)) # False - not granted permissions |= DELETE # grant permissions &= ~WRITE # revoke print(f"{permissions:03b}") # 101 -> read and delete
On 5 (0101) and 3 (0011)
5 & 31 - AND keeps bits set in both. 0101 & 0011 = 0001.
5 | 37 - OR keeps bits set in either. This is how flags are combined.
5 ^ 36 - XOR keeps bits set in exactly one.
~5-6 - flips every bit. Always equals -x - 1.
5 << 110 - shift left one place: doubles a positive number.
20 >> 25 - shift right two places: floor division by 4.

Watch out: & is not and. & binds tighter than ==, so `a == 1 & b == 2` means `a == (1 & b) == 2`. It also never short-circuits. Use and for logic.

Putting it together: can this learner enrol?

Four facts about a learner, two decisions:

  • Give each condition a name: is_adult = 18 <= age.
  • Join them with and - every one must be true.
  • Use in with a tuple for "admin or trainer".
  • Now each line says one thing - so when the answer is False, the failing condition is easy to find.
Can this learner enrol?
is_logged_in = True account_active = True age = 25 course_available = True role = "trainer" # Each line says one thing, so the failing one is obvious is_adult = 18 <= age can_enrol = is_logged_in and account_active and is_adult and course_available can_edit = is_logged_in and account_active and role in ("admin", "trainer") print(f"Can enrol: {can_enrol}") # True print(f"Can edit: {can_edit}") # True

The seven families

Arithmetic

+ - * / // % ** - covered in lesson 4. Remember / always returns a float.

10 // 3   # 3
Assignment

= and the augmented forms += -= *= /= //= %= **=, which update in place.

count += 1
Comparison

== != > < >= <= - always return a bool, and can be chained.

18 <= age <= 60
Logical

and or not - combine conditions, short-circuit, and return an operand.

a and b
Identity

is, is not - same object, not same value. For None, True, False.

x is None
Membership

in, not in - is this inside that. Keys for a dict, characters for a string.

"Py" in name
Bitwise

& | ^ ~ << >> - operate on the bits of an integer. Flags and low-level work.

flags & READ

Precedence, highest first

You do not need to memorise this. You need to know that comparison binds tighter than not, which binds tighter than and, which binds tighter than or - and to add parentheses everywhere else.

( )

Parentheses. The only tool you need to override everything below.

**

Exponentiation. Right-associative, and tighter than unary minus.

+x -x ~x

Unary plus, minus, and bitwise NOT.

* / // %

Multiplication and the division family.

+ -

Addition and subtraction.

<< >>

Bit shifts - lower than arithmetic, higher than comparison.

& ^ |

Bitwise AND, XOR, OR - in that order, and all tighter than comparison.

in is < <= > >= != ==

Comparison, membership and identity, all at the same level.

not

Logical NOT - lower than comparison, so `not a == b` means `not (a == b)`.

and

Logical AND.

or

Logical OR - the loosest of all, which is why it usually needs no parentheses.

Try it yourself

The code does not change. Swap the content string and the program does something else entirely.

and returns an operand

“print("a" and "b", "" or "b", not "a")”

The interning trap

“x=256;y=256;print(x is y); x=257;y=257;print(x is y)”

+= on a shared list

“a=[1];b=a;a+=[2];print(b); a=[1];b=a;a=a+[2];print(b)”

& is not and

“print(True & False, 1 & 2, bool(1 and 2))”

What usually goes wrong

Writing = where you meant ==

Assignment inside an if is a SyntaxError in Python, which is a kindness - in C it silently assigns and the condition is almost always true.

✗ if age = 18:
✓ if age == 18:
Using is to compare values

It asks whether two names point at one object, not whether the values match. It sometimes appears to work because CPython caches small integers and short strings, which makes it worse - the bug survives testing.

✗ if username is "Chandu":
✓ if username == "Chandu":
Using == to check for None

It usually works, but None is a singleton so identity is the precise question, and a class can override __eq__ to make == lie. `is None` cannot be overridden.

✗ if result == None:
✓ if result is None:
Relying on `or` for defaults when 0 is valid

Every falsy value takes the fallback, not just None. A configured port of 0, a quantity of 0, or an empty string that was deliberately set all get silently replaced.

✗ port = configured or 8080
✓ port = 8080 if configured is None else configured
Using & or | for logic

They are bitwise operators. They do not short-circuit, and they bind tighter than == - so `a == 1 & b == 2` is parsed as `a == (1 & b) == 2`, which is not remotely what you wrote.

✗ if is_active & is_admin:
✓ if is_active and is_admin:
Repeating the variable instead of using in

A tuple membership test says the intent directly and does not repeat the name, so it cannot go wrong when you add a third option.

✗ if role == "admin" or role == "trainer":
✓ if role in ("admin", "trainer"):
Mixing and and or without parentheses

and binds tighter than or, so the grouping may not be what you meant. Even where it is, the parentheses tell the next reader you knew.

✗ if age >= 18 and role == "admin" or role == "trainer":
✓ if age >= 18 and role in ("admin", "trainer"):

Best practices

  • Use == for values and reserve is for None, True, and False.
  • Write `x is None`, never `x == None`.
  • Use chained comparisons for ranges: 0 <= score <= 100.
  • Use `in` with a tuple rather than a chain of or-ed equality tests.
  • Put the guarding condition first so short-circuiting protects the one after it.
  • Give complex conditions a name - can_enrol = ... - so the if statement reads as a sentence.
  • Use and and or for logic, & and | only when you genuinely mean bits.
  • Add parentheses whenever and and or appear in the same expression.

Practice

Write these yourself before opening anything. Getting them wrong first is most of how this sticks.

1.

Given price = 5000 and discount = 0.10, work out the discount amount and the final price.

Show solution
price = 5000 discount = 0.10 discount_amount = price * discount final_price = price - discount_amount print(f"Discount: {discount_amount:,.2f}") # 500.00 print(f"Final: {final_price:,.2f}") # 4,500.00
2.

Check whether age = 25 falls between 18 and 60 inclusive, using a chained comparison.

Show solution
age = 25 print(18 <= age <= 60) # True
3.

Given role = "trainer", allow access if the role is either admin or trainer - without repeating the variable.

Show hint

Membership against a tuple.

Show solution
role = "trainer" print(role in ("admin", "trainer")) # True
4.

Given result = None, check whether it is None in the idiomatic way.

Show solution
result = None if result is None: print("No result") # No result
5.

Create two separate lists with identical contents. Show that they are equal but not identical, then make a third name that is identical to the first.

Show solution
a = [1, 2, 3] b = [1, 2, 3] c = a print(a == b) # True - same contents print(a is b) # False - two objects print(a is c) # True - one object, two names
6.

Given user = None, write a condition that checks user.is_active without raising an AttributeError.

Show hint

Short-circuiting does the work - the order of the two halves is the whole answer.

Show solution
user = None if user is not None and user.is_active: print("active") else: print("no active user") # no active user
7.

Using READ = 1, WRITE = 2, DELETE = 4, grant read and write, then check whether delete is granted.

Show solution
READ, WRITE, DELETE = 1, 2, 4 permissions = READ | WRITE print(permissions) # 3 print(bool(permissions & READ)) # True print(bool(permissions & DELETE)) # False
Coding challenge

Access control for the platform

Work out what a user is allowed to do from four separate facts about them. Then run every combination below and check each answer is what you intended.

It should
  • Decide can_view from: logged in, account active, and the course published
  • Decide can_edit from: logged in, account active, and a role of admin or trainer
  • Use `in` with a tuple for the role test rather than or-ed comparisons
  • Name each condition so the final expression reads as a sentence
  • Run the five cases below and print a table of the results
Start here
cases = [ ("admin", True, True, True), ("trainer", True, True, True), ("student", True, True, True), ("admin", True, False, True), # inactive account ("trainer", False, True, True), # logged out ] for role, logged_in, active, published in cases: ...
Show one solution
One solution
EDITOR_ROLES = ("admin", "trainer") cases = [ ("admin", True, True, True), ("trainer", True, True, True), ("student", True, True, True), ("admin", True, False, True), # inactive account ("trainer", False, True, True), # logged out ] print(f"{'role':<9}{'in':<6}{'active':<8}{'pub':<6}{'view':<7}{'edit'}") print("-" * 42) for role, logged_in, active, published in cases: # One named condition per rule - the failing one is then obvious. signed_in_and_ok = logged_in and active can_view = signed_in_and_ok and published can_edit = signed_in_and_ok and role in EDITOR_ROLES print(f"{role:<9}{logged_in!s:<6}{active!s:<8}{published!s:<6}" f"{can_view!s:<7}{can_edit!s}") # role in active pub view edit # ------------------------------------------ # admin True True True True True # trainer True True True True True # student True True True True False # admin True False True False False # trainer False True True False False

Key points

  • An operator acts on operands and produces a value; a condition is just an expression that evaluates to a bool.
  • Augmented assignment (+=, -=, ...) updates a name in place; on a list, += mutates while + builds a new list.
  • == compares values, is compares object identity. They answer different questions.
  • Use is only for None, True, and False. Small-integer caching makes it appear to work elsewhere.
  • Comparisons can be chained: 18 <= age <= 60, with the middle evaluated once.
  • and and or return one of their operands, not necessarily a boolean. not always returns a boolean.
  • `x or default` falls back on any falsy value, not just None - watch out for 0 and "".
  • Short-circuiting means the second operand may never be evaluated, which is what makes None guards safe.
  • `in` tests membership; on a dict it looks at the keys.
  • Bitwise operators work on bits and are the basis of permission flags. & is not and.
  • Precedence: comparison, then not, then and, then or. Parenthesise anything else.

Quick check before you move on

What does print(10 > 5 and 3 < 2) give, and what about `or` instead of `and`?
False with and, because the second condition fails. True with or, because the first succeeds - and with or the second is never even evaluated.
For a = [1, 2] and b = [1, 2], what do a == b and a is b give?
True and False. The contents are equal, but they are two separate list objects.
What does "" or "Python" evaluate to?
"Python" - the string itself, not True. or returns the first truthy operand, which is what makes it usable as a default.
Why does `if user is not None and user.is_active:` not crash when user is None?
Short-circuiting. The first half is False, so the result is already known and the second half is never evaluated.

Interview questions

Why does `256 is 256` return True but `257 is 257` may not?

CPython pre-allocates and caches integer objects from -5 to 256, so every reference to 256 is the same object. 257 is created fresh each time - though the peephole optimiser may still fold two literals in the same code block, which is why the answer differs between the REPL and a script. That inconsistency is exactly the argument for never using `is` on values.

What do `and` and `or` actually return?

One of their operands. `a and b` returns a when a is falsy, otherwise b; `a or b` returns a when a is truthy, otherwise b. The truthiness of the result matches the boolean logic, which is why it goes unnoticed, and it enables `x = value or default`. `not` is the exception - it always returns a real bool.

How does `a < b < c` differ from `(a < b) < c`?

Completely. The chained form means `a < b and b < c`, with b evaluated once. The parenthesised form compares a boolean against c - since True is 1, `1 < 2 < 3` is True but `(1 < 2) < 3` is `True < 3`, which is also True for a different and accidental reason.

Why is short-circuiting part of the language rather than an optimisation?

Because programs depend on it for correctness, not speed. `user is not None and user.is_active` is only safe if the second operand is genuinely skipped. A language that evaluated both would make that idiom crash, so the behaviour has to be guaranteed rather than best-effort.

When would you use bitwise operators in application code?

Packing a set of boolean flags into one integer - file permissions, feature toggles, protocol headers - where each flag is a power of two, | grants and & tests. Also for hashing, fast multiply or divide by powers of two, and masking in binary formats. In ordinary business logic, a set or a dataclass of booleans is clearer.

What is the difference between `x += [1]` and `x = x + [1]` for a list?

+= calls __iadd__ and extends the list in place, so every other name pointing at that list sees the change. x + [1] builds a new list and rebinds x, leaving the original untouched. For immutable types like int and str there is no difference, because there is no in-place option.

Quiz

  1. 1.

    What is the difference between = and ==?

  2. 2.

    What is the difference between == and is?

  3. 3.

    Why should you write `value is None` rather than `value == None`?

  4. 4.

    What is short-circuit evaluation?

  5. 5.

    What does `"" or "Python"` return, and why does it matter?

  6. 6.

    Why should you not use & in place of and?

Comments

Sign in to leave a comment. Your name and photo come from Google; nothing else is shared.

Loading comments...