CSE 110: Introduction to Programming

Sample Solution

The following shows one way to solve this problem. It is not the only way this problem can be solved.

prepare_for_loops_sample.py


"""
Author: Brother Burton

Purpose: Practice using for loops.
"""

# 1. Iterate through a list of items
colors = ["red", "blue", "green", "yellow"]

for color in colors:
    print(color)

print() # A blank line to make the output look nicer

# 2. Iterate through a list of numbers
for i in range(1, 9):
    print(i)

print() # A blank line to make the output look nicer

# 3. Iterate through even numbers

# Notice that we start at 2, go up through, but not including 21, and count by 2
for i in range(2, 21, 2):
    print(i)


Download: prepare_for_loops_sample.py