CS Grade 6-8

CS: Python: Lists and Basic Functions

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

View Answer Key
Name:
Date:
Score: / 15

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
    Four list boxes with the second position highlighted.

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

  2. 2
    Three animal list boxes with the bird selected.

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

  3. 3
    A grape being added to the end of an apple and banana list.

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

  4. 4
    Two new items being appended to the end of a list.

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

  5. 5
    A list of four student avatars being measured as a group.

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

  6. 6

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

  7. 7

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

  8. 8
    The first item in a list of school objects is highlighted.

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

  9. 9
    The green and blue cells are selected from a four-color list.

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

  10. 10
    A whole list of objects is being counted.

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

  11. 11
    Items from a list are combined into one total container.

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

  12. 12
    A loop moves list items into an accumulator container.

    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. 13

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

  14. 14
    One function machine displays output while another sends a value back.

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

  15. 15
    A filter lets larger list items pass into a new list.

    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.

LivePhysics™.com CS - Grade 6-8

More CS Worksheets

See all CS worksheets

More Grade 6-8 Worksheets

See all Grade 6-8 worksheets