# Lab 8: Lists and Loops¶

This lab will give you practice to help solidify your understanding of lists and loops.

## Part 1: Build Your own To-Do List¶

Make sure you understand the syntax introduced in the lists lecture.

### 1.1. Create a List¶

Create a list variable called todo_list with the following items:

1. Wake up early

2. Exercise

3. Eat Breakfast

4. Do Python Lab

5. Watch Class

[ ]:




### 1.2. Change an Item¶

Change “Wake up early” to “Wake up on time”

[ ]:




### 1.3. Access an Item¶

Print the third item in todo_list

[ ]:




### 1.4. Access an Indes¶

Print index three of todo_list

[ ]:




### 1.5. Use append¶

Use the append function to add “Sleep” to the end of todo_list

### 1.6. Use insert¶

Use insert to insert “Read the Book” between “Do Python Lab” and “Watch Class”

[ ]:




### 1.7. Use del¶

Use the del operator to remove “Wake up on time”

[ ]:




### 1.8. Use remove¶

Use the remove function to remove “Sleep” from the todo_list

[ ]:




## Part 2: Simple for Loops¶

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.

### 2.1. Loop over a List¶

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

[ ]:




### 2.2. Counting Loop¶

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

[ ]:




### 2.3. More Counting¶

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

[ ]:




### 2.4. File For Loop¶

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

[ ]:




## Part 3: Algorithms With for Loops¶

Now let’s do some harder things with for loops. If you’re having trouble with these problems remember to use the debugger.

### 3.1. Iterate in Reverse¶

Write a for loop that prints the elements of my_list in reverse order.

[ ]:

my_list = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven']

# Put your code below this comment.


### 3.2. Compute the Sum¶

Write a for loop that prints the sum of all the elements in my_list

[ ]:

my_list = [1, 2, 3.4, 239, 43, 0x12, 0b1101, 4, 3]

# Put your code below this comment.


### 3.3. Find the Largest Value¶

Write a for loop that prints the largest element in my_list

[ ]:

my_list = [1, 2, 3.4, 239, 43, 0x12, 0b1101, 4, 3]

# Put your code below this comment.


### 3.4. Find the Smallest Value¶

Write a for loop that prints the smallest element in my_list.

[ ]:

my_list = [1, 2, 3.4, 239, 43, 0x12, 0b1101, 4, 3]

# Put your code below this comment.


### 3.5. A List as a Function Argument¶

Write a function called add_me that takes one argument the_list. The function adds your name to the end of the list making your name the last element in the list.

• Function: add_me - Add your name to a list.

• Arguments: the_list

• Returns: None

[ ]:




Copy this list definition in to the cell below and call your add_me function with a_list as its argument.

a_list = [ 'one', 'two', 'apple', 'pear' ]


What happens to a_list after you call add_me?

[ ]:




### 3.6. Remove the Smallest Value¶

Write a function called remove_min that takes one argument the_list and removes the smallest value in it. The function returns the smallest value.

• Function: remove_min - Removes and returns the smallest value.

• Arguments: the_list - A list of values (they should all be the same type)

• Returns: The smallest value in the list.

Hint: Use the code you wrote in 3.4.

[ ]:




### 3.7. Sort by Removal¶

Write a for loop that calls your remove_min function from the previous question, once for every element in random_list. Each time you call remove_min print the return value. Your algorithm should remove all of the elments in the order of smallest to largest.

The code in the next cell creates random_list. Use random_list to test the code in this question. Since your code modifies random_list you should re-run the next cell when you want to test your code again. Re-running the cell gives you a new random list.

[ ]:

import random
random_list = []
for _ in range(random.randint(5,15)):
random_list.append(random.randint(0,100))



Put your solution in the next cell:

[ ]:




## A While Loop for Input¶

Write a while loop that uses the input() function to get an integer from the user. If the users’s guess is too high print “Too High” if the user’s guess is too low write “Too Low”. Exit the while loop when the user guesses the correct number. The code in the cell picks a random number an puts it into the number variable.

[ ]:

import random
number = random.randint(1,101)
print (f'Guessing game! Guess a number between 1 and 100 (spoiler alert the nuber is {number})')

# Put your code below this comment.