Four containers
Most programs juggle groups of things: the items in an order, the price of each dish, the guests who have already arrived. Python builds four collections into the language itself, each with its own literal syntax, and choosing the right one makes code shorter and faster.
- A list […] — an ordered sequence you can change.
- A tuple (…) — an ordered sequence you can't change; a fixed record.
- A dictionary {key: value} — look up a value by its key.
- A set {…} — unique values, fast in checks.
Lists and tuples
Lists index and slice exactly like strings, but they're mutable: append(x) adds to the end, insert(i, x) puts x at position i, pop() removes and returns the last item, remove(x) deletes the first x, and items[0] = "tea" replaces one. sorted(items) returns a new sorted list, while items.sort() sorts in place and returns None.
order = ["momo", "chiya"]
order.append("samosa")
order[1] = "lassi"
print(order, len(order))
dish = ("momo", 180)
name, price = dish
print(name, price)A tuple is a list you promise not to change. Use tuples for small fixed records — a coordinate (x, y), a (name, price) pair — and for returning several values from a function: return total, count returns a tuple, which the caller unpacks. A one-item tuple needs a trailing comma: ("momo",). Swapping two names is just a, b = b, a.