Python Basics: Your First Steps Into Programming
Lists and Tuples
Lesson 9 of 16 • 10 XP
Keep your place in this quest
Log in or sign up for free to subscribe, follow lesson progress, and access more learning content.
In Python, there are two basic data structures for storing collections of items: lists and tuples.
Both allow you to store multiple values in a single variable, but they have an important difference — lists can change, tuples cannot.
Let’s break them down.
Lists in Python
A list is a mutable collection of items.
Mutable means you can add, remove, and modify items at any time. Lists are written with square brackets [], and their items are separated by commas.
Example — creating a list:
fruits = ["apple", "banana", "orange"]
print(fruits)
# Output: ["apple", "banana", "orange"]
1.1. Accessing Items by Index
In Python, lists (and most sequences) start counting at index 0.
That means the first item in the list is at position 0, the second at 1, and so on.
print(fruits[0])
# Output: "apple"
TIP: Negative indexes count from the end: fruits[-1] → "orange"
1.2. Slicing Lists
Just like strings, you can slice a list to get a range of items:
print(fruits[0:2])
# Output: ["apple", "banana"]
The slice [start:end] includes the start index, but stops right before the end index.
1.3. Adding Items
Use .append() to add an item at the end of the list:
fruits.append("pineapple")
print(fruits)
# Output: ["apple", "banana", "orange", "pineapple"]
1.4. Removing Items
Use .remove() to delete the first occurrence of a specific value:
fruits.remove("orange")
print(fruits)
# Output: ["apple", "banana", "pineapple"]
1.5. Checking if an Item Exists
The in operator lets you see if a value is present in the list:
has_orange = "orange" in fruits
print(has_orange) # False
You can use this inside a function:
def is_fruit(item):
if item in fruits:
print(f"{item} is a fruit!")
else:
print(f"{item} is NOT a fruit!")
is_fruit("horse")
is_fruit("apple")
1.6. Modifying Items
Since lists are mutable, you can overwrite an existing value:
fruits[0] = "green apple"
print(fruits)
IMPORTANT!: Lists are great when you need flexibility. But if your collection of items should never change, use a tuple instead.
Tuples in Python
A tuple is very similar to a list, but it’s immutable. Immutable means that once it’s created, you cannot add, remove, or modify its elements.
Tuples are written with parentheses () and their items are separated by commas:
example = (1, 2, 3, 4)
print(example)
Note: Tuples can still contain mutable objects like lists inside them — but the tuple structure itself (which items it contains) cannot be changed.
Wrapping Up
- Lists: mutable, use
[], great for collections that will change. - Tuples: immutable, use
(), perfect for fixed data.
In the next lesson, we’ll explore loops — the tool you’ll use to go through each item in these collections and do something with them.