Lists Are the Workhorse of Data Science
A list stores multiple values in one variable, in order. Data science in Python almost always starts with a list of numbers or strings.
temps = [72, 68, 75, 80, 65, 77, 71]
print(temps[0]) # 72 (first item, index 0)
print(temps[-1]) # 71 (last item)
print(temps[1:4]) # [68, 75, 80] (slice)
Sorting and Looping
temps.sort() # modifies list in place: [65,68,71,72,75,77,80]
# Loop with enumerate to get index AND value
for i, t in enumerate(temps):
print(f"Day {i+1}: {t}°F")
print("Average:", sum(temps)/len(temps))
💡enumerate() is Gold
enumerate(my_list) gives you tuples of (index, value) as you loop. This is far cleaner than keeping a separate counter variable.
List Comprehensions (Power Move)
# Convert Fahrenheit list to Celsius in one line
celsius = [(t - 32) * 5/9 for t in temps]
above_70 = [t for t in temps if t > 70] # filter