Quiz 2
Registry Synced

Python Weeks 1–5 — compressed notes

887 words
4 min read
2026-08-02

Reading compass

Now · 1. Values, names, input, and output

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

IdeaRememberTiny example
VariableA 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-stringPut expressions inside {}.f"{name} has {n} points"
Type conversionConvert deliberately at the boundary.float("3.5")
Arithmetic order: parentheses → *** / // %+ - → comparisons → notandor.
python
n = int(input())
print(f"next = {n + 1}")
Operators you should trace without running:
python
17 // 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.
python
s = "python"
s[0]      # 'p'
s[-1]     # 'n'
s[1:4]    # 'yth'  (start included, stop excluded)
s[:3]     # 'pyt'
s[::2]    # 'pto'
PatternMeaning
len(s)number of characters
x in smembership 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: ==, !=, <, <=, >, >=.
python
if 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.
GoalCorrect formClassic wrong form
Between 10 and 2010 <= x <= 20x >= 10 or x <= 20
Not either conditionnot (a or b)not a or b
One of two pathsif ...: ... 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:
  1. an initial state,
  2. a condition that says continue while true, and
  3. an update that eventually makes it false.
python
n = 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:
NameStarts atChanges byPurpose
Counter0+ 1count occurrences
Accumulator0 or 1add/multiplybuild sum/product
FlagFalseset True once foundremember a condition
Sentinel loopinput valuenew inputstop on special value

5. for, range, nested loops, and control flow

python
for i in range(2, 10, 3):
    print(i)  # 2, 5, 8
range(start, stop, step) excludes stop. range(n) is 0 through n - 1.
python
for 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.
StatementEffect
breakimmediately leaves the nearest loop
continueskips to the next iteration of the nearest loop
passdoes nothing; placeholder only
Pre-submit trace checklist:
  1. What are the types after every input()?
  2. Which condition is checked first?
  3. Does every while path update its progress variable?
  4. Does range include the intended last value?
  5. Is an accumulator reset outside—not inside—the loop?
  6. Does your print use end= exactly where layout requires it?

25-minute last-pass routine

  1. 5 min: write the five section headings from memory and one trap under each.
  2. 10 min: hand-trace one string slice, one decision ladder, one digit loop, and one nested-loop pattern.
  3. 8 min: write two tiny original programs, run them locally, and test boundaries.
  4. 2 min: stop. Hydrate, reset your desk, and do not bring notes or AI into the OPPE.
Document outline

Keep your place and jump directly to a heading.

Table of Contents
System Normal // Awaiting Context

Intelligence Hub

Navigate the knowledge graph to generate context. The Hub adapts dynamically to surface backlinks, related notes, and metadata insights.