Build confidence with Python lists and basic functions by reading code, predicting output, and writing short functions.
Read each problem carefully. For code-writing problems, write clear Python code and show the expected result when asked.
Practice using lists, indexes, loops, parameters, and return values
CS - Grade 6-8
- 1
A Python list is written as scores = [85, 92, 78, 90]. What value is at index 1, and why?
- 2
What does this code print? animals = ['cat', 'dog', 'bird']; print(animals[2])
- 3
Write one line of Python code that adds the string 'grape' to the end of this list: fruits = ['apple', 'banana']
- 4
What is the final value of nums after this code runs? nums = [1, 2, 3]; nums.append(4); nums.append(5)
- 5
What does this code print? names = ['Ava', 'Ben', 'Mia', 'Leo']; print(len(names))
- 6
Write a function called greet that takes one parameter called name and returns the message 'Hello, ' plus the name.
- 7
What does this code print? def double(x): return x * 2; print(double(6))
- 8
Write a function called first_item that takes a list called items and returns the first item in the list.
- 9
What does this code print? colors = ['red', 'green', 'blue', 'yellow']; print(colors[1:3])
- 10
Write a function called count_items that takes a list called things and returns how many items are in the list.
- 11
What does this code print? nums = [3, 5, 7]; total = 0; for n in nums: total = total + n; print(total)
- 12
Write a function called sum_list that takes a list of numbers called nums and returns the sum of all numbers in the list using a for loop.
- 13
Find and fix the error in this code: def add_one(x): x + 1; print(add_one(4))
- 14
What is the difference between print and return in a Python function? Give a short explanation.
- 15
Write a function called bigger_than_five that takes a list of numbers and returns a new list containing only the numbers greater than 5.