Lab: Loops

This lab will help you better understand how loops work.

Part 1: for Loops and Squences

In programming there’s a thing called the 90/10 rule: A program spends 90% of its time in 10% of the code. That’s because of loops! Here is some practice with the for loop.

1. Loop over a List

Write a for loop that prints every element of todo_list from the last part.

[ ]:

2. Counting Loop

Write a for loop that prints every number from 0 to 10 including 0 an 10

[ ]:

3. More Counting

Write a for loop that prints every number from 5 to 10 including 5 and 10.

[ ]:

4. File For Loop

Write a for loop that prints every line of the file ../Lessons/files/example.txt with line nubers.

[ ]:

5. Loop Over Letters

Write a for loop that prints each letter in the sentence variable one at a time.

[2]:
sentence = "Mary had a little lamb."

6. Loop Over Words

Write a for loop that prints each word in the sentence variable, one at a time. Hint: Look for ``split`` in the Lists lecture notes.

[3]:
sentence = "Mary had a little lamb."

Part 2: The for Loop and Dictionaries

Now get practice using the for loops with a dictionary. Here’s a dictionary to get started:

[5]:
courses = {
    'cis-15': 'Python Programming for Everyone',
    'cis-81': 'Introduction to Networking',
    'cis-90': 'Introduction to UNIX/Linux',
    'cis-75': 'Computer Security Fundamentals',
}

1. Loop Over Keys

Write a for loop that prints all the keys in courses.

[ ]:

2. Loop Over Values

Write a for loop that prints all of the values in courses.

[ ]:

3. Keys and Values

Write a for loop that prints all keys and values in courses.

[ ]: