🐍 Python for Everyone

← Back to the quiz

Chapter 7 Exercises 20 min, hands-on

Exercises: Lists & Tuples

How to run these Download starter.py below, then run python3 starter.py.

1 Playlist slicing

Given playlist = ["Intro", "Song A", "Song B", "Song C", "Outro"], use slicing to print the first 3 songs, then the last 2 songs.

2 Cart methods

Given cart = ["notebook", "pen"], append "eraser", then insert "ruler" at position 1, then remove "pen". Print the cart after each step.

3 Tuple unpacking

Given location = ("Austin", "TX", 78701), unpack it into city, state, and zip_code, then print: "Austin, TX 78701".

4 Nested list grid

Given a 3x3 grid of numbers, print the value at row 1, column 2, then change row 0, column 0 to 100 and print the whole grid.

🐞 Task 5 — Debug the Code

This is supposed to leave original untouched and only change backup, but it changes both. Find and fix the bug.

buggy.py
original = ["draft"]
backup = original
backup.append("final")
print("original:", original)
print("backup:", backup)

6 Order report

Given a list of orders (each one [order_id, amount, status]), loop through them, and for every order with status "shipped", print its id and add its amount to a running total. Print the total in the form: "Total shipped: $107.25".

← Back to Quiz Practice Bank →