Lesson 2: Python Lists
Lists are the workhorses of data science in Python. A list stores multiple values in a single variable, and Python gives you powerful tools to add, remove, sort, and loop through them.
Key Concepts
Creating Lists
temps = [72, 68, 75, 80, 65]
A list uses square brackets. Values are separated by commas. A list can hold numbers, strings, or even other lists.
Indexing
temps[0] gives the first item (72). temps[-1] gives the last item (65). Python counts from 0. Slicing: temps[1:3] gives [68, 75].
List Methods
temps.append(70) adds to the end. temps.sort() sorts in place. temps.reverse() flips the order. len(temps) gives the count. sum(temps) adds them all up.
Looping Through Lists
for t in temps:
print(t)
This prints every temperature. You can also use for i, t in enumerate(temps): to get both the index and the value.
🔬 Interactive Lab: List Sorter & Visualizer
Enter up to 8 numbers, then click Sort to watch the bars rearrange into order.
✅ Check Your Understanding
1. What does nums[0] return for nums = [10, 20, 30]?
2. Which method adds an item to the end of a list?
3. What does nums[-1] return?