![]() |
|
CHAPTER 12 — Lists: Storing Multiple Items - Printable Version +- The Lumin Archive (https://theluminarchive.co.uk) +-- Forum: The Lumin Archive — Core Forums (https://theluminarchive.co.uk/forumdisplay.php?fid=3) +--- Forum: Courses — Structured Learning (https://theluminarchive.co.uk/forumdisplay.php?fid=69) +---- Forum: Beginner’s Guide to Coding — Python From Zero to Skill (https://theluminarchive.co.uk/forumdisplay.php?fid=72) +---- Thread: CHAPTER 12 — Lists: Storing Multiple Items (/showthread.php?tid=230) |
CHAPTER 12 — Lists: Storing Multiple Items - Leejohnston - 11-15-2025 Chapter 12 — Lists: Storing Multiple Items Lists let you store many values in one variable — the foundation of real-world coding. A list is like a container that holds multiple items: Code: fruits = ["apple", "banana", "cherry"]Each item has an index: • apple → index 0 • banana → index 1 • cherry → index 2 --- 12.1 Creating Lists Code: numbers = [10, 20, 30]Python allows mixed types — but good practice is to keep lists consistent. --- 12.2 Accessing Items Use square brackets: Code: fruits = ["apple", "banana", "cherry"]Indexes start at 0. --- 12.3 Changing Items Code: fruits[1] = "mango"--- 12.4 Adding Items append() → add to the end: Code: fruits.append("orange")insert() → add to a specific position: Code: fruits.insert(1, "kiwi")--- 12.5 Removing Items remove() → remove by value: Code: fruits.remove("banana")pop() → remove by index: Code: fruits.pop(0) # removes first itemRemove last item: Code: fruits.pop()--- 12.6 Checking Length Code: print(len(fruits))--- 12.7 Looping Through a List Code: for fruit in fruits:--- 12.8 Slicing Lists Code: numbers = [10, 20, 30, 40, 50]--- 12.9 Sorting Lists Sort alphabetically or numerically: Code: numbers.sort()Reverse: Code: numbers.reverse()--- 12.10 Checking for an Item Code: if "apple" in fruits:--- 12.11 Copying Lists (Important) If you want a REAL copy: Code: new_list = old_list.copy()If you do this: Code: new_list = old_listThey become linked — changing one changes the other. --- 12.12 List of Lists Lists can contain other lists: Code: matrix = [Access: Code: print(matrix[1][2]) # 6--- 12.13 Mini Project — To-Do List Write a program that: • starts with an empty list • asks the user for 3 tasks • appends them • prints the final list --- 12.14 Challenge — Shopping Cart System Ask the user to enter items until they type “done”. Store them in a list. Then: • display number of items • sort the list • print each item on a new line Example: Code: Your cart contains:--- 12.15 Chapter Summary • lists store multiple values • indexes start at 0 • append() adds to the end • remove() and pop() delete items • slices extract parts of lists • sort(), reverse(), and len() are essential • loops work beautifully with lists • lists can store ANY type Next: Chapter 13 — Dictionaries: Storing Information With Keys Dictionaries let you store data in a structured way, just like a database. --- Written and Compiled by Lee Johnston — Founder of The Lumin Archive |