1/76
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
Branch
A sequence of statements that runs only when its condition is True.
Condition (Boolean expression)
An expression evaluating to True or False; it controls if and while statements.
Relational operators
== != <
Logical operators
and, or, not — combine or invert Booleans. Must be lowercase in Python.
Boolean variable (flag)
A variable holding True/False that tracks a state, e.g. found = False.
Nested branch
An if/else inside another branch's body. Indentation decides what belongs where.
Multi-branch (if-elif-else)
A chain testing conditions in order; at most ONE branch runs.
Dangling else
An else with no braces — it pairs with the NEAREST unmatched if above it.
Short-circuit evaluation
and stops at the first False; or stops at the first True. Later operands never evaluate.
Chained comparison
0 <= x < 100 means (0 <= x) and (x < 100), evaluated left to right.
Conditional expression (ternary)
value_if_true if condition else value_if_false — e.g. y = 0 if x < 100 else x.
Truthiness
Values usable directly as conditions: 0, '', [], None are falsy; almost everything else is truthy.
if statement syntax
if condition: then an indented block. The colon is required.
First-true-branch rule
In if-elif-else, conditions test top-down; the FIRST True branch runs and the rest of the chain is skipped.
else pairing rule
else always pairs with the nearest if above it that doesn't already have an else.
Separate ifs are independent
Two separate if statements can BOTH run. An elif chain runs at most one branch.
No-true-branch rule
If no condition is True and there is no else, nothing runs — this is legal, not an error.
Operator precedence
Parentheses → not → and → or. Comparisons bind tighter than not.
Ternary best use
Simple assignments: y = 0 if x < 100 else x.
Indentation = structure
Python groups statements into blocks purely by indentation. 4 spaces conventional; never mix tabs and spaces.
== vs =
== compares; = assigns. Writing if x = 5: is a SyntaxError.
not a == b trap
Parses as not (a == b). Write a != b or add parentheses.
Never == on floats
0.1 + 0.2 == 0.3 is False — floating-point imprecision. Compare with a tolerance instead.
String comparison
Character by character using Unicode values; case-sensitive. 'Z' (90) < 'a' (97).
True == 1
In Python bool is a subclass of int: True == 1 and False == 0.
and / or return operands
They return the deciding operand, not True/False: 0 or 5 gives 5; 5 and 0 gives 0.
Short-circuit prevents crashes
x = 0 → (x != 0) and (10/x > 1) is False with NO ZeroDivisionError.
is vs ==
== compares values; is compares identity (same object in memory). Use == for values.
in on a dict
Checks KEYS only, not values.
De Morgan's laws
not (a and b) == (not a) or (not b); not (a or b) == (not a) and (not b).
in on a string
Substring test: 'abc' in '123abcd' is True.
Loop
Repeats a body of statements; each pass is one iteration.
while loop
Repeats while its condition is True. Condition is checked BEFORE each iteration.
for loop
Iterates over a sequence or container, visiting each element once.
Loop variable
The variable updated each pass (while) or assigned each element (for).
Infinite loop
The condition never becomes False — e.g. the loop variable is never updated.
Sentinel value
A special input value like -1 or 'quit' that ends an input loop; it is not part of the data.
break
Exits the loop immediately.
continue
Skips the rest of the body and jumps to the next iteration.
Nested loop
A loop inside another loop's body. The inner loop runs fully for EACH outer iteration.
Off-by-one error
Loop runs one time too many or too few — usually a < vs <= or range() boundary mistake.
Accumulator
A variable building a result across iterations, e.g. total += n.
while is pre-test
Condition checked before each pass — zero iterations are possible if it starts False.
range(stop)
0 up to stop-1. range(3) gives 0, 1, 2 — that is 3 iterations.
range(start, stop)
start up to stop-1. range(2, 7) gives 2, 3, 4, 5, 6.
range(start, stop, step)
range(2, 8, 3) gives 2, 5. Negative step counts down: range(7, 0, -2) gives 7, 5, 3, 1.
break scope
break exits only the INNERMOST loop it sits in.
Loop variable after the loop
Keeps its last value once the loop ends.
for over a dict
Iterates over KEYS. Use d[key] to get values.
for over a string
Assigns each CHARACTER in order.
Loop else
for/while can have an else clause — it runs only if the loop ended WITHOUT break.
while vs for
while: iteration count unknown beforehand (input-driven). for: count known or iterating a container.
Reassigning the for-loop variable
Does not change iteration: for n in range(3): n = 99 still visits 0, 1, 2.
while True
Needs a break inside, or it never ends.
!= in a while condition
The counter can skip the target: n=1; while n != 10: n += 2 runs forever (1,3,5,7,9,11…).
Range end is exclusive
range(1, 10) runs 9 times (1 through 9), NOT 10. Classic off-by-one source.
Loop-else runs on NO break
The else runs when the loop finishes normally — the name is misleading.
Variable defined only inside a branch
If the if/loop body never runs, the variable never exists → NameError. Initialize it before.
enumerate()
for i, v in enumerate(lst): gives (index, value) pairs together.
Trace — what prints? x = 7 | if x > 5: print('A') | elif x > 3: print('B') | else: print('C')
A — the FIRST true branch runs; the rest of the chain is skipped.
Trace — what prints? x = 5 | if x > 10: | ··if x > 20: print('big') | else: print('small')
small — the else pairs with the OUTER if (dangling else rule).
Trace — what prints? x = 10 | if x > 5: print('a') | if x > 8: print('b')
a then b — separate ifs are independent, so both run.
Trace — what prints? x = 0 | if x != 0 and 10/x > 1: print('y') | else: print('n')
n — and short-circuits on the False, so no ZeroDivisionError.
Trace — what prints? print(0 or 5) | print(5 and 0)
5 then 0 — and/or return the deciding operand, not True/False.
Trace — what prints? x = 150 | print(0 <= x < 100)
False — chained comparison: (0<=150) and (150<100).
Trace — what prints? x = True | y = False | print(not x or y)
False — not binds tighter: (not True) or False.
Trace — what prints? v = 1 | while v < 6: print(v) | v *= 2
1, 2, 4 — then v becomes 8 and fails the check.
Trace — what prints? for n in range(3): print(n)
0, 1, 2 — range starts at 0 and stops before 3.
Trace — what prints? total = 0 | for n in range(1, 5): total += n | print(total)
10 — sums 1+2+3+4.
Trace — what prints? for n in range(5): | ··if n == 3: break | ··print(n)
0, 1, 2 — break exits the loop immediately.
Trace — what prints? for n in range(5): | ··if n % 2 == 0: continue | ··print(n)
1, 3 — continue skips the even iterations.
Trace — how many lines? for a in range(3): | ··for b in range(3): print(a, b)
9 — the inner loop runs 3 times per outer iteration (3×3).
Trace — what prints? for n in range(3): | ··if n == 9: break | else: print('done')
done — no break ever happened, so the loop-else runs.
Trace — infinite? n = 1 | while n != 10: n += 2
Yes, infinite — n hits 1,3,5,7,9,11… and never exactly 10.
Leap year: 1900? 2000?
1900: no (century year, not divisible by 400). 2000: yes (divisible by 400).
Trace — what prints? print('apple' < 'Banana')
False — 'a' (97) > 'B' (66); string comparison is case-sensitive.
Trace — what happens? if x = 5: print('y')
SyntaxError — = assigns; == compares.