Lists
One Variable, Many Values
So far, every variable has held exactly one value. name = "Rahul". price = 49.99. One label, one box.
But programs frequently deal with collections. A shopping cart doesn't have one item — it has several. A classroom doesn't have one student — it has thirty. A week doesn't have one day — it has seven.
You could create a separate variable for each:
day1 = "Monday"
day2 = "Tuesday"
day3 = "Wednesday"
# and so on...
But this falls apart immediately. What if you don't know in advance how many you'll have? What if there are a thousand?
Python solves this with a list — a single variable that holds multiple values in sequence.
Creating a List
days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday"]
Square brackets. Values separated by commas. That's it.
Python prints the entire list at once, brackets and all. You can put any type of value into a list — strings, integers, floats, booleans, or even a mix.
Accessing Items by Index
Printing the whole list is fine, but usually you want a specific item. You access items by their index — their position in the list.
The critical rule: Python starts counting from zero.
fruits = ["apple", "banana", "cherry"]
# 0 1 2
The index goes inside square brackets after the list name. fruits[0] means: go to the list called fruits, take out the item at position 0.
Zero-based indexing is one of the things that trips up new programmers most often. It becomes completely natural with practice — but for now, just remember: the first item is always at index 0.
Negative Indexing
Python also lets you count from the end of the list using negative numbers.
-1 is always the last item, -2 is the second to last, and so on. This is genuinely useful — you often want the last item in a list without knowing how long the list is.
Changing Items
Lists are mutable — you can change their contents after creating them.
colours[1] = "yellow" means: go to position 1 in this list and replace whatever is there with "yellow". The list itself stays, only that one item changes.
Finding the Length
len() tells you how many items a list contains.
This is useful constantly — whenever you need to know how many items you're working with.
Adding and Removing Items
Lists grow and shrink. The two most common operations:
append()adds one item to the end of the list.remove()finds the first occurrence of a value and removes it.
Notice the dot notation: cart.append(...). This is how you call functions that belong to a specific object — in this case, the list. You'll see this pattern often.
Slicing — Taking a Portion
Sometimes you want a portion of a list rather than a single item. This is called slicing.
The slice syntax is list[start:stop]. The start index is included. The stop index is not — the slice goes up to but not including it.
When start is omitted, Python begins from the first item. When stop is omitted, Python goes all the way to the last item.
Checking if Something Is in a List
The in keyword lets you ask whether a value exists in a list.
This returns True or False — making it perfect to use directly inside an if statement.
What You Have Learned
A list lets you collect multiple values under a single name and work with them individually or all at once.
The key ideas:
- A list is created with square brackets:
[item1, item2, item3] - Items are accessed by index — and indexing starts at 0
- Negative indices count from the end:
-1is the last item - Lists are mutable — you can change, add, and remove items
len()gives the number of items in a listappend()adds to the end;remove()removes the first match- Slicing extracts a portion:
list[start:stop] inchecks whether a value exists in a list — returnsTrueorFalse
In the next lesson, you'll learn how to loop through a list — running the same block of code once for every item, automatically.