đ Repeating Actions
Repeating Actions
Learn how to make your programs repeat actions using loops - this makes your code shorter and more powerful!
What are Loops?
Loops let you repeat code multiple times without writing it over and over. Think of it like repeating a song - instead of singing it 10 times manually, you just say "repeat 10 times"!
The FOR Loop
The for loop repeats code a specific number of times. It's perfect when you know exactly how many times you want to repeat something.
Basic FOR Loop
What happens: The code inside the loop runs 5 times. The variable i counts from 0 to 4.
Output:
Understanding range()
The range() function creates a sequence of numbers. Here's how it works:
range(5)
Creates numbers: 0, 1, 2, 3, 4
(5 numbers, starting from 0)
range(1, 5)
Creates numbers: 1, 2, 3, 4
(starts at 1, stops before 5)
range(0, 10, 2)
Creates numbers: 0, 2, 4, 6, 8
(starts at 0, stops before 10, steps by 2)
The WHILE Loop
The while loop repeats code as long as a condition is True. It keeps going until the condition becomes False.
WHILE Loop Example
What happens: The loop runs while count is less than 5. Each time, it prints the count and adds 1 to it. When count reaches 5, the condition is False and the loop stops.
FOR vs WHILE: When to Use Which?
Use FOR Loop When:
- You know how many times to repeat
- You're working with a list of items
- You want to count something
Use WHILE Loop When:
- You don't know how many times to repeat
- You want to keep going until something happens
- You're waiting for a condition to change
Nested Loops
You can put loops inside other loops! This is called "nested loops" and it's useful for creating patterns.
Nested Loop Example
What happens: The outer loop runs 3 times. Each time, the inner loop runs 3 times. So you get 9 total prints (3 Ã 3).
Project: Blinking Pattern Generator
Let's create an LED that blinks in different patterns using loops!
Materials Needed:
- Raspberry Pi
- LED (any color)
- Resistor (220Ί)
- Breadboard and jumper wires
Pattern 1: Blink 5 Times
Pattern 2: SOS Pattern (3 short, 3 long, 3 short)
Pattern 3: Fade In and Out (using while loop)
Note: This uses PWM (Pulse Width Modulation) to control LED brightness. We'll learn more about PWM in later lessons!
Loop Control: break and continue
Sometimes you want to stop a loop early or skip an iteration. Python has two special keywords for this:
break
Stops the loop immediately
continue
Skips the rest of this iteration
Common Mistakes to Avoid
â ī¸ Watch Out For:
- Infinite Loops: Make sure your while loop has a way to stop
- Missing Colon: Don't forget the colon (:) after for and while
- Wrong Indentation: Code inside loops must be indented
- Off-by-One Errors: Remember range(5) gives 0-4, not 1-5
- Forgetting to Update: In while loops, make sure you change the condition variable
Summary
You've learned:
- â Loops let you repeat code without writing it multiple times
- â
forloops repeat a specific number of times - â
whileloops repeat as long as a condition is True - â
range()creates sequences of numbers for loops - â You can nest loops inside other loops for complex patterns
- â
breakstops a loop early,continueskips an iteration - â Loops are essential for creating LED patterns and animations
đŽ Try It: Practice with Loops!
Practice writing loops. Try these challenges:
đ Challenge 1: Count to 10
Write a for loop that prints numbers from 1 to 10:
đ Challenge 2: Countdown
Write a loop that counts down from 10 to 1, then prints "Blast off!":
đ Challenge 3: Sum Numbers
Write a loop that adds numbers from 1 to 5 and prints the total:
range(5) gives 0-4, but range(1, 6) gives 1-5. Use the second form when you want to start from 1!
đ¯ Activity: Blinking Pattern Generator
What You'll Build:
Create an LED that blinks in different patterns using loops!
Step-by-Step Instructions:
- Set Up Hardware: Connect LED to GPIO 18 with resistor
- Pattern 1: Write code to blink the LED exactly 5 times using a for loop
- Pattern 2: Create a pattern: blink 3 times fast, pause, blink 3 times slow
- Pattern 3: Use nested loops to create a pattern (e.g., 3 groups of 2 blinks each)
- Test: Run each pattern and observe the LED behavior
- Experiment: Try changing the number of blinks and timing
Pattern Ideas:
- â SOS pattern: 3 short, 3 long, 3 short
- â Heartbeat pattern: 2 quick blinks, pause, repeat
- â Countdown pattern: 5 blinks, pause, 4 blinks, pause, etc.
- â Random pattern: Use nested loops for complex sequences
đĒ Practice Challenges
Challenge 1: Multiplication Table
Write a loop that prints the 5 times table (5 Ã 1 = 5, 5 Ã 2 = 10, etc.) up to 5 Ã 10:
Challenge 2: LED Blink Counter
Write code that blinks an LED 10 times, but prints the count each time:
- Blink 1, Blink 2, Blink 3, etc.
- After 10 blinks, print "Done!"
Challenge 3: Stop at 7
Write a loop that counts from 1 to 10, but stops (using break) when it reaches 7:
Challenge 4: Skip Even Numbers
Write a loop that prints numbers 1-10, but skips even numbers (use continue):
Challenge 5: Nested Loop Pattern
Use nested loops to create this pattern: