diff --git a/homework_solutions/6.md_hw_solution.py b/homework_solutions/6.md_hw_solution.py new file mode 100644 index 0000000..071870c --- /dev/null +++ b/homework_solutions/6.md_hw_solution.py @@ -0,0 +1,85 @@ +# Homework: Tuples and Sets in Python + +# ------------------------------- +# 1️. Tuple Operations +# ------------------------------- + +# Create a tuple with 5 elements +my_tuple = (10, 20, 30, 40, 50) +print("Original tuple:", my_tuple) + +# Try to modify one of the elements (This will raise an error) +try: + my_tuple[1] = 100 +except TypeError as e: + print("Error when trying to modify tuple:", e) + +# Perform slicing on the tuple to extract the second and third elements +sliced_tuple = my_tuple[1:3] +print("Sliced tuple (2nd and 3rd elements):", sliced_tuple) + +# Concatenate the tuple with another tuple +another_tuple = (60, 70) +combined_tuple = my_tuple + another_tuple +print("Concatenated tuple:", combined_tuple) + + +# ------------------------------- +# 2️. Set Operations +# ------------------------------- + +# Create two sets of favorite fruits +my_fruits = {"apple", "banana", "cherry"} +friends_fruits = {"mango", "banana", "grape"} + +# Find union, intersection, and difference +print("\nUnion of fruits:", my_fruits | friends_fruits) +print("Intersection of fruits:", my_fruits & friends_fruits) +print("Difference (my fruits - friend's):", my_fruits - friends_fruits) + +# Add a new fruit to your set +my_fruits.add("orange") +print("After adding orange:", my_fruits) + +# Remove a fruit using remove() and discard() +my_fruits.remove("banana") # removes banana +print("After remove('banana'):", my_fruits) + +# discard() doesn’t raise an error if fruit doesn’t exist +my_fruits.discard("pineapple") +print("After discard('pineapple') (no error even if not present):", my_fruits) + +# Try removing non-existing fruit using remove() → raises error +try: + my_fruits.remove("pineapple") +except KeyError as e: + print("Error when using remove() on non-existing element:", e) + + +# ------------------------------- +# 3️. Tuple and Set Comparison +# ------------------------------- + +# Create a list of elements +my_list = [1, 2, 3, 3, 2] + +# Convert it into both a tuple and a set +my_tuple_from_list = tuple(my_list) +my_set_from_list = set(my_list) + +print("\nTuple from list:", my_tuple_from_list) +print("Set from list:", my_set_from_list) + +# Try adding new elements +# Tuples are immutable, sets are mutable +try: + my_tuple_from_list += (4,) + print("Tuple after adding element using +:", my_tuple_from_list) +except TypeError as e: + print("Error adding element to tuple:", e) + +my_set_from_list.add(4) +print("Set after adding element:", my_set_from_list) + +# Observation: Tuples allow duplicates and are immutable. +# Sets remove duplicates and are mutable. diff --git a/homework_solutions/7.md_hw_solution.py b/homework_solutions/7.md_hw_solution.py new file mode 100644 index 0000000..3b8f2ca --- /dev/null +++ b/homework_solutions/7.md_hw_solution.py @@ -0,0 +1,35 @@ +karnataka_dishes = { + "Bengaluru": "Bisi Bele Bath", + "Mysuru": "Mysore Pak", + "Mangaluru": "Neer Dosa", + "Hubballi": "Girmit", + "Udupi": "Masala Dosa" +} + + +karnataka_dishes["Shivamogga"] = "Kadubu" + + +karnataka_dishes["Bengaluru"] = "Ragi Mudde" + +del karnataka_dishes["Udupi"] + + +print("Cities:", karnataka_dishes.keys()) + +print("Dishes:", karnataka_dishes.values()) + +#Nested Dictionary Practice +friends = { + "Ananya": { + "favorite_subject": "Maths", + "favorite_food": "Paneer Butter Masala" + }, + "Rahul": { + "favorite_subject": "Science", + "favorite_food": "Masala Dosa" + } +} + + +print("Rahul's favorite food is:", friends["Rahul"]["favorite_food"]) diff --git a/homework_solutions/python_basics_1_homework.py b/homework_solutions/python_basics_1_homework.py new file mode 100644 index 0000000..2f6c449 --- /dev/null +++ b/homework_solutions/python_basics_1_homework.py @@ -0,0 +1,22 @@ +# Homework Solutions for Python Basics - 1 + +# 1️⃣ Arithmetic Practice +a = 10 +b = 5 + +print("Addition:", a + b) +print("Subtraction:", a - b) +print("Multiplication:", a * b) +print("Division:", a / b) + +# 2️⃣ Swap Two Variables (using third variable) +x = 5 +y = 10 +temp = x +x = y +y = temp +print("After swapping using third variable:", x, y) + +# 3️⃣ Swap without third variable +x, y = y, x +print("After swapping without third variable:", x, y) diff --git a/notes/2.md b/notes/2.md index fc6a86e..8991218 100644 --- a/notes/2.md +++ b/notes/2.md @@ -114,6 +114,15 @@ print(x) # Output: 10 --- + + +### **🧩 Quiz Time: Test Your Knowledge!** +You’ve learned about Variables, Data Types, and Operators in Python — now test your understanding! 🎯 +Click the link below to take a short interactive quiz: +👉 [Take the Python Basics - 1 Quiz] () +(Add a post of your quiz marks too and use #engineeringinkannada and mention me.) + + ### **YouTube Reference** Watch the following YouTube video from my channel: - [Watch the tutorial on YouTube](https://youtu.be/jr1FF9-VAJs?si=gBFha6Rq_ae3i16r) diff --git a/notes/homework_solutions/3.md_hw_solution.py b/notes/homework_solutions/3.md_hw_solution.py new file mode 100644 index 0000000..2b2c1c2 --- /dev/null +++ b/notes/homework_solutions/3.md_hw_solution.py @@ -0,0 +1,47 @@ +# 🧩 3.md — Homework Solutions + +# ----------------------------- +# Simple Greeting Program +# ----------------------------- +name = input("Enter your name: ") +age = int(input("Enter your age: ")) # converting input to integer + +# Using + operator +print("Hello, " + name + "! You are " + str(age) + " years old.") + +# Using f-string +print(f"Hello, {name}! You are {age} years old.") + + +# ----------------------------- +# String Manipulation Exercise +# ----------------------------- +sentence = input("Enter a sentence: ") + +# Applying string methods +uppercase_sentence = sentence.upper() +lowercase_sentence = sentence.lower() +replaced_sentence = sentence.replace(" ", "_") +stripped_sentence = sentence.strip() + +print("Uppercase:", uppercase_sentence) +print("Lowercase:", lowercase_sentence) +print("Replaced:", replaced_sentence) +print("Stripped:", stripped_sentence) + + +# ----------------------------- +# Character Counter +# ----------------------------- +text = input("Enter a string: ") +count = len(text.replace(" ", "")) # removes spaces and counts remaining characters + +print(f"Number of characters (excluding spaces): {count}") + + +# ----------------------------- +# Escape Sequence Practice +# ----------------------------- +print("Hello\n\tWorld") +print("This is a backslash: \\") + diff --git a/notes/homework_solutions/4.md_hw_solution.py b/notes/homework_solutions/4.md_hw_solution.py new file mode 100644 index 0000000..5cb5c82 --- /dev/null +++ b/notes/homework_solutions/4.md_hw_solution.py @@ -0,0 +1,72 @@ +# Operators in Python — Homework Solutions + +# ----------------------------------------------- +# 1. Logical Operator Practice +# ----------------------------------------------- + +# Take two numbers as input +num1 = int(input("Enter the first number: ")) +num2 = int(input("Enter the second number: ")) + +# Both greater than 10 +if num1 > 10 and num2 > 10: + print("✅ Both numbers are greater than 10.") +else: + print("❌ Both numbers are not greater than 10.") + +# At least one less than 5 +if num1 < 5 or num2 < 5: + print("✅ At least one number is less than 5.") +else: + print("❌ Neither number is less than 5.") + +# The first is not greater than the second +if not (num1 > num2): + print("✅ The first number is not greater than the second.") +else: + print("❌ The first number is greater than the second.") + + +# ----------------------------------------------- +# 2. Comparison Operator Challenge +# ----------------------------------------------- + +age = int(input("\nEnter your age: ")) + +if age >= 18: + print("You are an adult.") +else: + print("You are a minor.") + + +# ----------------------------------------------- +# 3. Membership Operator Exercise +# ----------------------------------------------- + +text = input("\nEnter a string: ") + +# Check if 'a' is in the string +if 'a' in text: + print("✅ The letter 'a' is present in the string.") +else: + print("❌ The letter 'a' is not present in the string.") + +# Check if "Python" is not in the string +if "Python" not in text: + print("✅ The word 'Python' is not found in the string.") +else: + print("❌ The word 'Python' is found in the string.") + + +# ----------------------------------------------- +# 4. Bitwise Operator Task +# ----------------------------------------------- + +a = int(input("\nEnter first integer (a): ")) +b = int(input("Enter second integer (b): ")) + +print("\nBitwise AND (a & b):", a & b) +print("Bitwise OR (a | b):", a | b) +print("Bitwise XOR (a ^ b):", a ^ b) +print("Left shift a by 2 positions (a << 2):", a << 2) +print("Right shift b by 1 position (b >> 1):", b >> 1) diff --git a/notes/homework_solutions/5.md_hw_solution.py b/notes/homework_solutions/5.md_hw_solution.py new file mode 100644 index 0000000..2079e9c --- /dev/null +++ b/notes/homework_solutions/5.md_hw_solution.py @@ -0,0 +1,38 @@ +# 🧩 Homework Solutions for 5.md (Lists in Python) + +# ----------------------------------------- +# 1️⃣ List Manipulation Exercise +# ----------------------------------------- + +# Step 1: Create a list of 5 items +items = ["apple", "banana", "cherry", "date", "fig"] +print("Original list:", items) + +# Step 2: Add a new item to the end +items.append("grape") +print("After adding an item at the end:", items) + +# Step 3: Add another item at the second position +items.insert(1, "kiwi") +print("After inserting 'kiwi' at second position:", items) + +# Step 4: Remove the third item from the list +items.pop(2) +print("After removing the third item:", items) + + +# ----------------------------------------- +# 2️⃣ Reverse and Sort a List +# ----------------------------------------- + +# Step 1: Create a list of numbers +numbers = [10, 3, 7, 2, 9, 1] +print("\nOriginal numbers list:", numbers) + +# Step 2: Sort the list in descending order +numbers.sort(reverse=True) +print("Sorted in descending order:", numbers) + +# Step 3: Reverse the sorted list +numbers.reverse() +print("After reversing the sorted list:", numbers) diff --git a/notes/homework_solutions/python_basics_1_homework.py b/notes/homework_solutions/python_basics_1_homework.py new file mode 100644 index 0000000..2f6c449 --- /dev/null +++ b/notes/homework_solutions/python_basics_1_homework.py @@ -0,0 +1,22 @@ +# Homework Solutions for Python Basics - 1 + +# 1️⃣ Arithmetic Practice +a = 10 +b = 5 + +print("Addition:", a + b) +print("Subtraction:", a - b) +print("Multiplication:", a * b) +print("Division:", a / b) + +# 2️⃣ Swap Two Variables (using third variable) +x = 5 +y = 10 +temp = x +x = y +y = temp +print("After swapping using third variable:", x, y) + +# 3️⃣ Swap without third variable +x, y = y, x +print("After swapping without third variable:", x, y)