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 [].

python
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):

python
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:

python
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.
python
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.
python
fruits.pop()          # Removes 'kiwi'
fruits.remove("mango") # Removes 'mango'
print(fruits)         # ['apple', 'pear', 'orange', 'grape']

Try This: In day5.py:

python
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:

text
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:

MethodWhat it doesExample
.append(x)Add x to endlst.append(5)
.pop()Remove last itemlst.pop() → 5
.remove(x)Remove first xlst.remove(5)
.sort()Sort listlst.sort()
.reverse()Reverse listlst.reverse()
.count(x)Count x in listlst.count(5)
.index(x)Find x’s indexlst.index(5)

Code It:

python
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
python
list1 = [1, 2]
list2 = [3, 4]
print(list1 + list2)  # [1, 2, 3, 4]
print(list1 * 2)      # [1, 2, 1, 2]
print(2 in list1)     # True

Try This: Add to day5.py:

python
scores = [85, 90, 70, 90]
print("Sorted:", sorted(scores))  # sorted() doesn’t change original
print("Original:", scores)
print("Has 90?", 90 in scores)

Output:

text
Sorted: [70, 85, 90, 90]
Original: [85, 90, 70, 90]
Has 90? True

Step 4: Mini Project — Shopping List App (30–40 mins)

Let’s build a simple shopping list! It’ll:

  1. Start with a few items.
  2. Ask user to add an item.
  3. Ask user to remove an item.
  4. Print final list and length.

Code (day5_shopping.py):

day5_shopping.py
python

Run It:

text
Current list: ['milk', 'bread', 'eggs']
Add an item: butter
Remove an item: bread
Final list: ['milk', 'eggs', 'butter']
Total items: 3

Challenge: Add error handling for empty input:

python
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

ErrorWhy?Fix
IndexErrorlst[10] (beyond length)Check len(lst)
ValueErrorlst.remove("x") (not in list)Use if x in lst
Wrong sort.sort() on mixed typesEnsure 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:

Comments

Popular Posts