← BackGrades 7-8 | Chapter 1: Python Basics

Python Loops 🔁

Automation & Repetition

Session 3: For & While Loops

What Are Loops? 🤔

Loops make computers repeat code. Without loops, you'd write the same code over and over. Computers are powerful because they never get tired!

Why Loops Matter:

  • AI trains by repeating millions of times
  • Servers process thousands of requests in loops
  • Games run loops to update graphics 60 times per second
  • Automation means doing the same task repeatedly

For Loops: Counting 📊

A for loop repeats code a specific number of times. Perfect when you know exactly how many repetitions you need.

Code Example - Print Numbers 1-5:

for i in range(1, 6):
  print(i)

Output: 1, 2, 3, 4, 5

range(1, 6) means start at 1, go up to (but not including) 6

For Loop: Multiplication Table 🔢

Create a 7 Times Table:

for i in range(1, 11):
  answer = 7 * i
  print(f"7 × {i} = {answer}")

Output:
7 × 1 = 7
7 × 2 = 14
... (continues to 7 × 10 = 70)

While Loops: Unknown Repetitions ⏰

A while loop repeats while a condition is true. Perfect when you don't know how many repetitions you need.

Countdown Example:

count = 10
while count > 0:
  print(count)
  count = count - 1
print("Blastoff!")

Output: 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, Blastoff!

While Loop: User Input 💬

A common use: repeat until user says to stop.

Code Example:

answer = ""
while answer != "quit":
  answer = input("Say something (or 'quit'): ")
  if answer != "quit":
    print("You said: " + answer)

Loop continues until user types "quit"

For vs While: Which to Use? 🤷

Use For Loop When:

  • You know exactly how many times to repeat
  • You need to count through numbers
  • Iterating through lists (next topic!)

Use While Loop When:

  • You don't know how many repetitions
  • Condition-based repetition
  • Waiting for user input

Project: Guessing Game 🎮

Code:

import random
secret = random.randint(1, 10)
guess = 0
while guess != secret:
  guess = int(input("Guess 1-10: "))
  if guess == secret:
    print("Correct!")
  elif guess < secret:
    print("Too low!")
  else:
    print("Too high!")

Break & Continue 🛑

Control loop flow with special keywords:

Break - Exit Loop Immediately:

while True:
  answer = input("Want to continue? ")
  if answer == "no":
    break # Exit loop
print("Done!")

Continue - Skip Rest of Loop:

for i in range(1, 6):
  if i == 3:
    continue # Skip 3
  print(i) # Prints 1, 2, 4, 5

Nested Loops 🔁🔁

Loops inside loops! Useful for creating patterns.

Times Table Grid:

for i in range(1, 4):
  for j in range(1, 4):
    result = i * j
    print(f"{result}", end=" ")
  print() # New line

Output: A 3×3 multiplication table

Common Loop Mistakes ❌

Watch Out For:

  • Infinite loops: while True without a break (program hangs)
  • Off-by-one errors: range(5) gives 0-4, not 1-5
  • Forgetting indentation: Code inside loop must be indented
  • Updating wrong variable: In while, update the condition variable!

What We Learned 🎓

  • For loops repeat a known number of times
  • While loops repeat while a condition is true
  • range() generates sequences of numbers
  • break exits a loop immediately
  • continue skips to next iteration
  • Nested loops create complex patterns
  • Loops are essential for automation!

Automation Mastered! 🎉

Your programs can now do repetitive tasks

Next Chapter: How AI Actually Works!