After working through this lesson, you’ll be able to
Write Python code using lists.
Video Lecture
Introductory Problem
Assign the variables youtube1, youtube2, youtube3, youtube4, youtube5 to the values 10, 23, 13, 15, 11 respectively. The values represent the minutes you spent watching YouTube each day of this week.
Print the value of the sum and average of these variables.
Rewrite your Do Now code as a list named youtube. Where in your code should you place your list? Before the while loop, inside the while loop or after the while loop? Why?
Using a for loop, print the index and elements of youtube. Use the header:
print("Index\tElement")
Create another for loop and just print out the elements.
done =Falseyoutube = [10,23,13,15,11]whilenot done:print("Menu")print("E1 - Example 1")print("Q - Quit") choice =input("Choice: ")if choice =="E1":print("Index\tElement")for i in youtube:print(i,"\t",youtube[i])elif choice =="Q":print("Exiting Game!") done =True
Ask the user for a number to search. If the number is in the list, print "Number is in the list". Else print "Number is not in the list".
done =Falseyoutube = [10,23,13,15,11]whilenot done:print("Menu")print("E3 - Example 3")print("Q - Quit") choice =input("Choice: ")if choice =="E4": num =int(input("Number to search: "))if num in youtube:print(num,"is in the list")else:print(num,"is not in the list")elif choice =="Q":print("Exiting Game!") done =True
Example 5
Write code that performs the following tasks:
Add the element 20 in the beginning of the list.
Remove the last element of the list.
Add the element 30 in the third position of the list.
Add the element 50 to the end of the list.
Using a for loop, print the index and elements of the list.