🎉 Welcome to PyVerse! Start Learning Today

Lists and Tuples

Friendly intro

Welcome to PyVerse! Today we'll learn about two super-useful ways to store many values in one place: lists and tuples. Think of a list like a backpack you can open, rearrange, and add things to. A tuple is more like a sealed box: you can look inside, but you don't change what's in it.

What you will learn

  • What lists and tuples are
  • How to create them
  • How to get items from them
  • How to add/remove items (lists) and why tuples can't change
  • When to use each one

1) Lists: your flexible backpack

A list stores items in order and can be changed (mutable).

Create a list:

fruits = ["apple", "banana", "cherry"] print(fruits)

Get items by index (positions start at 0):

print(fruits[0]) # "apple" print(fruits[2]) # "cherry"

Change an item:

fruits[1] = "blueberry" print(fruits) # ["apple", "blueberry", "cherry"]

Add items:

fruits.append("mango") # add at the end fruits.insert(1, "orange") # add at a position print(fruits)

Remove items:

fruits.remove("apple") # remove by value (first match) popped = fruits.pop() # remove last and return it print("Popped:", popped) print(fruits)

Check length and membership:

print(len(fruits)) # how many items print("mango" in fruits) # True/False

Slice (get a part of the list):

numbers = [10, 20, 30, 40, 50] print(numbers[1:4]) # [20, 30, 40] print(numbers[-2:]) # last two: [40, 50]

Loop through a list:

for item in fruits: print(item)

2) Tuples: your sealed box

A tuple stores items in order but cannot be changed (immutable).

Create a tuple:

point = (3, 5) print(point[0]) # 3 print(point[1]) # 5

Trying to change a tuple causes an error:

# point[0] = 7 # TypeError: 'tuple' object does not support item assignment

When are tuples useful?

  • Things that should not change: days of the week, fixed coordinates, RGB colors.
  • They can be a bit faster and safer because they don't change.

Unpacking tuples (handy trick):

x, y = point print("x:", x, "y:", y)

3) Lists vs Tuples (quick compare)

  • List: [ ] brackets, can change (add/remove/modify).
  • Tuple: ( ) parentheses, cannot change after creation.
  • Use lists for collections that will change.
  • Use tuples for fixed data you want to keep safe.

4) Converting between them

Make a tuple from a list:

fruits = ["apple", "banana"] fruits_t = tuple(fruits) print(fruits_t)

Make a list from a tuple:

coords = (10, 20, 30) coords_list = list(coords) coords_list.append(40) print(coords_list)

Mini exercise: List and Tuple challenge (5–8 minutes)

  1. Make a list named backpack with items: "notebook", "pen", "snack".
  2. Add "water bottle" to the end. Then insert "pencil" at position 1.
  3. Remove "snack". Print the backpack and its length.
  4. Make a tuple named start_pos with values (0, 0). Try to change start_pos[0] to 5. What happens?
  5. Convert start_pos to a list, change the first value to 5, then convert it back to a tuple and print it.

Hints:

  • Use append, insert, remove, len
  • Use tuple() and list() to convert

Example solution (peek only after trying!):

backpack = ["notebook", "pen", "snack"] backpack.append("water bottle") backpack.insert(1, "pencil") backpack.remove("snack") print(backpack) print("Items:", len(backpack)) start_pos = (0, 0) # start_pos[0] = 5 # This would cause a TypeError pos_list = list(start_pos) pos_list[0] = 5 start_pos = tuple(pos_list) print(start_pos)

Summary

  • Lists use [ ] and can change. Great for things that grow or shrink.
  • Tuples use ( ) and cannot change. Great for fixed, safe data.
  • You can access items by index, loop through them, slice parts, and convert between lists and tuples when needed.

You're now ready to organize data like a pro with lists and tuples!

Loading quizzes...