Lesson 10 / 47
Lists
Ordered, mutable sequences — the everyday Python collection.
Creating & changing
A list holds items in order and you can change it after creation — add, remove, or edit elements.
fruits = ["apple", "banana", "cherry"]
fruits.append("date")
fruits.insert(1, "kiwi")
fruits.remove("banana")
print(fruits)
Output:
['apple', 'kiwi', 'cherry', 'date']
Indexing & slicing
Negative indices count from the end. Slicing [start:stop:step] pulls a sub-list without a loop.
nums = [10, 20, 30, 40, 50]
print(nums[-1]) # 50
print(nums[1:4]) # [20, 30, 40]
print(nums[::-1]) # [50, 40, 30, 20, 10]List methods
Common methods: sort(), reverse(), pop(), extend(), index(), count(). Most mutate the list in place and return None.
sort() vs sorted()
sorted(nums) returns a new sorted list; nums.sort() sorts in place and returns None — a classic beginner bug.