ICT582 Python Revision
🎯 Learning Objectives
πŸ“š Key Concepts

while Loop

Keeps running as long as the condition is True. You must update the condition inside the loop or it runs forever.

# Count from 1 to 5
i = 1
while i <= 5:
    print(i)
    i += 1       # MUST update i, or infinite loop!

Use while when you don't know in advance how many iterations are needed.

for Loop

Iterates over a sequence (list, string, range) one item at a time.

# Print each item in a list
fruits = ["apple", "banana", "cherry"]
for fruit in fruits:
    print(fruit)

# Iterate over characters in a string
for ch in "hello":
    print(ch)

range() Function

CallProduces
range(5)0, 1, 2, 3, 4 (stop only)
range(2, 8)2, 3, 4, 5, 6, 7 (start, stop)
range(1, 10, 2)1, 3, 5, 7, 9 (start, stop, step)
range(10, 0, -1)10, 9, 8, … 1 (countdown)

The stop value is exclusive β€” range(5) goes 0–4, not 0–5.

for i in range(1, 6):
    print(i)       # 1 2 3 4 5

break β€” Exit Loop Early

Immediately exits the loop, regardless of the condition.

for i in range(10):
    if i == 5:
        break      # stops loop at i=5
    print(i)       # prints 0 1 2 3 4

continue β€” Skip an Iteration

Skips the rest of the current iteration and goes back to the loop condition.

for i in range(6):
    if i % 2 == 0:
        continue   # skip even numbers
    print(i)       # prints 1 3 5

Accumulator Pattern

Initialise a variable before the loop, update it inside. Used for sum, count, max, min.

# Sum of 1 to 10
total = 0
for i in range(1, 11):
    total += i
print("Sum:", total)   # Sum: 55

# Count how many negatives
numbers = [3, -1, 7, -4, 2, -8]
count = 0
for n in numbers:
    if n < 0:
        count += 1
print("Negatives:", count)  # 3

Nested Loops

A loop inside a loop. The inner loop runs completely for each iteration of the outer loop.

for i in range(1, 4):          # outer
    for j in range(1, 4):      # inner
        print(i, "*", j, "=", i*j)

# Total iterations = 3 Γ— 3 = 9

while vs for β€” When to Use

UseWhen
forYou know the number of iterations (iterate over sequence)
whileYou don't know how many times (wait for user input, search)
πŸ’» Code Examples

Example 1 β€” Input validation with while

age = int(input("Enter age: "))
while age < 0 or age > 120:
    print("Invalid. Try again.")
    age = int(input("Enter age: "))
print("Valid age:", age)

Example 2 β€” Multiplication table

n = int(input("Which table? "))
for i in range(1, 11):
    print(f"{n} x {i} = {n * i}")

Example 3 β€” Find first number divisible by 7 between 50–100

for i in range(50, 101):
    if i % 7 == 0:
        print("First:", i)
        break
⚠️ Exam Focus
  1. Trace loop execution β€” know exactly what gets printed for a given loop.
  2. Know range(start, stop, step): stop is exclusive, step can be negative.
  3. Spot infinite loops β€” a while loop without updating the condition variable.
  4. Accumulator pattern: initialise outside loop, update inside β€” very common in exams.
  5. Know when break exits immediately vs continue skips to next iteration.
❌ Common Mistakes
⚑ Quick Recap
← Topic 03: Conditions Topic 05: Data Structures β†’