DAY-6 OF LEARNING PYTHON

 Yo, Day 6, let’s unlock Tuples & Sets! 🔓

Heyy, you’re unstoppable! You’ve crushed Day 1 (Hello World), Day 2 (variables), Day 3 (operators), Day 4 (strings), and Day 5 (lists). Now, Day 6 is about Tuples and Sets — two more collection types that make your code smarter. Tuples are like lists but unchangeable (great for fixed data), and sets are like unique bags (no duplicates, perfect for quick checks). This is a 1–2 hour hands-on session, written like we’re brainstorming over snacks. Open VS Code, create day6.py, and let’s jump in. Questions? Fire away!


Step 1: What Are Tuples? (10 mins)

Tuples are ordered, immutable collections — use parentheses (). Once created, you can’t change them (no add/remove/replace).

python
colors = ("red", "green", "blue")
coords = (10, 20, 30)
mixed = (1, "hello", True)

Key Points:

  • Access with index: colors[0] → "red".
  • Slicing works: colors[1:3] → ('green', 'blue').
  • Length: len(colors) → 3.
  • Single item tuple: single = (5,) (note the comma!).
  • Empty: empty = ().

Why Tuples? Safe for constants (e.g., days of week).

Action: Skim this for 5 mins → GeeksforGeeks: Tuples. Focus on “Creating Tuples” and “Accessing Elements”.


Step 2: Working with Tuples (15 mins)

Indexing & Slicing (like lists):

python
days = ("Mon", "Tue", "Wed", "Thu", "Fri")
print(days[0])      # Mon
print(days[-1])     # Fri
print(days[1:4])    # ('Tue', 'Wed', 'Thu')

Can't Change (immutable):

python
# days[1] = "Tuesday"  # TypeError! Can't do this

Unpacking: Assign values directly:

python
x, y, z = (1, 2, 3)
print(x, y, z)  # 1 2 3

Operations:

  • Concatenate: tup1 + tup2
  • Repeat: tup * 2
  • Check: item in tup

Try This: In day6.py:

python
point = (5, 10)
print("X:", point[0], "Y:", point[1])
new_point = point + (15,)
print("Extended:", new_point)
print("Has 10?", 10 in point)

Run: python day6.py Output:

text
X: 5 Y: 10
Extended: (5, 10, 15)
Has 10? True

Step 3: What Are Sets? (10 mins)

Sets are unordered, mutable collections of unique items — use curly braces {}. No duplicates!

python
fruits = {"apple", "banana", "apple"}  # {'apple', 'banana'}
numbers = {1, 2, 3, 2}                 # {1, 2, 3}
empty = set()                          # Empty set ({} is dict!)

Key Points:

  • No index (unordered): Can't do fruits[0].
  • Fast lookups: item in set is quick.
  • Mutable: Can add/remove.

Action: Check GeeksforGeeks: Sets for 5 mins. Look at “Creating Sets” and “Set Operations”.


Step 4: Working with Sets (15 mins)

Adding/Removing:

  • .add(item): Add one.
  • .update(iterable): Add multiple.
  • .remove(item): Remove (error if not found).
  • .discard(item): Remove (no error if not found).
  • .pop(): Remove random item.
python
fruits = {"apple", "banana"}
fruits.add("orange")
fruits.update(["grape", "kiwi"])
print(fruits)  # {'apple', 'banana', 'orange', 'grape', 'kiwi'}

fruits.remove("banana")
fruits.discard("mango")  # No error
print(fruits.pop())      # Removes some item

Set Operations (math-like):

  • Union: set1 | set2 or set1.union(set2)
  • Intersection: set1 & set2 or set1.intersection(set2)
  • Difference: set1 - set2 or set1.difference(set2)
  • Symmetric Difference: set1 ^ set2
python
set1 = {1, 2, 3}
set2 = {2, 3, 4}
print(set1 | set2)  # {1, 2, 3, 4} (union)
print(set1 & set2)  # {2, 3} (intersection)
print(set1 - set2)  # {1} (difference)

Try This: Add to day6.py:

python
unique_nums = {1, 2, 2, 3}
print("Unique:", unique_nums)  # {1, 2, 3}
set_a = {1, 2}
set_b = {2, 3}
print("Common:", set_a & set_b)  # {2}

Output:

text
Unique: {1, 2, 3}
Common: {2}

Step 5: Mini Project — Unique Word Counter (30–40 mins)

Let’s build a Unique Word Counter! It’ll:

  1. Ask for a sentence.
  2. Split into words.
  3. Use a set to find unique words.
  4. Print unique words and count.

Code (day6_unique.py):

Unique Word Counter

sentence = input("Enter a sentence: ") words = sentence.split() # List of words unique_words = set(words) # Remove duplicates

print("Unique words:", unique_words) print("Count:", len(unique_words))

Run It:

text
Enter a sentence: hello world hello python
Unique words: {'hello', 'world', 'python'}
Count: 3

Challenge: Make words case-insensitive (convert to lower: sentence.lower()).

Extra Fun: Find common words between two sentences using intersection.


Step 6: Common Mistakes & Fixes (5 mins)

ErrorWhy?Fix
TypeErrortup[0] = 5Tuples immutable — create new tuple
KeyError.remove() on missing itemUse .discard()
Wrong empty setempty = {}Use set() ({} is dict)


Day 6 Wrap-Up

You learned:

  • Tuples: Immutable, ordered ((1, 2))
  • Sets: Unique, unordered ({1, 2})
  • Operations: Add/remove, union/intersection
  • Built a Unique Word Counter

Track Progress (in a notebook):

Day 6 Done: Tuples for fixed data, sets for uniques! Doubt: Why use sets over lists? Answer: Sets are faster for in checks and auto-remove duplicates.

Resources:

Comments

Popular Posts