Playing With Loops

Sometimes, there are pieces of code that have to run multiple times. It would be a pain to write the pieces of code multiple times. What we can do instead, though, is use loops to run the code over and over again.


"For" Loops

Create a new Repl called loops_example. Now, write the line print("You are the high scorer!") 10 TIMES.

Run the above code. What you'll see is that "You are the high scorer" prints out 10 times. It was boring writing that out 10 times though, right?

Let's now remove all of the print statements except one, and place it inside a For Loop.
Run the above code and notice what happens. The one line of code that we put inside the loop body executes multiple times--10 to be exact. *Play around with the numbers in parentheses and see what happens*

Below, we take a closer look at the structure of a for loop in Python:
This for loop consists of:

  • The word for
  • The loop variable - it keeps track of how many times the loop has run so far. At the start of the loop, it’s equal to the first number in the range. The second time around, it’s equal to the second number in the range, and so on. Once it completes the second-to-last number in the range, the loop stops.
  • in range(start, end) - all the numbers in the list from the first number to the second-to-the-last number. This tells us how many times the loop will run. If our range is (1,5), the loop will run 1,2,3,4 times
  • The [TAB] key. After we write the header of our loop, we have to indent the loop body using the tab key (or four spaces), just like with an if/elif/else statement. And don't forget the colon at the end of the loop header!
  • The loop body. This is the block of code that gets repeated.


Looping Over a List

As seen above, we can repeat a block of code a set number of times using a for loop. We can Also use a for loop to repeat a block of code for each element in a list
In the below example, we loop over a list of names, and print each name in the list out. Write and run the code.
Our loop variable, robot, holds a single item in the list instead of a number in a range. The value in robot is updated each time around the loop, so that it holds Bing, then Bleep, and finally Bloop. Once it reaches the end of the list, the loop stops. **Play around with the list and see what happens.


Looping Over a String

You can loop over strings (text) the same way you loop over list. To do so, write the following:

for char in string_variable:
    print(char)
                            
Set string_variable to any string (remeber: quotes!) and write the above code. Run the program and see what happens


Looping Over Two Lists

We can loop through one list in Python pretty easily, but if we want to loop through two (or more) lists at once, you'll need an extra variable to navigate both lists.

Write the below code in your file. This program uses a variable called index to move through both lists, printing out each robot’s name and also what color it is.
We advance throught both lists by indexing each list and updating the index variable.



"While" Loops

We use for loops when we know exactly how many times we want something to repeat. When we aren't sure how many times we want code to repeat, though, we use a while loop.

Think of a while loop as a loop with an if statement--as long as the if statement is true, the loop body repeats. Write the below code in your file.
This program asks the user if they want to throw a water baloon. As long as the loop condition is met (equals true) the loop body repeats. Once it turns false, the code continues and "Goodbye!" is printed. **Play around with the loop condition

Infinite Loops

If our loop condition can never be false, we run an infinite loop. This essentially means that our loop body runs forever. This is generally not advised in coding, as it can overload the computer you're programming.

Activity

Write a program that takes text as input and outputs the number of vowels in the text.
Extension: Have the program keep asking for text if the text has no vowels

Copyright © KX Technology Group, LLC. All Rights Reserved.