Python Lists for Beginners

Python Lists for Beginners

Here are the runnable exercises for Python Lists for Beginners.

#1. Create a list of three of your favorite foods. Save it in a variable called foods.

# Your code here:

#2. From your list of favorite foods, select the second item.

#3. Using a slice, create a reversed copy of your list of favorite food

#4. my name is john".upper() returns the string “MY NAME IS JOHN”. Given that fact, create an uppercase list of your favorite foods using a for loop.

#5. Create a second uppercase list of your favorite foods, using a list comprehension.

For exercises 6-14, use the following list as your starting point. (Tip: If you run the cell below, you can use the variables in cells beneath).

letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G']

#6. Use a function to get the length of the letters list. Display it.

#7. Get the last item in the list, using a positive index.

#8. Get the last item in the list, using a negative index.

#9. Write a slice expression to return the letters D through F (inclusive)

#10. Write a slice expression that returns the following list: [‘A’, ‘D’, ‘G’]

#11. Based on what you learned in exercise 4, can you guess how to write a program to return a list of lowercase letters based on the letters list?

#12. For #11, did you iterate the list using a list comprehension or a for loop? Why?

#13. How could you return a copy of the list using a slice expression?

#14. How could you return a list that looks like the list below?

['G', 'F', 'E', 'D', 'C', 'B', 'A']

#15. The range function returns a sequence — that is to say, it can be used as you would use a list inside a for-loop or list comprehension. For example:

num_list = [x for x in range(1,6)]
print(num_list)

Output:

[1, 2, 3, 4, 5]

Based on what you know about slice syntax, what would you expect the output of the following code to be?

#16. What do you predict will be printed by the code below?

num_list = [x for x in range(1,6)]
num_list[0:2] = [99, 100]
print(num_list)

#17. Here’s a slight variation on the code from #16. What do you expect will be printed by the following code?

num_list = [x for x in range(1,6)]
other_list = num_list[:]
other_list[0:2] = [99, 100]
print(num_list)