DAY-5 OF LEARNING PYTHON
Yo, Day 5, let’s roll with Lists! 🚀
Heyy, you’re on fire! You’ve already nailed Day 1 (Hello World), Day 2 (variables), Day 3 (operators), and Day 4 (strings). Now, Day 5 is all about Lists — Python’s super flexible way to store and mess with collections of stuff (like a shopping list or a playlist). This is a 1–2 hour hands-on session, written like we’re coding together over a chai. Open VS Code, create day5.py, and let’s dive in. Got doubts? Drop ‘em, I’ll sort you out!
Step 1: What Are Lists? (10 mins)
Lists are like a bag where you can throw in numbers, strings, or even other lists. They’re ordered, mutable (you can change them), and use square brackets [].
fruits = ["apple", "banana", "orange"]
scores = [95, 80, 75]
mixed = [1, "hello", True]Key Points:
- Access items with index (starts at 0): fruits[0] → "apple".
- Change items: fruits[1] = "mango" (mutable, unlike strings!).
- Get length: len(fruits) → 3.
- Lists can be empty: empty = [].
Action: Skim this for 5 mins → W3Schools: Python Lists. Check “List Basics” and “Access Items”.
Step 2: Working with Lists (15 mins)
Let’s play with lists:
Indexing & Slicing (like strings):
fruits = ["apple", "banana", "orange", "grape"]
print(fruits[0]) # apple
print(fruits[-1]) # grape
print(fruits[1:3]) # ['banana', 'orange']
print(fruits[::-1]) # ['grape', 'orange', 'banana', 'apple']Changing Lists:
fruits[1] = "mango" # Replace banana with mango
print(fruits) # ['apple', 'mango', 'orange', 'grape']Adding Items:
- .append(item): Add to end.
- .insert(index, item): Add at specific index.
fruits.append("kiwi") # ['apple', 'mango', 'orange', 'grape', 'kiwi']
fruits.insert(1, "pear") # ['apple', 'pear', 'mango', 'orange', 'grape', 'kiwi']Removing Items:
- .pop(): Remove last item (or at index).
- .remove(item): Remove specific item.
fruits.pop() # Removes 'kiwi'
fruits.remove("mango") # Removes 'mango'
print(fruits) # ['apple', 'pear', 'orange', 'grape']Try This: In day5.py:
items = ["pen", "book", "pencil"]
print("Original:", items)
items.append("eraser")
items[1] = "notebook"
print("Modified:", items)
print("First two:", items[:2])Run: python day5.py Output:
Original: ['pen', 'book', 'pencil']
Modified: ['pen', 'notebook', 'pencil', 'eraser']
First two: ['pen', 'notebook']Step 3: List Methods & Operations (20 mins)
Lists have built-in methods to make life easy:
| Method | What it does | Example |
|---|---|---|
| .append(x) | Add x to end | lst.append(5) |
| .pop() | Remove last item | lst.pop() → 5 |
| .remove(x) | Remove first x | lst.remove(5) |
| .sort() | Sort list | lst.sort() |
| .reverse() | Reverse list | lst.reverse() |
| .count(x) | Count x in list | lst.count(5) |
| .index(x) | Find x’s index | lst.index(5) |
Code It:
numbers = [3, 1, 4, 1, 5]
numbers.sort() # [1, 1, 3, 4, 5]
print(numbers)
print("Count of 1:", numbers.count(1)) # 2
print("Index of 4:", numbers.index(4)) # 3
numbers.reverse() # [5, 4, 3, 1, 1]
print(numbers)List Operations:
- Concatenate: lst1 + lst2
- Repeat: lst * 2
- Check if item exists: x in lst
list1 = [1, 2]
list2 = [3, 4]
print(list1 + list2) # [1, 2, 3, 4]
print(list1 * 2) # [1, 2, 1, 2]
print(2 in list1) # TrueTry This: Add to day5.py:
scores = [85, 90, 70, 90]
print("Sorted:", sorted(scores)) # sorted() doesn’t change original
print("Original:", scores)
print("Has 90?", 90 in scores)Output:
Sorted: [70, 85, 90, 90]
Original: [85, 90, 70, 90]
Has 90? TrueStep 4: Mini Project — Shopping List App (30–40 mins)
Let’s build a simple shopping list! It’ll:
- Start with a few items.
- Ask user to add an item.
- Ask user to remove an item.
- Print final list and length.
Code (day5_shopping.py):
Run It:
Current list: ['milk', 'bread', 'eggs']
Add an item: butter
Remove an item: bread
Final list: ['milk', 'eggs', 'butter']
Total items: 3Challenge: Add error handling for empty input:
new_item = input("Add an item: ")
if new_item.strip():
shopping_list.append(new_item)
else:
print("Error: Item cannot be empty!")Extra Fun: Sort the list before printing (shopping_list.sort()).
Step 5: Common Mistakes & Fixes (5 mins
| Error | Why? | Fix |
|---|---|---|
| IndexError | lst[10] (beyond length) | Check len(lst) |
| ValueError | lst.remove("x") (not in list) | Use if x in lst |
| Wrong sort | .sort() on mixed types | Ensure same type (e.g., all numbers) |
Day 5 Wrap-Up
You learned:
- Create and modify lists (append, remove, etc.)
- Index and slice lists (lst[0], lst[1:3])
- Use methods like .sort(), .count(), .index()
- Built a Shopping List App
Track Progress (in a notebook):
Day 5 Done: Lists are dope! Built a shopping app. Doubt: Why does .sort() change the list but sorted() doesn’t? Answer: .sort() modifies in-place; sorted() returns a new list.
Resources:
- W3Schools: Lists
- RealPython: Lists
- YouTube: Search “Python Lists freeCodeCamp” (first 10–15 mins)



Comments
Post a Comment