Python Weeks 1–5 — compressed notes
887 words
4 min read
2026-08-02T00:00:00.000Z
Visual companion
Python
Type and operator map
Python Week 1: the first filter for runtime behavior
View
Revision summary
What this note is really saying
Short form
A compact recall sheet for values, strings, decisions, loops, and patterns. # Python Weeks 1–5 — compressed notes > **Use before the exam, then close it.** This is preparation material, not an exam aid.

Python Weeks 1–5 — compressed notes
Use before the exam, then close it. This is preparation material, not an exam aid. The official May 2026 Python policy prohibits LLM use in an OPPE or programming assignment.
1. Values, names, input, and output
| Idea | Remember | Tiny example |
|---|---|---|
| Variable | A name points to a value; = assigns, it does not compare. | total = 0 |
input() | Always returns str. Convert if arithmetic follows. | age = int(input()) |
print() | Displays; it does not return a useful result. | print("sum:", a + b) |
| f-string | Put expressions inside {}. | f"{name} has {n} points" |
| Type conversion | Convert deliberately at the boundary. | float("3.5") |
Arithmetic order: parentheses →
** → * / // % → + - → comparisons → not → and → or.pythonn = int(input()) print(f"next = {n + 1}")
Operators you should trace without running:
python17 // 5 # 3: quotient rounded down 17 % 5 # 2: remainder 17 / 5 # 3.4: ordinary division is float 2 ** 3 # 8
For a negative dividend, do not guess: Python's
// rounds down, so -7 // 3 is -3 and -7 % 3 is 2.2. Strings: indexing, slicing, and methods
Strings are ordered and immutable: you can make a new string, but cannot change one character in place.
pythons = "python" s[0] # 'p' s[-1] # 'n' s[1:4] # 'yth' (start included, stop excluded) s[:3] # 'pyt' s[::2] # 'pto'
| Pattern | Meaning |
|---|---|
len(s) | number of characters |
x in s | membership test, result is Boolean |
s.lower() | returns a new lowercase string |
s.strip() | removes surrounding whitespace |
s.count("a") | counts non-overlapping occurrences |
s.replace("a", "b") | returns changed copy |
Trace habit: Write index labels above the string, including negative indices, before slicing. For a slice, circle the start and stop boundary—never include the stop character.
3. Boolean logic and decisions
Comparisons produce
True or False: ==, !=, <, <=, >, >=.pythonif mark >= 90: band = "A" elif mark >= 75: band = "B" else: band = "C"
elif branches are tested top-to-bottom; the first true branch wins. Put stricter / higher thresholds first.| Goal | Correct form | Classic wrong form |
|---|---|---|
| Between 10 and 20 | 10 <= x <= 20 | x >= 10 or x <= 20 |
| Not either condition | not (a or b) | not a or b |
| One of two paths | if ...: ... else: ... | two independent ifs when paths must exclude each other |
Use truth tables for compound expressions. Do not rely on the English reading of
and/or under pressure.4. while: state, condition, update
Every useful
while loop has:- an initial state,
- a condition that says continue while true, and
- an update that eventually makes it false.
pythonn = 482 digit_sum = 0 while n > 0: digit_sum = digit_sum + (n % 10) n = n // 10 print(digit_sum)
For every trace, make a table with one row per iteration:
n before body | digit | accumulator | n after update.Common loop roles:
| Name | Starts at | Changes by | Purpose |
|---|---|---|---|
| Counter | 0 | + 1 | count occurrences |
| Accumulator | 0 or 1 | add/multiply | build sum/product |
| Flag | False | set True once found | remember a condition |
| Sentinel loop | input value | new input | stop on special value |
5. for, range, nested loops, and control flow
pythonfor i in range(2, 10, 3): print(i) # 2, 5, 8
range(start, stop, step) excludes stop. range(n) is 0 through n - 1.pythonfor row in range(3): for col in range(4): print("*", end="") print()
The outer loop controls rows; the inner loop controls items in one row;
print() with no end makes the newline.| Statement | Effect |
|---|---|
break | immediately leaves the nearest loop |
continue | skips to the next iteration of the nearest loop |
pass | does nothing; placeholder only |
Pre-submit trace checklist:
- What are the types after every
input()? - Which condition is checked first?
- Does every
whilepath update its progress variable? - Does
rangeinclude the intended last value? - Is an accumulator reset outside—not inside—the loop?
- Does your
printuseend=exactly where layout requires it?
25-minute last-pass routine
- 5 min: write the five section headings from memory and one trap under each.
- 10 min: hand-trace one string slice, one decision ladder, one digit loop, and one nested-loop pattern.
- 8 min: write two tiny original programs, run them locally, and test boundaries.
- 2 min: stop. Hydrate, reset your desk, and do not bring notes or AI into the OPPE.