Quest 1 β’ Lesson 3
π¦ Lists & Tuples
A list is a collection of items that can be changed (mutable). A tuple is like a list but cannot be changed (immutable). Both are fundamental for storing groups of data.
mixed = [10, "hello", True, 3.14]
print(fruits) # Output: ['apple', 'banana', 'cherry']
print(fruits[0]) # Output: apple
π§ List Basics
- Lists are created with
[ ](square brackets). - Items are indexed starting from
0β first item isfruits[0]. - You can store different data types in one list.
π οΈ Useful List Methods
π Tuples β Immutable Lists
Tuples are created with ( ) (parentheses). They cannot be changed after creation.
coordinates = (10, 20)
# days.append("Thu") would cause an error β tuples are immutable!
Use tuples for data that should never change (e.g., weekdays, fixed settings).
β¨ Challenge: Build a Shopping List
Create a list called shopping with 3 items. Then:
1. Append a fourth item.
2. Remove the second item.
3. Print the final list.
shopping = ["milk", "bread", "eggs"]
shopping.append("butter")
shopping.remove("bread")
print(shopping)
π Try It Mentally
What would be the output of this code?
numbers.append(4)
print(numbers[2])
Reveal answer
Output: 3 (index 2 is the third item)
β€οΈ Support Free Education
This course is 100% free. If it helps you, consider buying me a coffee.
β Buy Me a Coffeeβ‘οΈ Ready for more?
Next lesson: Conditional Statements (if/else) β control the flow of your program.
Continue to Lesson 1.4 β(Coming soon β check back or buy Pro Pack for instant access)