Lesson 03-04: The for Loop

Learning Target: I can use for loops to repeat a set of instructions.

for Loop Syntax

In addition to conditionals (if statements), another type of control structure are loops. Loops, as the name implies, will repeat some code based on certain conditions! The first loop we are going to learn is the for loop.

At a very basic level, the syntax of a for loop looks like this:

for x in range(times_to_repeat):
    #code to repeat

So for example, running the code below will print "Hello!" five times:

Note that similar to if statements, for loops require a colon at the end and a tab on the next line. This is true anytime you see a colon. But what it also means is, the only part that is looped is the part that is tabbed in!. Take the following as an example:

Hopefully you notice that the “Outside the loop” is only printed once - because it is not considered within the loop. When looping, you wait for the loop to continue executing, then you continue on after the loop.

Checks For Understanding


for x in range(5):
    u0304-1: How many times does “boo” get printed in the block of code above?
  • (A) 3
  • (B) 5
  • Don't forget to count the ones outside the loop!
  • (C) 6
  • (D) 7
  • (E) 9


In the space below, write a for loop that will print out "Baking soda!" three times.

Your code should only contain two lines. The output when run will look like:

Baking soda!
Baking soda!
Baking soda!


Beyonce’s hit single, Single Ladies, starts off like this:

All the single ladies (All the single ladies)
All the single ladies (All the single ladies)
All the single ladies (All the single ladies)
All the single ladies
Now put your hands up

Write a short program using print() statements and a for loop that will print out the lyrics to the introduction of this song. You can have print() statement outside the loop, but no two print() statements can be the same.

Next Section - Lesson 03-05: The range() Function