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 answer will vary
foods = ['Fish and Chips', 'African Vegetables', 'Chilli']
print(foods)
['Fish and Chips', 'African Vegetables', 'Chilli']
#2. From your list of favorite foods, select the second item.
print(foods[1])
African Vegetables
#3. Using a slice, create a reversed copy of your list of favorite food
print(foods[::-1])
['Chilli', 'African Vegetables', 'Fish and Chips']
#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.
uppercase_foods = []
for food in foods:
uppercase_foods.append(food.upper())
print(uppercase_foods)
['FISH AND CHIPS', 'AFRICAN VEGETABLES', 'CHILLI']
#5. Create a second uppercase list of your favorite foods, using a list comprehension.
uppercase_foods = [food.upper() for food in foods]
print(uppercase_foods)
['FISH AND CHIPS', 'AFRICAN VEGETABLES', 'CHILLI']
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.
print(len(letters))
7
#7. Get the last item in the list, using a positive index.
last_item_index = len(letters) - 1
print(letters[last_item_index])
G
#8. Get the last item in the list, using a negative index.
print(letters[-1])
G
#9. Write a slice expression to return the letters D through F (inclusive)
subset = letters[3:6]
print(subset)
['D', 'E', 'F']
#10. Write a slice expression that returns the following list: [‘A’, ‘D’, ‘G’]
every_third_letter = letters[::3]
print(every_third_letter)
['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?
lower_letters = [letter.lower() for letter in letters]
print(lower_letters)
['a', 'b', 'c', 'd', 'e', 'f', 'g']
#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?
print(letters[:])
['A', 'B', 'C', 'D', 'E', 'F', 'G']
#14. How could you return a list that looks like the list below?
['G', 'F', 'E', 'D', 'C', 'B', 'A']
print(letters[::-1])
['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?
num_list = [x for x in range(0,26,5)]
print(num_list)
[0, 5, 10, 15, 20, 25]
[0, 5, 10, 15, 20, 25]
#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)
[99, 100, 3, 4, 5]
[99, 100, 3, 4, 5]
#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)
[1, 2, 3, 4, 5]
[1, 2, 3, 4, 5]