Mission #5
🔄Lesson 5: For Loops and Repeating Magic
Cast looping spells to repeat actions effortlessly!
+150 XP
Next MissionLesson 5: For Loops and Repeating Magic
Computers never get tired. Instead of typing print() 50 times, we use a For Loop to repeat actions automatically using Python's helper function range().
Key Concepts to Remember:
- •The for Loop: Repeats a block of code a specific number of times without retyping it.
- •Python Counts from 0!: range(5) counts: 0, 1, 2, 3, 4 (that is still 5 numbers in total).
- •range(start, stop): range(1, 6) counts from 1 up to 5 (Python always stops right BEFORE the second number).
- •Countdown with step: range(10, 0, -1) starts at 10, counts backwards by -1, and stops at 1.
💻Counting Down Example (IDE Syntax Colors)
print("🚀 NASA Countdown Starting...")
for sec in range(5, 0, -1):
print(f"T-minus {sec}...")
print("🔥 BLASTOFF! 🚀")
💡 The loop starts at 5, counts down to 1, and then prints the blastoff message.
💡 Pro Tip: Remember: Python always stops right before the stop number in range().