What are lists?
Lists are Python’s most versatile data structure. They’re like containers that can hold multiple items in a specific order. Think of a list like:- A shopping list (milk, eggs, bread)
- A to-do list (tasks in order)
- A playlist (songs in sequence)
Creating lists
Lists use square brackets
[] and items are separated by commas. You can mix different data types in the same list!Accessing items
Lists are indexed starting at 0:Changing lists
Lists are mutable - you can change them:List methods
Inserting and Removing Elements
append(val): Appends an item to the end of the list.insert(idx, val): Inserts an item at a specific index.pop(): Removes and returns the last item in the list.pop(idx): Removes and returns the item at a specific index.
Sorting: sort() vs sorted()
list.sort(): Sorts the list in-place (mutates the original list; returnsNone).sorted(list): A built-in function that returns a new sorted list, leaving the original list unchanged.
Mathematical Operations: sum(), min(), max()
For lists containing numeric values, Python provides helpful built-in mathematical functions:
Checking lists
Common mistakes
Index out of range
Index out of range
Modifying while looping
Modifying while looping
Shallow copy issues
Shallow copy issues
Practice & Exercises
To reinforce what you’ve learned in this section (Lists, indexing, slicing, and operations), practice with these interactive notebooks:Follow-Along Practice
Practice initializing lists, accessing elements, slicing, and list manipulation.💻 VS Code | 🚀 Colab | 📥 Download
Practice Exercises
Test your knowledge with hands-on exercises on shopping lists and list slicing.💻 VS Code | 🚀 Colab | 📥 Download
What’s next?
Now let’s learn about Tuples - perfect for storing ordered data that should never change!Tuples
Learn about immutable collections