4. Working with Lists
1. Simple Definition & Meaning
Now that you have a list, how do you use it? Working with lists means you can add things to it, remove things, or look at every single item one by one (called looping).
2. Real-World Examples
- Example 1: Adding to a list is like putting a new toy into your toy box. In Python, we say
append(). - Example 2: Looping through a list is like a teacher reading out the attendance register name by name until everyone is called.
3. Important Points
- Use
.append("item")to add an item to the end of the list. - Use a
forloop to do something with every item. Example:for toy in toy_box: print(toy). - Indentation (spaces at the start of the line) is super important in loops!
📝 Exam Notes (Quick Revision)
- Appending:
list.append(value)adds to the end. - Sorting:
list.sort()sorts permanently,sorted(list)sorts temporarily. - For Loops: Used to iterate over a sequence. Watch out for indentation errors!