CODE PATH
PRO ACCOUNT

Master Code
On The Go

Learn. Practice. Build.

Quest 1 β€’ Lesson 3

πŸ“¦ Lists & Tuples

Store, organise, and manipulate collections of data – understand when to use lists vs tuples.

A list is a collection of items that can be changed (mutable). A tuple is like a list but cannot be changed (immutable). Both are fundamental for storing groups of data.

"Lists are great for to‑do lists – you can add, remove, or modify tasks. Tuples are for fixed data, like days of the week."
lists.py
fruits = ["apple", "banana", "cherry"]
mixed = [10, "hello", True, 3.14]

print(fruits) # Output: ['apple', 'banana', 'cherry']
print(fruits[0]) # Output: apple

🧠 List Basics

πŸ› οΈ Useful List Methods

.append() – adds an item to the end.
fruits.append("orange") # fruits now has 4 items
.remove() – removes the first occurrence of an item.
fruits.remove("banana")
len() – returns the number of items.
print(len(fruits)) # Output: 3
.pop() – removes and returns the last item.
last = fruits.pop() # removes and returns 'cherry'

πŸ“Œ Tuples – Immutable Lists

Tuples are created with ( ) (parentheses). They cannot be changed after creation.

days = ("Mon", "Tue", "Wed")
coordinates = (10, 20)

# days.append("Thu") would cause an error – tuples are immutable!

Use tuples for data that should never change (e.g., weekdays, fixed settings).

⚠️ Common Mistakes

❌ Trying to change a tuple:

Tuples are immutable – you cannot add, remove, or change items after creation.

days = ("Mon", "Tue")
days[0] = "Sun"  # ❌ TypeError: 'tuple' object does not support item assignment

❌ Forgetting that list indexing starts at 0:

The first item in a list or tuple is at index 0, not 1.

fruits = ["apple", "banana", "cherry"]
print(fruits[1])  # "banana" – index 1 is the second item

πŸ’‘ Pro Tips

Use lists for data that changes – tuples for fixed data.

Use lists when you need to add, remove, or update items. Use tuples for fixed data like days of the week, coordinates, or configuration settings.

Use tuple unpacking for cleaner code.

You can unpack a tuple into multiple variables in one line.

coordinates = (10, 20)
x, y = coordinates
print(x)  # 10
print(y)  # 20

✨ Challenge: Build a Shopping List

Create a list called shopping with 3 items. Then:

  1. Append a fourth item.
  2. Remove the second item.
  3. Print the final list.

πŸ“š What's Next?

After mastering lists and tuples, you're ready to learn:

πŸ“€ Share This Lesson

Help others learn Python – share this lesson!

❀️ Support Free Education

This course is 100% free. If it helps you, consider buying me a coffee.

β˜• Buy Me a Coffee
← Back to Python Course Hub