A travel checklist holds items that will be modified throughout a trip.
What You Need to Understand
A list is an ordered, mutable sequence that may contain repeated values and mixed object types. Square brackets create a list; indexes access individual elements starting at zero.
When to Use It
A travel checklist holds items that will be modified throughout a trip.
Step-by-Step Python Example
travel_items = ["passport", "charger", "passport"]
print(travel_items[0])
print(len(travel_items))
travel_items[1] = "adapter"
print(travel_items)
How the Example Works
The first index is zero; duplicates remain in the list. Assigning to an index replaces exactly one element.
Example Output
passport
3
['passport', 'adapter', 'passport']
River
A Second Example
Compare this smaller variation with the main example, then experiment with your own values.
books = ["Atlas", "Orbit", "River"]
print(books[-1])
Important Behavior and Edge Cases
Accessing an index outside the list raises IndexError. Prefer descriptive item names and consistent types when possible.
Try It Yourself
Create a three-item reading list and replace the second entry.
Common Beginner Mistakes
- Accessing an index outside the list raises
IndexError. Prefer descriptive item names and consistent types when possible. - Assuming every sequence operation modifies the original data or that a tuple makes nested mutable objects immutable.
Key Takeaways
- A list is an ordered, mutable sequence that may contain repeated values and mixed object types.
- The first index is zero; duplicates remain in the list.
- Accessing an index outside the list raises
IndexError.