Back to Student Worksheet
CS Grade 6-8 Answer Key

CS: Python: Lists and Basic Functions

Practice using lists, indexes, loops, parameters, and return values

Answer Key
Name:
Date:
Score: / 15

CS: Python: Lists and Basic Functions

Practice using lists, indexes, loops, parameters, and return values

CS - Grade 6-8

Instructions: Read each problem carefully. For code-writing problems, write clear Python code and show the expected result when asked.
  1. 1

    A Python list is written as scores = [85, 92, 78, 90]. What value is at index 1, and why?

    Count positions starting with 0, not 1.

    The value at index 1 is 92 because Python list indexes start at 0, so index 0 is 85 and index 1 is 92.
  2. 2

    What does this code print? animals = ['cat', 'dog', 'bird']; print(animals[2])

    The code prints bird because 'bird' is the item at index 2 in the list.
  3. 3

    Write one line of Python code that adds the string 'grape' to the end of this list: fruits = ['apple', 'banana']

    Use the list method that adds one item to the end of a list.

    One correct line is fruits.append('grape'). This adds 'grape' to the end of the fruits list.
  4. 4

    What is the final value of nums after this code runs? nums = [1, 2, 3]; nums.append(4); nums.append(5)

    The final value of nums is [1, 2, 3, 4, 5] because append adds each new item to the end of the list.
  5. 5

    What does this code print? names = ['Ava', 'Ben', 'Mia', 'Leo']; print(len(names))

    The len function counts items, not letters.

    The code prints 4 because len(names) counts the number of items in the list.
  6. 6

    Write a function called greet that takes one parameter called name and returns the message 'Hello, ' plus the name.

    Use a parameter inside the return statement.

    One correct answer is def greet(name): return 'Hello, ' + name. For example, greet('Sam') returns 'Hello, Sam'.
  7. 7

    What does this code print? def double(x): return x * 2; print(double(6))

    The code prints 12 because the function returns 6 multiplied by 2.
  8. 8

    Write a function called first_item that takes a list called items and returns the first item in the list.

    Use index 0 to get the first item.

    One correct answer is def first_item(items): return items[0]. This works because the first item in a Python list is at index 0.
  9. 9

    What does this code print? colors = ['red', 'green', 'blue', 'yellow']; print(colors[1:3])

    In a slice, the ending index is not included.

    The code prints ['green', 'blue'] because the slice starts at index 1 and stops before index 3.
  10. 10

    Write a function called count_items that takes a list called things and returns how many items are in the list.

    One correct answer is def count_items(things): return len(things). This returns the number of items in the list.
  11. 11

    What does this code print? nums = [3, 5, 7]; total = 0; for n in nums: total = total + n; print(total)

    Track how total changes each time through the loop.

    The code prints 15 because the loop adds 3, then 5, then 7 to the total.
  12. 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.

    Start with total = 0, then add each number inside the loop.

    One correct answer is def sum_list(nums): total = 0; for n in nums: total = total + n; return total. The function adds each number to total and returns the final sum.
  13. 13

    Find and fix the error in this code: def add_one(x): x + 1; print(add_one(4))

    The function is missing a return statement. A fixed version is def add_one(x): return x + 1; print(add_one(4)). This prints 5.
  14. 14

    What is the difference between print and return in a Python function? Give a short explanation.

    Think about whether the value is only displayed or actually given back to the program.

    print shows a value on the screen, but return sends a value back from the function so the program can use it later.
  15. 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.

    Use an empty result list, an if statement, and append.

    One correct answer is def bigger_than_five(nums): result = []; for n in nums: if n > 5: result.append(n); return result. This creates a new list and adds only the numbers greater than 5.
LivePhysics™.com CS - Grade 6-8 - Answer Key