Skip to content
CY-306 · Computer Workshop (Python Programming)/Quick Revision Short Notes

Computer Workshop (Python Programming) (CY-306) - Unit 3 Short Notes

How unit 3 is examined

This unit covers decision making with if statements, repetition with for and while loops, and the jump statements break, continue and pass. No topic was asked in the supplied papers, so all three are taught as likely fresh questions.

Conditional Statements - If, If-else, Nested If-else

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A conditional statement runs a block of code only when its condition evaluates to True, so the program can choose between different paths.</mark>

Diagram.

<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-01" viewBox="0 0 467 252" width="467" height="252" role="img" aria-label="If-else flow. S = start, C = condition test, T = if block, F = else block, E = next statement"><style>#dsfig-u3-01 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-01 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-01 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-01 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-01 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-01 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-01 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-01 .t{fill:#16181D;font-weight:500}#dsfig-u3-01 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-01 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-01 .dot{fill:#16181D}#dsfig-u3-01 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-01 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-01 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-01 .ah{fill:#454C5A}#dsfig-u3-01 .ah.hi{fill:#2340B8}#dsfig-u3-01 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-01 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-01 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-01 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-01 .e{stroke:#B1B7C3}html.dark #dsfig-u3-01 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-01 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-01 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-01 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-01 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-01 .t{fill:#E6E8ED}html.dark #dsfig-u3-01 .t.inv{fill:#0F1115}html.dark #dsfig-u3-01 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-01 .dot{fill:#E6E8ED}html.dark #dsfig-u3-01 .ann{fill:#8FA3FF}html.dark #dsfig-u3-01 .lbl{fill:#858D9C}html.dark #dsfig-u3-01 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-01 .ah{fill:#B1B7C3}html.dark #dsfig-u3-01 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-01 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-01 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-01 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah1" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh1" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M59,126 L148,126" marker-end="url(#ah1)"/><path class="e" d="M184.8,115.5 L280.5,51.6" marker-end="url(#ah1)"/><path class="e" d="M184.8,136.5 L280.5,200.4" marker-end="url(#ah1)"/><path class="e" d="M313.8,50.5 L409.5,114.4" marker-end="url(#ah1)"/><path class="e" d="M313.8,201.5 L409.5,137.6" marker-end="url(#ah1)"/><g class="wl"><rect x="213.1" y="74" width="40.8" height="18" rx="9"/><text class="t" x="233.5" y="83" dy=".35em" text-anchor="middle">True</text></g><g class="wl"><rect x="210" y="160" width="47.1" height="18" rx="9"/><text class="t" x="233.5" y="169" dy=".35em" text-anchor="middle">False</text></g><circle class="n" cx="40" cy="126" r="18"/><text class="t" x="40" y="126" dy=".35em" text-anchor="middle">S</text><circle class="n" cx="169" cy="126" r="18"/><text class="t" x="169" y="126" dy=".35em" text-anchor="middle">C</text><circle class="n" cx="298" cy="40" r="18"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">T</text><circle class="n" cx="298" cy="212" r="18"/><text class="t" x="298" y="212" dy=".35em" text-anchor="middle">F</text><circle class="n" cx="427" cy="126" r="18"/><text class="t" x="427" y="126" dy=".35em" text-anchor="middle">E</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">If-else flow. S = start, C = condition test, T = if block, F = else block, E = next statement</figcaption></figure>

Key points.

  1. The if statement runs its indented block only when the condition is True, and the block is skipped when the condition is False.
  2. The if-else statement gives two paths, and exactly one of them runs: the if block on True and the else block on False.
  3. The elif clause chains several conditions, and the first True branch runs while all remaining branches are skipped.
  4. A nested if places one if statement inside another, so the inner condition is tested only when the outer condition is True.
  5. Python marks a block by indentation, usually four spaces, and a colon must follow the condition, so a wrong indent gives an IndentationError.
  6. Conditions use comparison operators (==, !=, <, >, <=, >=) and logical operators (and, or, not); any non-zero number or non-empty value counts as True.

Syntax.

if condition1:
    block1
elif condition2:
    block2
else:
    block3

Example.

m = 72
if m >= 90: print("A")
elif m >= 60: print("B")   # prints B
else: print("C")

x = 15
if x > 10:                 # nested if
    if x % 2 == 0: print("even>10")
    else: print("odd>10")  # prints odd>10

Logical operators in conditions.

Operator Meaning Result
and Both conditions are True True only if both are True
or At least one condition is True False only if both are False
not Reverses the condition True becomes False and the reverse

More examples.

a, b, c = 7, 12, 9
if a > b and a > c: print(a)
elif b > c: print(b)        # prints 12 (largest of three)
else: print(c)

print("even" if 8 % 2 == 0 else "odd")   # even, one-line form
  1. The one-line conditional expression x if condition else y gives x when the condition is True and y otherwise, and it suits simple choices.
  2. An if block cannot be empty, so pass is written when the body is not ready yet.

Answer frame. Open with the definition; draw the if-else flowchart; then give the syntax of if, if-else, elif and nested if in that order with one small program; close with the point that indentation defines each block.

Pitfall: Writing = (assignment) instead of == (comparison) in the condition is a syntax error in Python.

Iterative Statement - For, While, Nested Loops

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>A loop repeats a block of statements again and again, and Python provides the for loop and the while loop.</mark>

Diagram.

<figure class="ds-fig" style="margin:1.4rem 0;overflow-x:auto"><svg xmlns="http://www.w3.org/2000/svg" id="dsfig-u3-02" viewBox="0 0 338 252" width="338" height="252" role="img" aria-label="Loop flow. S = start, C = condition test, B = loop body, E = exit to next statement"><style>#dsfig-u3-02 .e{stroke:#454C5A;stroke-width:1.4;fill:none}#dsfig-u3-02 .e.hi{stroke:#2340B8;stroke-width:2.6}#dsfig-u3-02 .n{fill:#FFFFFF;stroke:#16181D;stroke-width:1.4}#dsfig-u3-02 .n.hi{fill:#E3E9FC;stroke:#2340B8;stroke-width:2.2}#dsfig-u3-02 .n.rb-b{fill:#16181D;stroke:#16181D}#dsfig-u3-02 .n.rb-r{fill:#BD3227;stroke:#BD3227}#dsfig-u3-02 text{font-family:"JetBrains Mono",ui-monospace,Menlo,Consolas,monospace;font-size:13px}#dsfig-u3-02 .t{fill:#16181D;font-weight:500}#dsfig-u3-02 .t.inv{fill:#FFFFFF;font-weight:700}#dsfig-u3-02 .kd{stroke:#16181D;stroke-width:1.2}#dsfig-u3-02 .dot{fill:#16181D}#dsfig-u3-02 .ann{fill:#2340B8;font-size:11px;font-weight:700}#dsfig-u3-02 .lbl{fill:#6F7787;font-family:system-ui,-apple-system,sans-serif;font-size:12px;font-weight:700}#dsfig-u3-02 .ptr{fill:#2340B8;font-size:12px;font-weight:700}#dsfig-u3-02 .ah{fill:#454C5A}#dsfig-u3-02 .ah.hi{fill:#2340B8}#dsfig-u3-02 .wl rect{fill:#FFFFFF;stroke:#DCE0E7}#dsfig-u3-02 .wl .t{font-size:12px;font-weight:700}#dsfig-u3-02 .wl.hi rect{fill:#2340B8;stroke:#2340B8}#dsfig-u3-02 .wl.hi .t{fill:#FFFFFF}html.dark #dsfig-u3-02 .e{stroke:#B1B7C3}html.dark #dsfig-u3-02 .e.hi{stroke:#8FA3FF}html.dark #dsfig-u3-02 .n{fill:#161920;stroke:#E6E8ED}html.dark #dsfig-u3-02 .n.hi{fill:#1E2748;stroke:#8FA3FF}html.dark #dsfig-u3-02 .n.rb-b{fill:#E6E8ED;stroke:#E6E8ED}html.dark #dsfig-u3-02 .n.rb-r{fill:#FF7E71;stroke:#FF7E71}html.dark #dsfig-u3-02 .t{fill:#E6E8ED}html.dark #dsfig-u3-02 .t.inv{fill:#0F1115}html.dark #dsfig-u3-02 .kd{stroke:#E6E8ED}html.dark #dsfig-u3-02 .dot{fill:#E6E8ED}html.dark #dsfig-u3-02 .ann{fill:#8FA3FF}html.dark #dsfig-u3-02 .lbl{fill:#858D9C}html.dark #dsfig-u3-02 .ptr{fill:#8FA3FF}html.dark #dsfig-u3-02 .ah{fill:#B1B7C3}html.dark #dsfig-u3-02 .ah.hi{fill:#8FA3FF}html.dark #dsfig-u3-02 .wl rect{fill:#161920;stroke:#2A2E37}html.dark #dsfig-u3-02 .wl.hi rect{fill:#8FA3FF;stroke:#8FA3FF}html.dark #dsfig-u3-02 .wl.hi .t{fill:#0F1115}</style><defs><marker id="ah2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah" d="M0,1 L9,5 L0,9 z"/></marker><marker id="ahh2" viewBox="0 0 10 10" refX="9" refY="5" markerWidth="7" markerHeight="7" orient="auto-start-reverse"><path class="ah hi" d="M0,1 L9,5 L0,9 z"/></marker></defs><path class="e" d="M59,126 L148,126" marker-end="url(#ah2)"/><path class="e" d="M187.6,122.3 Q251.3,109.6 286.3,57.4" marker-end="url(#ah2)"/><path class="e" d="M279.4,43.7 Q215.7,56.4 180.7,108.6" marker-end="url(#ah2)"/><path class="e" d="M184.8,136.5 L280.5,200.4" marker-end="url(#ah2)"/><g class="wl"><rect x="223.7" y="90.7" width="40.8" height="18" rx="9"/><text class="t" x="244.1" y="99.7" dy=".35em" text-anchor="middle">True</text></g><g class="wl"><rect x="210" y="160" width="47.1" height="18" rx="9"/><text class="t" x="233.5" y="169" dy=".35em" text-anchor="middle">False</text></g><circle class="n" cx="40" cy="126" r="18"/><text class="t" x="40" y="126" dy=".35em" text-anchor="middle">S</text><circle class="n" cx="169" cy="126" r="18"/><text class="t" x="169" y="126" dy=".35em" text-anchor="middle">C</text><circle class="n" cx="298" cy="40" r="18"/><text class="t" x="298" y="40" dy=".35em" text-anchor="middle">B</text><circle class="n" cx="298" cy="212" r="18"/><text class="t" x="298" y="212" dy=".35em" text-anchor="middle">E</text></svg><figcaption style="font-size:.82em;opacity:.72;margin-top:.45rem">Loop flow. S = start, C = condition test, B = loop body, E = exit to next statement</figcaption></figure>

Key points.

  1. A for loop iterates over a sequence such as a list, tuple, string or range(), and it is used when the number of repetitions is known.
  2. A while loop repeats as long as its condition stays True, so the body must change the condition or the loop runs forever.
  3. range(start, stop, step) produces numbers up to but not including stop, so range(1, 4) gives 1, 2, 3 and range(10, 0, -3) gives 10, 7, 4, 1.
  4. A nested loop puts one loop inside another, and the inner loop completes all its iterations for every single pass of the outer loop.
  5. Both loops may have an else block, which runs when the loop ends normally and is skipped if the loop is left by break.
  6. The loop variable of a for loop keeps its last value after the loop ends.

Comparison.

Basis for loop while loop
Use Known number of repetitions Unknown number of repetitions
Controlled by Sequence or range() Condition
Loop variable Updated automatically Programmer must update it
Risk Rarely infinite Infinite loop if condition never becomes False
Syntax for i in range(n): while condition:

Example.

t = 0
for x in range(1, 101): t += x
print(t)                    # 5050

for i in range(1, 4):       # nested loop, star pattern
    print("*" * i)          # * then ** then ***

More examples.

n, f = 5, 1
while n > 0:                # factorial of 5 with while
    f *= n
    n -= 1
print(f)                    # 120

a, b = 0, 1
for _ in range(7):          # Fibonacci series
    print(a, end=" ")       # 0 1 1 2 3 5 8
    a, b = b, a + b

Trace of a nested loop (for i in range(1, 4): for j in range(1, 3): print(i*j)):

Outer i Inner j runs Printed
1 1, 2 1 2
2 1, 2 2 4
3 1, 2 3 6

The inner loop runs 2 times for each of the 3 outer passes, so the body executes $3 \times 2 = 6$ times.

  1. Looping over a string gives its characters one by one, and looping over a dictionary gives its keys.
  2. while True: makes an intentional infinite loop that is normally left with break.

Answer frame. Open with the definition of a loop; draw the loop flowchart; then explain for, while and nested loops with a comparison table and one program each; close with the note that the inner loop runs fully for each outer iteration.

Pitfall: Forgetting to update the variable inside a while loop creates an infinite loop.

Control statements - Break, Continue, Pass

<span style="display:inline-block;padding:.16em .6em;border:1.5px solid currentColor;border-radius:999px;font-size:.68em;font-weight:700;letter-spacing:.06em;text-transform:uppercase;opacity:.75">Not asked since 2022</span>

Definition. <mark>Break, continue and pass are control statements that change the normal flow of execution inside a loop or a block.</mark>

Key points.

  1. The break statement terminates the innermost loop immediately, and control moves to the first statement after that loop.
  2. The continue statement skips the rest of the current iteration and jumps straight to the next iteration of the loop.
  3. The pass statement is a null operation that does nothing, and it is used as a placeholder where a statement is syntactically required, such as an empty function, class or loop body.
  4. The break and continue statements affect only the loop in which they are written, so in nested loops the outer loop is not stopped.
  5. When a loop is left by break, its else block is skipped, which is used in search programs such as prime checking.

Comparison.

Basis break continue pass
Effect Exits the loop Skips to next iteration No effect
Loop continues No Yes Yes
Typical use Stop when found Ignore unwanted values Empty block placeholder

Example.

for n in range(1, 6):
    if n == 2: continue     # skip 2
    if n == 4: break        # stop at 4
    print(n, end=" ")       # 1 3

for n in range(2, 8):
    for d in range(2, n):
        if n % d == 0: break
    else: print(n, end=" ") # 2 3 5 7

Uses of each statement.

for i in range(1, 6):
    if i == 3: pass         # placeholder, nothing happens
    else: print(i, end=" ") # 1 2 4 5

def todo(): pass            # empty function body is legal
  1. Use break when the search is finished, for example on finding a value in a list, so the remaining iterations are not wasted.
  2. Use continue to ignore unwanted items, for example to skip negative numbers while adding a list.
  3. Without pass, an empty function, class, if or loop body causes a syntax error.

Answer frame. Open with the definition; give one line on each statement in the order break, continue, pass; show the comparison table and the example with its output; close with the point that pass merely holds a place.

Pitfall: pass does not skip anything, unlike continue; it only fills an empty block.

Last-minute revision

  • A conditional statement runs a block only when its condition is True.
  • if-elif-else runs exactly one branch, the first True one.
  • A nested if is an if placed inside another if, and the inner test runs only when the outer test is True.
  • for iterates over a sequence and while repeats while its condition is True.
  • range(1, 4) gives 1, 2, 3 because the stop value is excluded.
  • range(10, 0, -3) gives 10, 7, 4, 1.
  • In nested loops the inner loop runs fully for each outer iteration.
  • break exits the loop, continue skips to the next iteration, pass does nothing.
  • The loop else block runs only when the loop is not ended by break.
  • Python uses indentation and a colon, not braces, to mark blocks.
  • The sum of 1 to 100 by a for loop is 5050.

Memory hooks

  • Break = exit the loop, Continue = skip this round, Pass = placeholder.
  • For = known count, While = unknown count.
  • Nested loop = clock: the minutes complete fully for every hour tick.
  • Elif = "else if": first True wins, the rest are ignored.

Coverage checklist

  • Conditional Statements - If, If-else, Nested If-else: covered with flowchart, syntax and example; no past questions.
  • Iterative Statement - For, While, Nested Loops: covered with flowchart, comparison and examples; no past questions.
  • Control statements - Break, Continue, Pass: covered with comparison and example; no past questions.
Go to where you left off?

Quick Add to Notes

Save questions, your own notes and screenshots into notes filed by unit. It takes a free account.

Create free account

Have an account? Log in