9.6. Python Loops and Lists

../_images/python-logo.png

9.6.1. Python Loops

Loops are used to repeat a block of code. In Python, the : is used after for and while loops. The for uses a counter variable, often i for index that is incremented every time in the loop within a range of values including the initial value in the range and up to but not including the last value in the range. The statements under the loop must all be indented and lined up under the for.

# loop 10 times from 0 to 9
for i in range(10):
    # do something

# loop from initial value up to final value
for i in range(initial, final):
    # do something
    # do something

The following code will print out the numbers 1 to 10. Try it out by clicking the “Run” button. Click on the CodeLens button and then next to step through the code line by line and see the i variable change.

Run the following code. Then, change the range to print out the numbers 1 to 20. CLick on the Show CodeLens button and then click repeatedly on the Next button to see the value of the variable i change as the loop runs.

Put the blocks in order to create the for loop header to print from 2 to 5. There are extra blocks that you don’t need.

The following program should print out 5 *’s using a for loop. Drag the needed blocks from the left and put them in the correct order and indentation on the right. Choose between the a and b blocks. The extra blocks are not needed in the solution. Click the Check button to check your solution. Click on Help if you get stuck.

Let’s use a loop with a turtle to draw a square.

Run this code to have the turtle draw a square using a loop. Can you make the turtle draw a hexagon? How many times does the loop need to run to draw each side? What should the angle be for each turn? Try different values until you get it to work.

9.6.2. Python Lists

Lists in Python are indicated with square brackets: [].

# create a list of numbers
numbers = [1, 2, 3, 4, 5]
# create a list of strings
animals = ["cat", "dog", "bird", "fish"]
print(animals)

Lists in Python are numbered starting from 0. The list name can be followed by the square brackets with an index number in it to retrieve the element at that index. For example animals[0] will return the first element in the list. Change the index variable below to see what is printed out.

Run the following code. Change the index i to another number and run again.

Loops are often used to go through a list of items. In fact, the range function we used in the for loops above actually creates a list of numbers. Instead of range, we can simply use the list name in a for loop to visit each item in the list.

# loop through a list of numbers
for number in numbers:
    # do something
# loop through a list of strings
for animal in animals:
    # do something

The following code will print out each item in the list on a separate line.

Run the following code. Add more animals to the list and run it again.

The following program should calculate and print the average of a list of numbers using a for loop. Start by initializing the variable sum and then create the list of numbers. The blocks have been mixed up and include extra blocks that are not needed in the solution. Drag the needed blocks from the left and put them in the correct order on the right. Click the Check Me button to check your solution.

9.6.3. Picture Project

../_images/student.jpg

Photographs and images are made up of pixels which are tiny picture elements that color in the image. The color of a pixel is represented using the RGB (Red, Green, Blue) color model, which stores values for red, green, and blue, each ranging from 0 to 255. You can make any color by mixing these values!

Try the following Color Chooser by moving the sliders to see the RGB values for each color. What is the RGB value for black? What is the RGB value for white? What is the RGB value for purple?

The following code will create a picture from a file and then loop through all the pixels in the picture. It will then change the color of any pixel that is close to white to cyan. This code and project are based on the Picture Lab by Dr. Barbara Ericson.

Run the following code to see how it changes the white background of the image. The original image is above. Can you change the color of the student’s hair which is close to black? Can you change the red t-shirt to another color? You can also experiment with the files kitten.jpg and puppies.jpg.

You can use the images below to try out your code.

Data file: student.jpg
Data file: kitten.jpg
Data file: puppies.jpg
You have attempted of activities on this page