From 93f0760529a835b9d136edfe9be8bd494f4e8d1a Mon Sep 17 00:00:00 2001 From: Krish Kagra Date: Fri, 14 Aug 2026 11:58:38 +0530 Subject: [PATCH] CaseStudies on python topics by Krish Kagra --- .../01_For_Loop.md | 86 +++++++++++ .../02_While_Loop.md | 82 +++++++++++ .../loops-and-control-statements/03_Break.md | 87 +++++++++++ .../04_Continue.md | 85 +++++++++++ .../loops-and-control-statements/05_Pass.md | 88 ++++++++++++ .../06_If_Else_Loop.md | 92 ++++++++++++ .../07_Nested_Loop.md | 88 ++++++++++++ .../krish-kagra/operators/01_Arithmetic_Op.md | 115 +++++++++++++++ .../krish-kagra/operators/02_Comparison_Op.md | 111 ++++++++++++++ .../krish-kagra/operators/03_Logical_Op.md | 107 ++++++++++++++ .../krish-kagra/operators/04_Assignment_Op.md | 115 +++++++++++++++ .../krish-kagra/operators/05_Membership_Op.md | 111 ++++++++++++++ .../krish-kagra/operators/06_Identity_Op.md | 109 ++++++++++++++ .../krish-kagra/operators/07_Bitwise_Op.md | 112 +++++++++++++++ .../operators/08_Operator_Precedence.md | 136 ++++++++++++++++++ .../why-python/01_History_Python.md | 66 +++++++++ .../krish-kagra/why-python/02_What_Python.md | 86 +++++++++++ .../krish-kagra/why-python/03_Why_Python.md | 87 +++++++++++ .../krish-kagra/why-python/04_Features.md | 97 +++++++++++++ .../why-python/05_Interpreter_Compiler.md | 90 ++++++++++++ 20 files changed, 1950 insertions(+) create mode 100644 summership-26-prs/krish-kagra/loops-and-control-statements/01_For_Loop.md create mode 100644 summership-26-prs/krish-kagra/loops-and-control-statements/02_While_Loop.md create mode 100644 summership-26-prs/krish-kagra/loops-and-control-statements/03_Break.md create mode 100644 summership-26-prs/krish-kagra/loops-and-control-statements/04_Continue.md create mode 100644 summership-26-prs/krish-kagra/loops-and-control-statements/05_Pass.md create mode 100644 summership-26-prs/krish-kagra/loops-and-control-statements/06_If_Else_Loop.md create mode 100644 summership-26-prs/krish-kagra/loops-and-control-statements/07_Nested_Loop.md create mode 100644 summership-26-prs/krish-kagra/operators/01_Arithmetic_Op.md create mode 100644 summership-26-prs/krish-kagra/operators/02_Comparison_Op.md create mode 100644 summership-26-prs/krish-kagra/operators/03_Logical_Op.md create mode 100644 summership-26-prs/krish-kagra/operators/04_Assignment_Op.md create mode 100644 summership-26-prs/krish-kagra/operators/05_Membership_Op.md create mode 100644 summership-26-prs/krish-kagra/operators/06_Identity_Op.md create mode 100644 summership-26-prs/krish-kagra/operators/07_Bitwise_Op.md create mode 100644 summership-26-prs/krish-kagra/operators/08_Operator_Precedence.md create mode 100644 summership-26-prs/krish-kagra/why-python/01_History_Python.md create mode 100644 summership-26-prs/krish-kagra/why-python/02_What_Python.md create mode 100644 summership-26-prs/krish-kagra/why-python/03_Why_Python.md create mode 100644 summership-26-prs/krish-kagra/why-python/04_Features.md create mode 100644 summership-26-prs/krish-kagra/why-python/05_Interpreter_Compiler.md diff --git a/summership-26-prs/krish-kagra/loops-and-control-statements/01_For_Loop.md b/summership-26-prs/krish-kagra/loops-and-control-statements/01_For_Loop.md new file mode 100644 index 00000000..e946b3bb --- /dev/null +++ b/summership-26-prs/krish-kagra/loops-and-control-statements/01_For_Loop.md @@ -0,0 +1,86 @@ +# 1 for Loop + +**The Monkey and the Mangoes** + +**Concept:** for Loop +**Difficulty:** Beginner +**Source:** Indian folk tale + +--- + +## The Story + +Once upon a time, in a dense forest, there lived a clever monkey named Bandar. One sunny afternoon, Bandar discovered a magnificent mango tree loaded with ripe, golden mangoes. There were exactly 12 mangoes hanging from the lowest branch. + +Bandar was hungry and decided to eat every single mango from that branch. He climbed up, sat on the branch, and began counting: + +"One mango... I'll eat this one." +"Two mangoes... I'll eat this one too." +"Three mangoes... and this one..." + +Bandar continued counting from 1 to 12, eating each mango in order. He didn't need to check if there were mangoes left after each one — he knew exactly how many there were because he had counted them first. When he reached the twelfth mango, he ate it, and his task was complete. + +The monkey sat back, patted his full belly, and thought, "I knew exactly how many mangoes I had to eat. I just worked through each one in order." + +--- + +## The Bridge + +Bandar knew the number of items beforehand, just like how a `for` loop works when you know the exact number of times something needs to repeat. + +| In the story | In Python | Why | +| -------------------------------------- | ---------------------------------- | ------------------------------------------------------------- | +| Bandar counted 12 mangoes first | `range(12)` | The loop knows exactly how many times to run before it starts | +| "One mango... I'll eat this one" | `for mango in range(12):` | The loop runs once for each item in the sequence | +| Bandar eats whichever mango is next | `eat(mango)` | The same action happens for each item | +| After the 12th mango, Bandar stops | Loop finishes after 12 iterations | The loop automatically stops after the last item | +| Bandar doesn't check "are there more?" | The loop doesn't check a condition | For loops run for a predetermined number of times | + +--- + +## The Python Code + +```python +mangoes = 12 + +for mango in range(mangoes): + print("Eating mango number", mango + 1) + # The monkey eats each mango + +print("All mangoes eaten! The monkey is full.") +``` + +--- + +## Check Yourself + +**1. What made Bandar's task suitable for a for loop?** + +* A) He had to check after each mango if he was full +* B) He knew exactly how many mangoes there were +* C) He didn't know when to stop +* D) He had to adjust his plan based on each mango + +**2. Which Python code best represents Bandar eating 12 mangoes?** + +* A) `while mangoes > 0: eat(mangoes); mangoes -= 1` +* B) `for mango in range(12): eat(mango)` +* C) `if mangoes == 12: eat(mangoes)` +* D) `for mango in range(100): eat(mango)` + +**3. A farmer has 20 chickens and wants to feed each one exactly 1 cup of grain. Which loop fits best?** + +* A) A for loop, because the number of chickens is known +* B) A while loop, because chickens might not be hungry +* C) An if statement, because each chicken needs the same amount +* D) No loop, because feeding is a one-time action + +--- + +## Answer Key + +**1. B** — Bandar knew exactly how many mangoes were on the branch, making it a perfect `for` loop situation where the count is known beforehand. + +**2. B** — The `for` loop with `range(12)` matches Bandar's situation perfectly: he knows the exact count and processes each item in order. + +**3. A** — Since the farmer knows there are exactly 20 chickens and each gets the same treatment, a `for` loop is the right choice. diff --git a/summership-26-prs/krish-kagra/loops-and-control-statements/02_While_Loop.md b/summership-26-prs/krish-kagra/loops-and-control-statements/02_While_Loop.md new file mode 100644 index 00000000..166029d4 --- /dev/null +++ b/summership-26-prs/krish-kagra/loops-and-control-statements/02_While_Loop.md @@ -0,0 +1,82 @@ +# 2 while Loop + +**The Thirsty Crow** + +**Concept:** while Loop +**Difficulty:** Beginner +**Source:** Indian folk tale + +--- + +## The Story + +It had not rained in weeks. A crow flew from field to field with a dry throat, finding every pond cracked and every trough empty. By afternoon it could barely call out. + +At the edge of a village it found a clay pot. There was water inside — but far down, past the reach of its beak. The crow pushed its head in as far as it would go. Nothing. The pot was too narrow to climb into and too heavy to tip. + +The crow looked around and saw pebbles scattered in the dirt. It picked one up, dropped it into the pot, and looked again. The water was a little higher. Not enough. So it fetched another. And another. And another. + +The crow did not count its pebbles. It only checked, each time, whether the water had come close enough to drink. When at last it had, the crow stopped fetching pebbles and drank. + +--- + +## The Bridge + +The crow never decided how many pebbles it would need. It decided on a rule: *keep dropping pebbles as long as the water is out of reach.* + +| In the story | In Python | Why | +| ------------------------------------------- | ---------------------------------- | -------------------------------------------------------------------------------- | +| The water is still too low to drink | The loop condition | Checked before every single pebble. While it stays true, the crow keeps working. | +| Fetch one pebble and drop it in | The loop body | One repetition, one small change. The body runs again and again. | +| The water rises a little | The state the condition depends on | Something inside the body has to move the world closer to stopping. | +| The water is close enough — the crow drinks | The condition becomes false | The loop stops on its own. Nobody told it the number. | + +--- + +## The Python Code + +```python +water_level = 10 +beak_reach = 25 + +while water_level < beak_reach: + water_level = water_level + 3 # drop one pebble + print("Pebble dropped, water now at", water_level) + +print("The crow drinks.") +``` + +--- + +## Check Yourself + +**1. In the crow's story, which moment is the loop condition?** + +* A) Checking whether the water is still out of reach +* B) Dropping a pebble into the pot +* C) Finding the pot at the edge of the village +* D) Drinking the water at the end + +**2. Which loop matches what the crow actually did?** + +* A) `while water_level < beak_reach: water_level += 3` +* B) `for pebble in range(400): water_level += 3` +* C) `if water_level < beak_reach: water_level += 3` +* D) `while water_level < beak_reach: print("still thirsty")` + +**3. A new situation: you are filling a bucket from a slow tap. You do not know how long it will take — you just keep the tap running until the bucket is full. Which construct fits?** + +* A) A while loop, because the stopping point is a condition rather than a count +* B) A for loop over `range(60)`, because filling a bucket takes about a minute +* C) A single if statement checking whether the bucket is full +* D) No loop is needed — just set the bucket to full + +--- + +## Answer Key + +**1. A** — The condition is the test that runs before every repetition and decides whether there is another one. + +**2. A** — The crow repeats while the water is below its reach, and each repetition raises the water. + +**3. A** — Exactly the crow's shape: repeat an action while something is not yet true. The number of repetitions is unknown until it happens. diff --git a/summership-26-prs/krish-kagra/loops-and-control-statements/03_Break.md b/summership-26-prs/krish-kagra/loops-and-control-statements/03_Break.md new file mode 100644 index 00000000..9f89ef00 --- /dev/null +++ b/summership-26-prs/krish-kagra/loops-and-control-statements/03_Break.md @@ -0,0 +1,87 @@ +# 3 break_statement + +**The Sage and the Mouse** + +**Concept:** break +**Difficulty:** Beginner +**Source:** Indian folk tale + +--- + +## The Story + +In a small village, there lived a wise sage named Gyanesh. One day, a young student came to him and said, "Guruji, I have lost my pet mouse somewhere in the village. Please help me find it." + +The sage smiled and said, "Let us go search. We will visit every house in the village until we find your mouse. We don't need to visit houses after we've found it." + +The sage and the student started their search. They went to the first house — no mouse. The second house — no mouse. The third, fourth, and fifth houses — still no mouse. At the sixth house, the student's eyes lit up. "There it is!" he shouted, pointing to a tiny brown mouse hiding under the bed. + +The sage said, "Now that we have found the mouse, we can stop searching. There is no need to visit the remaining houses." And they stopped their search right there, without checking any more houses. + +--- + +## The Bridge + +The sage knew that once the goal was achieved, there was no reason to continue. This is exactly what `break` does in a loop — it stops the loop immediately when a condition is met. + +| In the story | In Python | Why | +| --------------------------------------------------- | --------------------------------- | ----------------------------------------------- | +| "We will visit every house until we find the mouse" | The loop runs through the houses | The loop was set up to go through all houses. | +| At the sixth house, the mouse is found | The `if` condition becomes True | The condition checks if the mouse is found. | +| "Now we can stop searching" | `break` is executed | The `break` command exits the loop immediately. | +| They don't visit the remaining houses | The loop ends right there | The rest of the iterations are skipped. | +| The sage had planned to visit all houses | The loop was prepared to continue | Without the break, the search would continue. | + +--- + +## The Python Code + +```python +houses = ["House 1", "House 2", "House 3", "House 4", "House 5", "House 6", "House 7", "House 8"] +mouse_found = False + +for house in houses: + print("Searching", house) + if house == "House 6": # Found the mouse! + print("Found the mouse!") + mouse_found = True + break # Stop searching immediately + print("No mouse here.") + +print("Search complete!") +``` + +--- + +## Check Yourself + +**1. Why did the sage and student stop searching at the sixth house?** + +* A) They were tired +* B) They found what they were looking for +* C) They ran out of houses to search +* D) They decided the mouse wasn't important + +**2. In Python, what does `break` do?** + +* A) It pauses the loop and waits for user input +* B) It exits the loop immediately, skipping all remaining iterations +* C) It checks if the loop should continue +* D) It restarts the loop from the beginning + +**3. You are reading a long list of names to find your friend's name. Once you find it, you stop reading. Which code should you use?** + +* A) `for name in names: if name == "friend": break` +* B) `while True: read_next_name()` +* C) `for name in names: read(name)` +* D) `if "friend" in names: print("found")` + +--- + +## Answer Key + +**1. B** — The student found the mouse at the sixth house, so there was no reason to continue searching. + +**2. B** — `break` exits the loop immediately, skipping all remaining iterations. + +**3. A** — This code searches through the list and stops as soon as the friend's name is found, exactly like the sage's search. diff --git a/summership-26-prs/krish-kagra/loops-and-control-statements/04_Continue.md b/summership-26-prs/krish-kagra/loops-and-control-statements/04_Continue.md new file mode 100644 index 00000000..e3157db3 --- /dev/null +++ b/summership-26-prs/krish-kagra/loops-and-control-statements/04_Continue.md @@ -0,0 +1,85 @@ +# 4 continue_statement + +**The Elephant and the Greedy King** + +**Concept:** continue +**Difficulty:** Beginner +**Source:** Indian folk tale + +--- + +## The Story + +In a grand kingdom, there was a king who wanted to count all the elephants in his royal stable. There were many elephants, and each had to be inspected and counted. + +The king's minister started counting: "One... Two... Three..." But the king had a special rule: "If an elephant is grey, count it. If an elephant is white, do not count it today — it will be counted separately tomorrow. Just move on to the next elephant." + +The minister inspected each elephant one by one. When he saw a grey elephant, he counted it. When he saw a white elephant, he said, "Not today," and moved to the next elephant without counting it. He inspected every elephant, but only counted the grey ones. + +--- + +## The Bridge + +The minister didn't stop the process when he saw a white elephant — he just skipped that one and continued with the next. This is exactly what `continue` does: it skips the current iteration and moves to the next one. + +| In the story | In Python | Why | +| --------------------------------------- | ------------------------- | ---------------------------------------------- | +| The minister goes through each elephant | The loop iterates | Each elephant represents one loop iteration. | +| "If it's grey, count it" | The normal loop body | The main action happens for most items. | +| "If it's white, move on to the next" | `continue` statement | Skips this elephant and goes to the next. | +| The minister never stops the process | The loop doesn't break | Unlike `break`, the loop continues to the end. | +| The minister checks the next elephant | The next iteration begins | The loop continues normally after `continue`. | + +--- + +## The Python Code + +```python id="c8m3qv" +elephants = ["grey", "grey", "white", "grey", "white", "grey"] +count = 0 + +for elephant in elephants: + if elephant == "white": + print("White elephant - not counting today") + continue # Skip this elephant and move to the next + + count += 1 + print("Counting a grey elephant. Total:", count) + +print("Total grey elephants:", count) +``` + +--- + +## Check Yourself + +**1. What did the minister do when he saw a white elephant?** + +* A) He stopped the entire counting process +* B) He skipped it and moved to the next elephant +* C) He counted it anyway +* D) He sent it away + +**2. In Python, what is the difference between `break` and `continue`?** + +* A) `break` skips one iteration; `continue` ends the loop +* B) `break` ends the loop; `continue` skips one iteration +* C) Both do the same thing +* D) `break` is for for loops; `continue` is for while loops + +**3. You are going through a list of items to buy. If an item is damaged, you skip it and check the next one. If the item is the last item on your list, you stop shopping. Which would you use?** + +* A) `continue` for damaged items; `break` for the last item +* B) `break` for damaged items; `continue` for the last item +* C) `continue` for both situations +* D) `break` for both situations + +--- + +## Answer Key + +**1. B** — The minister skipped white elephants and moved on to the next one, just like `continue` does. + +**2. B** — `break` exits the entire loop; `continue` skips only the current iteration and continues with the next. + +**3. A** — `continue` skips damaged items; `break` stops shopping when you reach the last item. diff --git a/summership-26-prs/krish-kagra/loops-and-control-statements/05_Pass.md b/summership-26-prs/krish-kagra/loops-and-control-statements/05_Pass.md new file mode 100644 index 00000000..7f0a9ab9 --- /dev/null +++ b/summership-26-prs/krish-kagra/loops-and-control-statements/05_Pass.md @@ -0,0 +1,88 @@ +# 5 pass_statement + +**The Potter's Unfinished Pots** + +**Concept:** pass +**Difficulty:** Beginner +**Source:** Indian folk tale + +--- + +## The Story + +In a pottery workshop, a master potter named Kumbhar was teaching his apprentice how to make clay pots. He showed the apprentice the different steps: preparing the clay, shaping the pot, drying it, painting it, and finally firing it in the kiln. + +But one day, the apprentice asked, "Master, how do we handle special decorative pots? They need extra care and attention." + +The wise potter replied, "For now, just leave a space for that step. We will fill it in later when we learn more about decorating. For regular pots, do everything normally. For decorative pots, do nothing now — but remember to put something there later." + +The apprentice continued making pots. For each regular pot, he went through all the steps. For decorative pots, he simply said, "This will be decorated later," and moved on to the next step. + +--- + +## The Bridge + +The potter needed to have something in place for future work, even though he wasn't ready to implement it yet. This is what `pass` does in Python — it's a placeholder that does nothing, allowing the code to run without errors. + +| In the story | In Python | Why | +| ------------------------------------------------ | -------------------------------------------- | --------------------------------------------------- | +| "Do nothing now, but we'll add decoration later" | `pass` statement | Does nothing, but allows the code to run. | +| For regular pots, do all the steps | Normal loop body | Regular action runs normally. | +| For decorative pots, skip the decoration step | The `if` statement with `pass` | Tells Python to do nothing and continue. | +| The loop continues to the next pot | The loop continues normally | After `pass`, the loop moves to the next iteration. | +| The apprentice remembers to fill it later | The placeholder reminds us where to add code | Indicates intentional "do nothing" for future work. | + +--- + +## The Python Code + +```python id="k2pz7m" +pots = ["regular", "regular", "decorative", "regular", "decorative"] + +for pot in pots: + print("Preparing clay for", pot, "pot") + + if pot == "decorative": + print("This pot needs special decoration later") + pass # Placeholder — we'll add decoration code here later + else: + print("Adding regular decoration to", pot, "pot") + + print("Firing pot in kiln") + print("---") +``` + +--- + +## Check Yourself + +**1. Why did the potter use a placeholder for decorative pots?** + +* A) He didn't know how to make decorative pots +* B) He needed to fill the space with proper decoration steps later +* C) He wanted to skip all decorative pots +* D) He didn't care about decorative pots + +**2. In Python, what does `pass` do?** + +* A) It skips the current iteration and moves to the next one +* B) It exits the loop immediately +* C) It does absolutely nothing +* D) It restarts the loop from the beginning + +**3. You are writing a program and want to handle a special case, but you don't know how to implement it yet. You need to test your code. What should you use?** + +* A) `pass` as a placeholder +* B) `break` to stop the loop +* C) `continue` to skip that case +* D) Remove the special case completely + +--- + +## Answer Key + +**1. B** — The potter needed a placeholder for future decoration steps, just like `pass` is a placeholder for future code. + +**2. C** — `pass` does nothing — it's a placeholder that allows code to run without errors. + +**3. A** — `pass` is perfect as a placeholder for code you plan to implement later. diff --git a/summership-26-prs/krish-kagra/loops-and-control-statements/06_If_Else_Loop.md b/summership-26-prs/krish-kagra/loops-and-control-statements/06_If_Else_Loop.md new file mode 100644 index 00000000..c5fbac40 --- /dev/null +++ b/summership-26-prs/krish-kagra/loops-and-control-statements/06_If_Else_Loop.md @@ -0,0 +1,92 @@ +# 6 else with Loops + +**The Merchant and the Golden Necklace** + +**Concept:** else with Loops +**Difficulty:** Intermediate +**Source:** Indian folk tale + +--- + +## The Story + +In a bustling marketplace, a wealthy merchant named Dhanraj lost his precious golden necklace. He announced to his servants, "Search every room of my house thoroughly. If you find the necklace, bring it to me. But if you search all the rooms and do not find it, come back and report that it is truly lost." + +The servants began searching. They searched the kitchen — no necklace. The bedroom — no necklace. The courtyard — no necklace. The dining hall — no necklace. Room after room, they searched. + +Finally, they had searched every single room in the house. The necklace was nowhere to be found. When they finished the search without finding it, they returned to the merchant and said, "Sir, we have searched every room, but the necklace is not here. It is truly lost." + +The merchant sighed, "Then it must have been stolen. Let us report it to the king's guards." + +--- + +## The Bridge + +The servants had a clear plan: search all rooms, and if they find it, stop and report. But if they search all rooms and find nothing, then report that it's lost. This is exactly how `else` works with a loop — the `else` block runs only when the loop completes without encountering a `break`. + +| In the story | In Python | Why | +| ------------------------------------------------ | ------------------------------------ | ---------------------------------------------- | +| "Search every room" | The loop | The loop goes through each room. | +| "If you find it, bring it to me" | `break` when found | Stop searching when found. | +| "If you search all rooms and don't find it" | The loop completes without `break` | All iterations run without interruption. | +| "Come back and report it is lost" | The `else` block runs | This runs only when no `break` occurred. | +| The servants searched all rooms before reporting | The `else` runs after all iterations | It only runs when the loop finishes naturally. | + +--- + +## The Python Code + +```python id="n8q2vk" +rooms = ["Kitchen", "Bedroom", "Courtyard", "Dining Hall", "Garden"] +necklace_found = None + +for room in rooms: + print("Searching", room) + if room == "Garden": # If found in Garden + print("Found the necklace in the garden!") + necklace_found = True + break +else: + # This runs if the loop completes without finding the necklace + print("Searched all rooms. The necklace is truly lost.") + +if necklace_found: + print("Report: Necklace found!") +else: + print("Report: Necklace is lost. Contact the king's guards.") +``` + +--- + +## Check Yourself + +**1. When did the servants report that the necklace was lost?** + +* A) When they found it in the garden +* B) After searching all rooms and not finding it +* C) Before they started searching +* D) After searching only half the rooms + +**2. In Python, when does the `else` block run with a loop?** + +* A) Always, after every loop +* B) Only when a `break` occurs +* C) Only when the loop completes without a `break` +* D) Never — `else` doesn't work with loops + +**3. You are checking a list of students to see if any have scored 100%. If you find one, stop and announce the winner. If you check everyone and no one has 100%, announce that no one got a perfect score. Which structure should you use?** + +* A) A for loop with `else` for the "no one" case +* B) Two separate loops +* C) A while loop with `break` +* D) An if statement after the loop + +--- + +## Answer Key + +**1. B** — The servants reported the necklace was lost only after searching every room and finding nothing. + +**2. C** — The `else` block runs only when the loop completes all iterations without encountering a `break`. + +**3. A** — The for loop with `else` is perfect for this: the `else` runs if no perfect score is found and no `break` occurs. diff --git a/summership-26-prs/krish-kagra/loops-and-control-statements/07_Nested_Loop.md b/summership-26-prs/krish-kagra/loops-and-control-statements/07_Nested_Loop.md new file mode 100644 index 00000000..77207ef5 --- /dev/null +++ b/summership-26-prs/krish-kagra/loops-and-control-statements/07_Nested_Loop.md @@ -0,0 +1,88 @@ +# 7 Nested Loops + +**The Fisherman and the Golden Fish** + +**Concept:** Nested Loops +**Difficulty:** Intermediate +**Source:** Indian folk tale + +--- + +## The Story + +In a coastal village, there lived a fisherman named Matsya. Every day, he would cast his net into the sea to catch fish. But Matsya was very particular — he wanted to organize his catch neatly. + +One day, after a good catch, Matsya sat on the shore with his baskets. He had 3 baskets, and each basket could hold 5 fish. He began sorting his fish: + +"First basket: fish number 1, fish number 2, fish number 3, fish number 4, fish number 5." +"Second basket: fish number 1, fish number 2, fish number 3, fish number 4, fish number 5." +"Third basket: fish number 1, fish number 2, fish number 3, fish number 4, fish number 5." + +Matsya repeated the same action of placing 5 fish into each basket. He worked systematically: for each basket, he put 5 fish in it. He had to keep track of both which basket he was filling and which fish he was placing. + +--- + +## The Bridge + +Matsya had a loop inside a loop! The outer loop went through each basket, and the inner loop went through the fish for that basket. This is called a nested loop. + +| In the story | In Python | Why | +| ---------------------------------------- | ---------------------------------- | --------------------------------------------- | +| "3 baskets" | Outer loop | The outer loop runs once for each basket. | +| "Each basket holds 5 fish" | Inner loop | For each basket, the inner loop runs 5 times. | +| "First basket: fish 1, 2, 3, 4, 5" | Inner loop completes | The inner loop finishes for one basket. | +| "Second basket: fish 1, 2, 3, 4, 5" | Outer loop moves to next iteration | Then the next basket begins. | +| Matsya knows which basket and which fish | Two variables track progress | Nested loops need to track both positions. | + +--- + +## The Python Code + +```python id="u3d9qx" +baskets = 3 +fish_per_basket = 5 + +for basket in range(1, baskets + 1): + print("Starting basket", basket) + for fish in range(1, fish_per_basket + 1): + print(" Placing fish", fish, "in basket", basket) + print("Finished basket", basket) + print("---") + +print("All fish sorted!") +``` + +--- + +## Check Yourself + +**1. How many total fish did Matsya place in all baskets?** + +* A) 3 +* B) 5 +* C) 8 +* D) 15 + +**2. In a nested loop, what happens first?** + +* A) The inner loop runs for the first outer loop iteration +* B) The outer loop finishes before the inner loop starts +* C) Both loops run at the same time +* D) The inner loop runs for all iterations before the outer loop starts + +**3. You have a classroom with 5 rows of desks, and each row has 4 desks. You want to clean each desk in every row. Which structure would you use?** + +* A) One loop for rows, another nested loop for desks in each row +* B) A single loop for all desks +* C) Two separate loops that run one after another +* D) No loops — just clean each desk individually + +--- + +## Answer Key + +**1. D** — 3 baskets × 5 fish per basket = 15 total fish. + +**2. A** — The inner loop completes all its iterations for the first outer loop iteration before the outer loop moves to the next iteration. + +**3. A** — This is a perfect nested loop situation: the outer loop goes through each row, and the inner loop goes through each desk in that row. diff --git a/summership-26-prs/krish-kagra/operators/01_Arithmetic_Op.md b/summership-26-prs/krish-kagra/operators/01_Arithmetic_Op.md new file mode 100644 index 00000000..e8a10c65 --- /dev/null +++ b/summership-26-prs/krish-kagra/operators/01_Arithmetic_Op.md @@ -0,0 +1,115 @@ +# 1 Arithmetic Operators + +**The Brahmin's Pot (Hitopadesha)** + +**Concept:** Arithmetic Operators (`+`, `-`, `*`, `/`, `%`, `**`, `//`) +**Difficulty:** Beginner +**Source:** Hitopadesha + +--- + +## The Story + +A poor Brahmin lived in a small village. One day, a generous merchant gave him a clay pot filled with flour. The Brahmin was overjoyed. He took the pot home and hung it on a hook above his bed. + +As he lay down, he began to dream. "I will sell this flour and buy two goats. The goats will give birth to more goats. I will sell the goats and buy cows. The cows will give milk, and I will sell the milk to buy buffaloes. The buffaloes will have calves, and soon I will have a whole herd!" + +He calculated in his mind: *one pot of flour becomes two goats, which become four goats, which become eight goats...* He multiplied and added in his head, growing more excited with each arithmetic operation. + +"In the end," he thought, "I will have so much wealth that I will build a grand house, marry a beautiful woman, and have a son. When my son misbehaves, I will scold him!" And in his excitement, he kicked his leg—striking the pot, which shattered into pieces. All his dreams, built on arithmetic, came crashing down. + +--- + +## The Bridge + +The Brahmin was performing arithmetic operations in his head—adding, multiplying, and planning. Python's arithmetic operators let us perform these same calculations on numbers. + +| In the story | In Python | Why | +| --------------------------------- | ------------------------------------- | ------------------------------------ | +| "One pot becomes two goats" | `1 * 2 = 2` | Multiplication (`*`) | +| "Two goats become four goats" | `2 * 2 = 4` | More multiplication | +| "Sell flour and buy goats" | `goats = flour / price_per_goat` | Division (`/`) | +| "Cows give milk to sell" | `total_wealth = cows * milk_price` | Combining operations | +| The Brahmin calculated totals | `total = initial + growth - expenses` | Addition (`+`) and subtraction (`-`) | +| The pot shattered—his count ended | `remaining = total % 10` | Modulo (`%`) gives the remainder | + +--- + +## The Python Code + +```python +# Arithmetic operators in Python + +# Basic operations +flour_pots = 1 +goats = flour_pots * 2 # Multiplication: 2 +cows = goats * 2 # Multiplication: 4 +buffaloes = cows * 2 # Multiplication: 8 + +total_animals = goats + cows + buffaloes # Addition: 14 +total_animals = total_animals - 1 # Subtraction: 13 (one got sick) + +# Division and modulo +animals_per_pen = 3 +pens_needed = total_animals // animals_per_pen # Floor division: 4 +leftover = total_animals % animals_per_pen # Modulo: 1 + +# Exponentiation +wealth_growth = 2 ** 3 # 2 to the power of 3 = 8 + +print("Total animals:", total_animals) +print("Pens needed:", pens_needed) +print("Leftover animals:", leftover) +``` + +--- + +## Check Yourself + +**1. Which arithmetic operator would you use to find the remainder after division?** + +* A) `/` +* B) `%` +* C) `*` +* D) `//` + +--- + +**2. The Brahmin calculated `2 * 2 * 2 = 8`. What is the Python equivalent?** + +* A) `2 + 2 + 2` +* B) `2 ** 3` +* C) `2 / 3` +* D) `2 % 3` + +--- + +**3. If you have 17 apples and want to put them in bags of 5, which operator tells you how many bags you need?** + +* A) `/` +* B) `%` +* C) `//` +* D) `**` + +--- + +**4. If a farmer has 20 liters of milk and sells each liter for ₹3, then spends ₹10, what is the final amount?** + +* A) `20 + 3 - 10` +* B) `20 * 3 - 10` +* C) `20 / 3 - 10` +* D) `20 % 3 - 10` + +--- + +## Answer Key + +**1. B** — The modulo operator `%` returns the remainder after division. + +**2. B** — `2 ** 3` means 2 raised to the power of 3, which equals 8. + +**3. C** — Floor division (`//`) gives the whole number of bags needed (17 // 5 = 3 bags, with 2 left over). + +**4. B** — Multiply first, then subtract expenses: `20 * 3 - 10 = 50`. + +--- diff --git a/summership-26-prs/krish-kagra/operators/02_Comparison_Op.md b/summership-26-prs/krish-kagra/operators/02_Comparison_Op.md new file mode 100644 index 00000000..0cedcf0b --- /dev/null +++ b/summership-26-prs/krish-kagra/operators/02_Comparison_Op.md @@ -0,0 +1,111 @@ +# 2 Comparison Operators + +**The Wise Judge and the Two Mothers (Jataka Tales)** + +**Concept:** Comparison Operators (`==`, `!=`, `>`, `<`, `>=`, `<=`) +**Difficulty:** Beginner +**Source:** Jataka Tales + +--- + +## The Story + +A woman was bathing her child in a river when a demoness, disguised as a woman, stole the baby. Both women claimed the child was theirs and brought their dispute before the wise Bodhisattva, who served as the village judge. + +The judge said, "I will settle this fairly. Each of you will pull the child toward yourself. The one who pulls harder will be the true mother." + +The women began to pull. The real mother, seeing her child cry in pain, could not bear it. She let go, weeping. The demoness pulled the child toward herself triumphantly. + +The judge declared, "Stop! The one who let go is the true mother. A real mother compares her child's pain to her own happiness and chooses her child's well-being above all." + +The demoness was exposed and fled. The mother and child were reunited. + +--- + +## The Bridge + +The judge compared the actions of the two women to determine the truth. Comparison operators in Python let us compare values and make decisions based on those comparisons. + +| In the story | In Python | Why | +| ------------------------------------- | ----------------------------------- | ---------------------------- | +| "Is this woman the real mother?" | `woman1 == real_mother` | Equality check (`==`) | +| "The demoness is not the mother" | `woman2 != real_mother` | Inequality check (`!=`) | +| "Who pulled harder?" | `pull_strength1 > pull_strength2` | Greater than (`>`) | +| "The real mother's love was stronger" | `love_real > love_fake` | Comparison of values | +| "The child's pain was too great" | `pain > 10` | Checking a condition | +| The judge declared the winner | `if mother == real: return_child()` | Decision based on comparison | + +--- + +## The Python Code + +```python +# Comparison operators in Python + +real_mother = "Rama" +woman1 = "Rama" +woman2 = "Demoness" + +# Equality and inequality +print(woman1 == real_mother) # True - woman1 is the real mother +print(woman2 == real_mother) # False - woman2 is not + +# Greater than / Less than +pull_strength_rama = 5 +pull_strength_demon = 8 +print(pull_strength_rama > pull_strength_demon) # False +print(pull_strength_rama < pull_strength_demon) # True + +# Greater than or equal to / Less than or equal to +love_real = 100 +love_fake = 20 +print(love_real >= love_fake) # True +print(love_fake <= 30) # True + +# Making a decision +if woman1 == real_mother: + print("Return the child to woman1!") +else: + print("Woman1 is not the mother.") +``` + +--- + +## Check Yourself + +**1. Which operator checks if two values are equal?** + +* A) `=` +* B) `==` +* C) `!=` +* D) `>=` + +--- + +**2. The judge compared pull strengths. Which operator would check if Rama pulled harder than the demoness?** + +* A) `rama < demon` +* B) `rama == demon` +* C) `rama > demon` +* D) `rama != demon` + +--- + +**3. Which comparison would be TRUE if the demoness is NOT the real mother?** + +* A) `demoness == mother` +* B) `demoness != mother` +* C) `demoness > mother` +* D) `demoness < mother` + +--- + +## Answer Key + +**1. B** — `==` is the equality operator. `=` is for assignment. + +**2. C** — `rama > demon` checks if Rama's strength is greater. + +**3. B** — `demoness != mother` means "demoness is not equal to mother," which is true. + +--- diff --git a/summership-26-prs/krish-kagra/operators/03_Logical_Op.md b/summership-26-prs/krish-kagra/operators/03_Logical_Op.md new file mode 100644 index 00000000..e5a1d1c6 --- /dev/null +++ b/summership-26-prs/krish-kagra/operators/03_Logical_Op.md @@ -0,0 +1,107 @@ +# 3 Logical Operators + +**The Four Friends and the Hunter (Panchatantra)** + +**Concept:** Logical Operators (`and`, `or`, `not`) +**Difficulty:** Beginner +**Source:** Panchatantra + +--- + +## The Story + +In a forest, four friends lived together: a deer, a crow, a turtle, and a mouse. They had promised to protect one another. + +One day, the deer was caught in a hunter's net. The crow flew to warn the others. "The deer is trapped!" he cried. The mouse said, "I can chew through the net, BUT I need to reach the deer first." The turtle said, "I can distract the hunter, OR I can carry the mouse on my back to the deer." + +They decided: "We will succeed IF the mouse chews the net AND the hunter is distracted." The crow flew above the hunter, cawing loudly. The turtle crawled near the hunter's feet. The hunter chased the turtle, leaving the deer alone. The mouse chewed through the net, and the deer escaped. + +They all survived because they used their combined abilities—NOT because of luck, but because of careful planning and teamwork. + +--- + +## The Bridge + +The friends combined conditions to make decisions. Logical operators let us combine multiple conditions in Python. + +| In the story | In Python | Why | +| --------------------------------------------------------- | -------------------------------------- | ---------------------------- | +| "I can chew the net, BUT I need to reach the deer" | `can_chew and can_reach` | Both must be true (`and`) | +| "I can distract the hunter, OR carry the mouse" | `can_distract or can_carry` | Either one is enough (`or`) | +| "They succeeded NOT because of luck" | `not luck` | Negation (`not`) | +| "We will succeed IF mouse chews AND hunter is distracted" | `mouse_chews and hunter_distracted` | Both conditions must be true | +| "The hunter chased the turtle, leaving the deer" | `if hunter_distracted: deer_escapes()` | Condition leads to action | + +--- + +## The Python Code + +```python id="l0gic5" +# Logical operators in Python + +mouse_can_chew = True +mouse_can_reach = True +hunter_distracted = True +luck = False + +# AND - both must be True +deer_escapes = mouse_can_chew and mouse_can_reach and hunter_distracted +print("Deer escapes:", deer_escapes) # True - all conditions met + +# OR - at least one must be True +turtle_can_distract = True +turtle_can_carry = False +turtle_helps = turtle_can_distract or turtle_can_carry +print("Turtle helps:", turtle_helps) # True - at least one is True + +# NOT - reverses the condition +print(not luck) # True - it was NOT luck +print(not mouse_can_chew) # False - mouse CAN chew + +# Combining multiple conditions +if mouse_can_chew and (hunter_distracted or turtle_helps): + print("The deer escapes successfully!") +else: + print("The deer is caught.") +``` + +--- + +## Check Yourself + +**1. Which logical operator requires BOTH conditions to be true?** + +* A) `or` +* B) `and` +* C) `not` +* D) `==` + +--- + +**2. The friends succeeded because mouse chewed AND hunter was distracted. How would you write this in Python?** + +* A) `mouse_chews or hunter_distracted` +* B) `mouse_chews and hunter_distracted` +* C) `not mouse_chews` +* D) `mouse_chews == hunter_distracted` + +--- + +**3. If `x = True` and `y = False`, what is the result of `x or y`?** + +* A) `True` +* B) `False` +* C) `None` +* D) Error + +--- + +## Answer Key + +**1. B** — `and` requires both conditions to be True. + +**2. B** — `and` combines both conditions; both must be True for the deer to escape. + +**3. A** — `True or False` evaluates to `True` because `or` only needs one condition to be True. + +--- diff --git a/summership-26-prs/krish-kagra/operators/04_Assignment_Op.md b/summership-26-prs/krish-kagra/operators/04_Assignment_Op.md new file mode 100644 index 00000000..f29e69c0 --- /dev/null +++ b/summership-26-prs/krish-kagra/operators/04_Assignment_Op.md @@ -0,0 +1,115 @@ +# 4 Assignment Operators + +**The King's Growing Treasure (Vikram-Betaal)** + +**Concept:** Assignment Operators (`=`, `+=`, `-=`, `*=`, `/=`, `%=`, `//=`, `**=`) +**Difficulty:** Beginner +**Source:** King Vikramaditya and Betaal + +--- + +## The Story + +King Vikramaditya was known for his wisdom and wealth. One day, Betaal the ghost asked him a riddle: "O King, if you had a treasure that doubled every day, how would you keep track of it?" + +The king replied, "I would count it each day and add the new amount to the old." He began: "Today I have 100 gold coins. Tomorrow, I will have 200. So I add 100 more. The next day, I have 400, so I add 200 more. Each day, I update my total." + +Betaal laughed. "But King, you must also spend some gold on your kingdom. Each day, you subtract what you give away." The king nodded. "Yes, I must also divide my treasure among my ministers and multiply the rest through trade." + +The king realized that managing wealth meant constantly updating his totals—adding, subtracting, multiplying, and dividing—always assigning the new value back to his treasury. + +--- + +## The Bridge + +The king was performing assignment operations—updating a variable with new values. Assignment operators in Python let us update variables concisely. + +| In the story | In Python | Why | +| --------------------------------------- | ----------------- | -------------------------- | +| "Today I have 100 coins" | `treasure = 100` | Simple assignment (`=`) | +| "Tomorrow, I add 100 more" | `treasure += 100` | Add and assign (`+=`) | +| "Each day, I subtract what I give away" | `treasure -= 50` | Subtract and assign (`-=`) | +| "The treasure doubled" | `treasure *= 2` | Multiply and assign (`*=`) | +| "I divide among my ministers" | `treasure /= 5` | Divide and assign (`/=`) | +| "The remainder after division" | `treasure %= 10` | Modulo and assign (`%=`) | + +--- + +## The Python Code + +```python id="asgn54" +# Assignment operators in Python + +# Simple assignment +treasure = 100 +print("Initial treasure:", treasure) + +# Add and assign +treasure += 100 # Same as: treasure = treasure + 100 +print("After adding:", treasure) # 200 + +# Multiply and assign +treasure *= 2 # Same as: treasure = treasure * 2 +print("After doubling:", treasure) # 400 + +# Subtract and assign +treasure -= 50 # Same as: treasure = treasure - 50 +print("After spending:", treasure) # 350 + +# Divide and assign +treasure /= 5 # Same as: treasure = treasure / 5 +print("After dividing:", treasure) # 70.0 + +# Floor divide and assign +treasure //= 3 # Same as: treasure = treasure // 3 +print("After floor division:", treasure) # 23.0 + +# Modulo and assign +treasure %= 10 # Same as: treasure = treasure % 10 +print("After modulo:", treasure) # 3.0 + +# Power and assign +treasure **= 2 # Same as: treasure = treasure ** 2 +print("After squaring:", treasure) # 9.0 +``` + +--- + +## Check Yourself + +**1. What does `x += 5` do?** + +* A) Adds 5 to x and stores the result in x +* B) Checks if x is equal to 5 +* C) Multiplies x by 5 +* D) Subtracts 5 from x + +--- + +**2. If `gold = 50`, what is the result of `gold *= 3`?** + +* A) 53 +* B) 150 +* C) 47 +* D) 50 + +--- + +**3. The king's treasure doubled each day. If `treasure = 100`, which operator would you use to double it?** + +* A) `treasure += 2` +* B) `treasure *= 2` +* C) `treasure /= 2` +* D) `treasure **= 2` + +--- + +## Answer Key + +**1. A** — `x += 5` is shorthand for `x = x + 5`. + +**2. B** — `gold *= 3` means `gold = gold * 3`, so 50 * 3 = 150. + +**3. B** — `treasure *= 2` doubles the treasure (`treasure = treasure * 2`). + +--- diff --git a/summership-26-prs/krish-kagra/operators/05_Membership_Op.md b/summership-26-prs/krish-kagra/operators/05_Membership_Op.md new file mode 100644 index 00000000..fcaaf337 --- /dev/null +++ b/summership-26-prs/krish-kagra/operators/05_Membership_Op.md @@ -0,0 +1,111 @@ +# 5 Membership Operators + +**The Golden Mallard (Jataka Tales)** + +**Concept:** Membership Operators (`in`, `not in`) +**Difficulty:** Beginner +**Source:** Jataka Tales + +--- + +## The Story + +In a beautiful lake, there lived a magnificent golden mallard duck with feathers of pure gold. Every day, the mallard would pluck one golden feather and leave it for a poor widow and her two daughters who lived nearby. + +The widow collected the feathers and sold them. But one day, greed overcame her. She thought, "If the mallard has golden feathers, surely there must be a treasure inside the lake. I will catch the duck and take all its feathers at once!" + +She searched the lake. Was the mallard in the reeds? *No.* Was it behind the lotus flowers? *No.* Was it swimming near the shore? *No.* She looked everywhere, but the mallard was not in any of those places. + +When she finally found the mallard, it said, "Greed has made you lose what you had. Since you wanted everything at once, you shall have nothing." And with that, the mallard flew away, never to return. The widow checked her basket—there were no golden feathers in it anymore. The mallard was no longer in the lake, and her golden feathers were no longer in her possession. + +--- + +## The Bridge + +The widow checked whether the mallard was in various locations. Membership operators in Python check whether a value exists in a collection. + +| In the story | In Python | Why | +| --------------------------------------------- | -------------------------- | --------------------------------- | +| "Was the mallard in the reeds?" | `mallard in reeds` | Checking membership (`in`) | +| "Was it behind the lotus flowers?" | `mallard in lotus_flowers` | More membership checks | +| "No, it was not in any of those places" | `mallard not in reeds` | Not in (`not in`) | +| "There were no golden feathers in the basket" | `feather not in basket` | Checking absence | +| "The mallard was no longer in the lake" | `mallard not in lake` | Membership check returns False | +| The widow checked each possible location | Loop with `in` operator | Iterating and checking membership | + +--- + +## The Python Code + +```python id="memb55" +# Membership operators in Python + +# A list of locations +locations = ["reeds", "lotus_flowers", "shore", "deep_water"] +mallard_location = "deep_water" + +# Checking if mallard is in a location +print(mallard_location in locations) # True - "deep_water" is in the list + +# Checking if mallard is NOT in a location +print(mallard_location not in locations) # False - it IS in the list + +# Checking specific locations +if mallard_location == "reeds": + print("Mallard is in the reeds!") +elif mallard_location in ["lotus_flowers", "shore"]: + print("Mallard is near the shore or flowers!") +else: + print("Mallard is somewhere else") + +# Checking for golden feathers in a basket +basket = ["copper", "silver", "bronze"] # No gold here +if "gold" not in basket: + print("No golden feathers in the basket!") + +# Membership with strings +sentence = "The golden mallard lives in the lake" +print("golden" in sentence) # True +print("treasure" in sentence) # False +``` + +--- + +## Check Yourself + +**1. What does the `in` operator do in Python?** + +* A) Checks if a value exists in a sequence +* B) Adds a value to a sequence +* C) Removes a value from a sequence +* D) Compares two values + +--- + +**2. If `basket = ["gold", "silver", "bronze"]`, what is `"gold" in basket`?** + +* A) `True` +* B) `False` +* C) `None` +* D) Error + +--- + +**3. Which operator would you use to check if a mallard is NOT in the lake?** + +* A) `mallard in lake` +* B) `mallard not in lake` +* C) `mallard == lake` +* D) `mallard != lake` + +--- + +## Answer Key + +**1. A** — `in` checks if a value exists in a sequence (list, tuple, string, etc.). + +**2. A** — `"gold" in basket` returns `True` because "gold" is in the list. + +**3. B** — `mallard not in lake` checks if the mallard is NOT present in the lake. + +--- diff --git a/summership-26-prs/krish-kagra/operators/06_Identity_Op.md b/summership-26-prs/krish-kagra/operators/06_Identity_Op.md new file mode 100644 index 00000000..6ae95b12 --- /dev/null +++ b/summership-26-prs/krish-kagra/operators/06_Identity_Op.md @@ -0,0 +1,109 @@ +# 6 Identity Operators + +**The Two Bodies and One Soul (Vikram-Betaal)** + +**Concept:** Identity Operators (`is`, `is not`) +**Difficulty:** Beginner +**Source:** King Vikramaditya and Betaal + +--- + +## The Story + +One night, Betaal the ghost told King Vikramaditya a strange tale. "O King, there was a man who could project his soul into any body. One day, he left his own body and entered the body of a dead prince. His friend, thinking the man was truly dead, cremated his original body. The man was now trapped in the prince's body." + +Betaal asked, "O King, is the man still the same person, even though his body is different? Is he the same identity or a different one?" + +The king thought deeply. "The body is not the person. The soul is the person. Even though the body changed, the soul remained the same. The prince's body and the man's original body are different objects, but the soul within them is the same identity." + +Betaal smiled. "You have answered correctly, O King. Just as two bodies can share one soul, two variables can point to the same object in memory." + +--- + +## The Bridge + +The king distinguished between the body (the object) and the soul (the identity). Identity operators in Python check whether two variables refer to the same object in memory. + +| In the story | In Python | Why | +| ----------------------------------------- | ------------------------------- | -------------------------------------------- | +| "Is the man still the same person?" | `man is person` | Identity check (`is`) | +| "The body changed, but the soul remained" | `soul is not body` | Different identity (`is not`) | +| "The prince's body is a different object" | `prince_body is not man_body` | Two different objects | +| "The soul within them is the same" | `soul_in_man is soul_in_prince` | Same identity | +| Two bodies, one identity | Two variables, one object | `is` checks if they refer to the same object | + +--- + +## The Python Code + +```python id="ident56" +# Identity operators in Python + +# Two variables pointing to the same object +man_soul = {"name": "The Man", "body": "original"} +prince_soul = man_soul # Both refer to the SAME object + +# Check if they are the same object +print(man_soul is prince_soul) # True - they are the same object + +# Two different objects with the same content +man_body = {"type": "human", "status": "alive"} +prince_body = {"type": "human", "status": "alive"} + +print(man_body is prince_body) # False - different objects +print(man_body == prince_body) # True - same content, different objects + +# Using is not +print(man_body is not prince_body) # True - they are different objects + +# With simple values +a = 10 +b = 10 +print(a is b) # True (Python may cache small integers) + +c = [1, 2, 3] +d = [1, 2, 3] +print(c is d) # False - different list objects +print(c == d) # True - same content +``` + +--- + +## Check Yourself + +**1. What does the `is` operator check?** + +* A) If two values are equal +* B) If two variables refer to the same object +* C) If a value exists in a sequence +* D) If two values are different + +--- + +**2. If `a = [1, 2, 3]` and `b = [1, 2, 3]`, what is `a is b`?** + +* A) `True` +* B) `False` +* C) `None` +* D) Error + +--- + +**3. Which operator would you use to check if two variables point to different objects?** + +* A) `!=` +* B) `is not` +* C) `not in` +* D) `==` + +--- + +## Answer Key + +**1. B** — `is` checks object identity—whether two variables refer to the same object in memory. + +**2. B** — `a` and `b` are two different list objects with the same content, so `a is b` is `False`. + +**3. B** — `is not` checks if two variables point to different objects. + +--- diff --git a/summership-26-prs/krish-kagra/operators/07_Bitwise_Op.md b/summership-26-prs/krish-kagra/operators/07_Bitwise_Op.md new file mode 100644 index 00000000..0a3c8ecc --- /dev/null +++ b/summership-26-prs/krish-kagra/operators/07_Bitwise_Op.md @@ -0,0 +1,112 @@ +# 7 Bitwise Operators + +**The Lion and the Clever Rabbit (Panchatantra)** + +**Concept:** Bitwise Operators (`&`, `|`, `^`, `~`, `<<`, `>>`) +**Difficulty:** Intermediate +**Source:** Panchatantra + +--- + +## The Story + +In a forest, a ferocious lion was terrorizing all the animals. The animals decided to send one animal each day to the lion as food. When it was the clever rabbit's turn, he had a plan. + +The rabbit arrived late and told the lion, "O mighty lion, I was stopped by another lion who claimed to be stronger than you. He said he would eat me instead." + +The lion roared, "Take me to this impostor!" The rabbit led the lion to a deep well. "He lives down there," said the rabbit. The lion looked into the well and saw his own reflection. He roared, and the reflection roared back. Enraged, the lion jumped into the well and drowned. + +The rabbit had used the lion's own strength against him—turning each roar back at him, bit by bit. + +--- + +## The Bridge + +The lion's reflection shows how bits work. Bitwise operators in Python work on individual bits of numbers, just like the lion's roars were reflected back at him. + +| In the story | In Python | Why | | +| --------------------------------- | ---------------------- | -------------------------------- | ----------------- | +| The lion saw his reflection | `a & b` (AND) | Each bit is compared | | +| The reflection roared back | `a | b` (OR) | Bits are combined | +| The lion fought his own image | `a ^ b` (XOR) | Bits that differ are highlighted | | +| The lion was defeated by himself | `~a` (NOT) | Bits are inverted | | +| The lion moved closer to the well | `a << 1` (Left shift) | Bits shift left | | +| The lion moved away from the well | `a >> 1` (Right shift) | Bits shift right | | + +--- + +## The Python Code + +```python id="bit57" +# Bitwise operators in Python + +# Binary representation +a = 5 # 0101 in binary +b = 3 # 0011 in binary + +# Bitwise AND - both bits must be 1 +print(a & b) # 0001 = 1 + +# Bitwise OR - at least one bit must be 1 +print(a | b) # 0111 = 7 + +# Bitwise XOR - bits must be different +print(a ^ b) # 0110 = 6 + +# Bitwise NOT - flips all bits +print(~a) # -6 (in Python, ~x = -x - 1) + +# Left shift - shifts bits left (multiplies by 2) +print(a << 1) # 1010 = 10 + +# Right shift - shifts bits right (divides by 2) +print(a >> 1) # 0010 = 2 + +# Practical example: checking if a number is even +def is_even(num): + return (num & 1) == 0 + +print(is_even(4)) # True - 4 & 1 = 0 +print(is_even(5)) # False - 5 & 1 = 1 +``` + +--- + +## Check Yourself + +**1. What is the result of `5 & 3` in Python?** + +* A) 8 +* B) 7 +* C) 1 +* D) 6 + +--- + +**2. What does the left shift operator (`<<`) do?** + +* A) Shifts bits to the right +* B) Shifts bits to the left (multiplies by 2) +* C) Compares two bits +* D) Flips all bits + +--- + +**3. If `x = 8` (1000 in binary), what is `x >> 1`?** + +* A) 16 +* B) 4 +* C) 8 +* D) 0 + +--- + +## Answer Key + +**1. C** — 5 (0101) & 3 (0011) = 0001 = 1. + +**2. B** — Left shift (`<<`) moves bits to the left, which multiplies the number by 2. + +**3. B** — 8 (1000) >> 1 = 4 (0100), which is 8 ÷ 2. + +--- diff --git a/summership-26-prs/krish-kagra/operators/08_Operator_Precedence.md b/summership-26-prs/krish-kagra/operators/08_Operator_Precedence.md new file mode 100644 index 00000000..3b4502d1 --- /dev/null +++ b/summership-26-prs/krish-kagra/operators/08_Operator_Precedence.md @@ -0,0 +1,136 @@ +# 8 Operator Precedence + +**The Court of King Vikramaditya (Vikram-Betaal)** + +**Concept:** Operator Precedence +**Difficulty:** Intermediate +**Source:** King Vikramaditya and Betaal + +--- + +## The Story + +King Vikramaditya held court with his ministers, each responsible for different matters. The king had a strict order: + +1. First, the royal priest would speak (parentheses). +2. Then, the prime minister would present matters of multiplication and division (wealth and army). +3. Next, the treasurer would report on addition and subtraction (income and expenses). +4. Finally, the guards would handle comparisons and decisions (judgments). + +One day, a minister tried to speak out of turn. The king said, "In my court, there is a fixed order. The priest speaks first, then the prime minister, then the treasurer, and finally the guards. If you speak out of order, chaos will follow." + +The minister understood. Just as the court had a hierarchy, mathematical expressions must follow a fixed order to be evaluated correctly. + +--- + +## The Bridge + +Operator precedence is like the court's hierarchy—certain operations are performed before others. + +| In the court | In Python | Precedence (highest to lowest) | +| ---------------------------------------- | -------------------------------- | ------------------------------ | +| Royal priest speaks first | `()` parentheses | Highest precedence | +| Prime minister (multiplication/division) | `*`, `/`, `//`, `%` | Second highest | +| Treasurer (addition/subtraction) | `+`, `-` | Third highest | +| Guards (comparisons) | `==`, `!=`, `>`, `<`, `>=`, `<=` | Lower precedence | +| Messengers (assignment) | `=`, `+=`, `-=`, etc. | Lowest precedence | + +--- + +## The Python Code + +```python id="prec58" +# Operator precedence in Python + +# Example 1: Without parentheses +result = 10 + 5 * 2 +print(result) # 20 (not 30) because * happens before + + +# Example 2: With parentheses +result = (10 + 5) * 2 +print(result) # 30 (parentheses first) + +# Example 3: Mixed operators +result = 20 - 4 / 2 + 3 * 2 +# Step 1: 4 / 2 = 2 +# Step 2: 3 * 2 = 6 +# Step 3: 20 - 2 + 6 = 24 +print(result) # 24.0 + +# Example 4: Comparison with arithmetic +x = 10 +result = x + 5 > 12 +# Step 1: x + 5 = 15 +# Step 2: 15 > 12 = True +print(result) # True + +# Example 5: Logical with comparison +age = 25 +has_license = True +can_drive = age >= 18 and has_license +# Step 1: age >= 18 = True +# Step 2: True and True = True +print(can_drive) # True +``` + +--- + +## Operator Precedence Table (Highest to Lowest) + +| Operator | Description | +| ---------------------------------------------------------------- | ------------------------------------------------ | +| `()` | Parentheses | +| `**` | Exponentiation | +| `+x`, `-x`, `~x` | Unary positive, negative, bitwise NOT | +| `*`, `/`, `//`, `%` | Multiplication, division, floor division, modulo | +| `+`, `-` | Addition, subtraction | +| `<<`, `>>` | Bitwise shifts | +| `&` | Bitwise AND | +| `^` | Bitwise XOR | +| `\|` | Bitwise OR | +| `==`, `!=`, `>`, `<`, `>=`, `<=`, `is`, `is not`, `in`, `not in` | Comparisons, identity, membership | +| `not` | Logical NOT | +| `and` | Logical AND | +| `or` | Logical OR | +| `=`, `+=`, `-=`, etc. | Assignment | + +--- + +## Check Yourself + +**1. What is the result of `2 + 3 * 4`?** + +* A) 20 +* B) 14 +* C) 24 +* D) 10 + +--- + +**2. How would you change `2 + 3 * 4` to get 20?** + +* A) `2 + (3 * 4)` +* B) `(2 + 3) * 4` +* C) `2 * (3 + 4)` +* D) `(2 * 3) + 4` + +--- + +**3. Which operator has the highest precedence?** + +* A) `+` +* B) `*` +* C) `()` +* D) `==` + +--- + +## Answer Key + +**1. B** — 3 * 4 = 12, then 2 + 12 = 14 (multiplication before addition). + +**2. B** — `(2 + 3) * 4` = 5 * 4 = 20. Parentheses change the order. + +**3. C** — Parentheses `()` have the highest precedence in Python. + +--- diff --git a/summership-26-prs/krish-kagra/why-python/01_History_Python.md b/summership-26-prs/krish-kagra/why-python/01_History_Python.md new file mode 100644 index 00000000..332e054d --- /dev/null +++ b/summership-26-prs/krish-kagra/why-python/01_History_Python.md @@ -0,0 +1,66 @@ +# 1 History of Python + +**The Churning of the Ocean (Samudra Manthan)** + +**Concept:** History of Python +**Difficulty:** Beginner +**Source:** Indian mythology + +--- + +## The Story + +Long ago, the gods and demons churned the cosmic ocean to obtain the nectar of immortality. Many precious things emerged: the wish‑giving tree, the divine cow, the moon, and finally the pot of nectar. Each treasure came from a different source, yet together they formed a collection of wonders that could serve all beings. + +In the same way, Python was not born in a day. Guido van Rossum started it in the late 1980s as a hobby project, inspired by the ABC language, the power of C, and the elegance of Modula‑3. Over the years, the community added libraries, tools, and ideas from many other languages. Like the ocean churning, Python’s history is a blend of contributions from many people, evolving into the rich language we use today. + +--- + +## The Bridge + +| In the story | In Python's history | Why | +|--------------|---------------------|-----| +| Many treasures emerged from the ocean | Many features came from different languages | Python borrowed ideas from ABC, C, Modula‑3, etc. | +| The nectar was the final prize | The final, stable Python 3.0 release | It took time and many versions to reach maturity | +| Gods and demons worked together | The open‑source community (developers worldwide) | Python is built by a diverse global community | +| Each treasure had a purpose | Each feature (lists, dictionaries, etc.) serves a need | Python’s design is practical and versatile | + +--- + +## The Python Connection + +Python was conceived in the late 1980s by Guido van Rossum at Centrum Wiskunde & Informatica (CWI) in the Netherlands. It was released in 1991 as Python 0.9.0. The language name comes from the British comedy group *Monty Python* – not the snake! Python 2.0 came in 2000, and Python 3.0 in 2008, which was a major revision that broke backward compatibility to clean up the language. + +Today, Python is maintained by the Python Software Foundation and is one of the most popular programming languages in the world. + +--- + +## Check Yourself + +**1. Who created Python?** +- A) Guido van Rossum +- B) Tim Berners‑Lee +- C) James Gosling +- D) Bjarne Stroustrup + +**2. What was a major inspiration for Python's design?** +- A) The C++ language +- B) The ABC language +- C) The Java language +- D) The Ruby language + +**3. Why is Python called "Python"?** +- A) After the snake +- B) After a Greek myth +- C) After the British comedy group Monty Python +- D) After the Pythonidae family + +--- + +## Answer Key + +**1. A** — Guido van Rossum created Python in the late 1980s. + +**2. B** — Python was heavily inspired by the ABC language, which Guido had worked on previously. + +**3. C** — The name comes from the Monty Python comedy group, not the snake. \ No newline at end of file diff --git a/summership-26-prs/krish-kagra/why-python/02_What_Python.md b/summership-26-prs/krish-kagra/why-python/02_What_Python.md new file mode 100644 index 00000000..b3f0f518 --- /dev/null +++ b/summership-26-prs/krish-kagra/why-python/02_What_Python.md @@ -0,0 +1,86 @@ +# 2 What is Python? + +**The Magic Pot that Could Do Anything** + +**Concept:** What is Python? +**Difficulty:** Beginner +**Source:** Indian folk tale + +--- + +## The Story + +In a small village, there lived a poor widow with her son. One day, the son found an old clay pot in the forest. When he brought it home, his mother said, "This pot is useless – it's just an empty pot." + +But the son, being curious, whispered to the pot, "I wish for a bowl of rice." Instantly, the pot filled with steaming rice. They tried again: "I wish for fresh vegetables." And the pot gave them vegetables. The pot could cook, clean, and even tell stories! It was not just a pot – it was a tool that could transform itself to do whatever the user needed. + +--- + +## The Bridge + +Python is like that magic pot. It is a programming language, but it's not limited to one task. You can use it to build websites, analyze data, create games, automate boring tasks, and even control robots — all with the same language. + +| In the story | In Python | Why | +| --------------------------------------------- | ------------------------------------------------ | ------------------------------------------- | +| The pot could produce food, stories, and more | Python can be used for web, data, AI, automation | Python is a general-purpose language | +| The pot followed the user's wish | Python executes the instructions we write | It interprets our code and performs actions | +| The pot was simple to use | Python has a clean, readable syntax | It is beginner-friendly | +| The pot was always ready | Python is open-source and free | Anyone can download and use it | + +--- + +## The Python Code + +```python id="p9x4ab" +# Python can be used for many things + +# 1. Simple arithmetic +print(5 + 3) + +# 2. Working with text +print("Hello, World!") + +# 3. Creating a list +fruits = ["apple", "banana", "cherry"] + +# 4. Making decisions +if len(fruits) > 2: + print("We have enough fruits!") + +# This is the same language for all these tasks. +``` + +--- + +## Check Yourself + +**1. What type of programming language is Python?** + +* A) Low-level +* B) General-purpose +* C) Only for web development +* D) Only for data science + +**2. Which of the following can Python be used for?** + +* A) Building websites +* B) Data analysis +* C) Automating tasks +* D) All of the above + +**3. What makes Python beginner-friendly?** + +* A) It has complex syntax +* B) It has clean, readable code +* C) It is only for experts +* D) It requires a special compiler + +--- + +## Answer Key + +**1. B** — Python is a general-purpose language, meaning it can be used for many types of applications. + +**2. D** — Python is used in web development, data science, automation, and much more. + +**3. B** — Python's syntax is designed to be clean and easy to read, making it great for beginners. diff --git a/summership-26-prs/krish-kagra/why-python/03_Why_Python.md b/summership-26-prs/krish-kagra/why-python/03_Why_Python.md new file mode 100644 index 00000000..a5433bb3 --- /dev/null +++ b/summership-26-prs/krish-kagra/why-python/03_Why_Python.md @@ -0,0 +1,87 @@ +# 3 Why Learn Python? + +**The Wise Parrot and the Three Tasks** + +**Concept:** Why Learn Python? +**Difficulty:** Beginner +**Source:** Indian folk tale + +--- + +## The Story + +A king owned a magical parrot that could speak many languages. One day, the king gave the parrot three tasks: + +1. Tell the court the time every hour. +2. Recite poetry to entertain guests. +3. Find lost objects by describing them. + +The parrot accomplished all tasks with ease. The king asked, "Why are you so capable?" The parrot replied, "I learned the language of the humans, the language of the animals, and the language of the gods. With that, I can do anything." + +--- + +## The Bridge + +Learning Python is like learning that universal language. It opens many doors because it's widely used, has a huge community, and works in many domains. + +| In the story | In Python | Why | +| ----------------------------------------- | --------------------------------------------- | ------------------------------------------ | +| The parrot learned multiple languages | Python is versatile | One language can be used for many purposes | +| The parrot could do three different tasks | Python works for web, data, AI, automation | It saves time learning multiple languages | +| The king trusted the parrot | Python is popular and widely used in industry | Many companies hire Python developers | +| The parrot was easy to talk to | Python code reads like English | It's easy to learn and write | + +--- + +## Why Python is a Great Choice + +* **Easy to learn** – simple syntax, like plain English. +* **Huge community** – millions of developers worldwide; lots of help online. +* **Rich libraries** – thousands of free modules for every need (e.g., Django for web, NumPy for science, TensorFlow for AI). +* **High demand** – many companies use Python, so job opportunities are plentiful. +* **Cross-platform** – runs on Windows, macOS, Linux, and more. + +--- + +## A Simple Python Example + +```python id="v1x7lm" +# This is a simple Python program +name = input("What is your name? ") +print("Hello, " + name + "! Welcome to Python.") +``` + +--- + +## Check Yourself + +**1. Which of the following is a reason to learn Python?** + +* A) It is very difficult to learn +* B) It is used in many different fields +* C) It only runs on one operating system +* D) It has no community support + +**2. What makes Python code easy to understand?** + +* A) It uses many symbols +* B) Its syntax is clean and readable +* C) It is written in binary +* D) It requires memorizing many rules + +**3. Which industries commonly use Python?** + +* A) Web development +* B) Data science +* C) Artificial intelligence +* D) All of the above + +--- + +## Answer Key + +**1. B** — Python is versatile and used in web, data, AI, automation, etc., making it a valuable skill. + +**2. B** — Python's syntax is designed to be clear and readable, like plain English. + +**3. D** — Python is used across all these industries and many more. diff --git a/summership-26-prs/krish-kagra/why-python/04_Features.md b/summership-26-prs/krish-kagra/why-python/04_Features.md new file mode 100644 index 00000000..4ef80e6b --- /dev/null +++ b/summership-26-prs/krish-kagra/why-python/04_Features.md @@ -0,0 +1,97 @@ +# 4 Main Features of Python + +**The Chariot with Six Special Wheels** + +**Concept:** Main Features of Python +**Difficulty:** Beginner +**Source:** Indian folk tale + +--- + +## The Story + +In a prosperous kingdom, the king commissioned a chariot that would be the best in the world. The chariot had six special wheels: + +* One wheel could run on any terrain – sand, rock, water. +* One wheel never lost its grip. +* One wheel was self-repairing. +* One wheel could change size to fit any road. +* One wheel was silent and smooth. +* One wheel made the chariot easy to steer. + +The chariot became famous because it was reliable, adaptable, and powerful — just like Python. + +--- + +## The Bridge + +Python has several standout features that make it a powerful and pleasant language to use. + +| Feature | In the story | Why it matters | +| -------------------------- | ---------------------------------- | ---------------------------------------- | +| **Easy to learn and read** | The wheel that ran on any terrain | Python syntax is simple and clear | +| **Interpreted** | The wheel that never lost its grip | Code runs line by line, easy to test | +| **Dynamically typed** | The self-repairing wheel | You don't need to declare variable types | +| **Object-oriented** | The wheel that changed size | Supports classes and inheritance | +| **Large standard library** | The silent and smooth wheel | Built-in modules for many tasks | +| **Extensible** | The easy-to-steer wheel | Can integrate with C/C++ for performance | + +--- + +## Python Code Showing Features + +```python id="t6k9pz" +# 1. Easy to read +name = "Alice" + +# 2. Dynamically typed – no type declaration needed +age = 30 # integer +age = "thirty" # now a string – no error + +# 3. Object-oriented +class Student: + def __init__(self, name): + self.name = name + + def greet(self): + print("Hello, I am", self.name) + +# 4. Using standard library +import math +print(math.sqrt(16)) # 4.0 +``` + +--- + +## Check Yourself + +**1. Which feature allows Python to run code line by line without compiling first?** + +* A) Dynamically typed +* B) Interpreted +* C) Object-oriented +* D) Extensible + +**2. What does "dynamically typed" mean in Python?** + +* A) You must declare variable types +* B) The type of a variable can change at runtime +* C) Types are fixed forever +* D) Python does not have types + +**3. Python's standard library is:** + +* A) Very small and limited +* B) Large and includes many useful modules +* C) Only for web development +* D) Not included with Python + +--- + +## Answer Key + +**1. B** — Python is an interpreted language, meaning it executes code line by line. + +**2. B** — In dynamically typed languages, the type is determined at runtime and can change. + +**3. B** — Python comes with a large standard library covering many tasks, like math, file I/O, networking, etc. diff --git a/summership-26-prs/krish-kagra/why-python/05_Interpreter_Compiler.md b/summership-26-prs/krish-kagra/why-python/05_Interpreter_Compiler.md new file mode 100644 index 00000000..698fdab8 --- /dev/null +++ b/summership-26-prs/krish-kagra/why-python/05_Interpreter_Compiler.md @@ -0,0 +1,90 @@ +# 5 Compiler vs Interpreter Basics + +**The Two Translators: One for the Whole Book, One for Each Page** + +**Concept:** Compiler vs Interpreter +**Difficulty:** Beginner +**Source:** Indian folk tale · public domain · original retelling + +--- + +## The Story + +In a land of many languages, there were two great translators. + +* **Translator A** would take a whole book, sit in a room, and translate every word at once, producing a new book in the target language. Once done, you could read the whole translation instantly. +* **Translator B** would read the original book aloud, page by page, translating each sentence as they went. The audience heard the translation in real time, but if the translator stumbled on a sentence, they had to stop and figure it out before moving on. + +--- + +## The Bridge + +This is exactly the difference between a **compiler** and an **interpreter**. + +| Translator A (Compiler) | Translator B (Interpreter) | Python's role | +| ------------------------------------------- | -------------------------------------------------- | ------------------------------------------------------------------ | +| Translates the whole program at once | Translates and executes line by line | Python is interpreted (like B) | +| Produces a separate executable file | No separate file – runs directly | Python doesn't produce an exe (though it does bytecode internally) | +| Faster execution later, but slower to start | Starts quickly, but may be slower during execution | Python is usually slower than compiled languages like C | +| Errors are caught at compile time | Errors are caught at runtime | Python catches errors when the line runs | + +--- + +## A Quick Comparison Table + +| Feature | Compiler | Interpreter | +| ----------- | ------------------------ | --------------------------- | +| Translation | All at once | Line by line | +| Output | Executable file | Direct execution | +| Speed | Generally faster | Generally slower | +| Debugging | Harder (need to rebuild) | Easier (immediate feedback) | +| Examples | C, C++, Rust | Python, JavaScript, Ruby | + +Python is **interpreted**, but it does compile to bytecode internally (an intermediate step) to improve performance — but the user still experiences it as an interpreted language. + +--- + +## Python Example (Interpreted) + +```python id="z4p1kn" +# This code runs line by line +print("This is line 1") +print("This is line 2") +# If there's a syntax error on line 3, line 1 and 2 still run +print("This line has an error" # missing closing parenthesis +``` + +--- + +## Check Yourself + +**1. Which of the following best describes an interpreter?** + +* A) Translates the entire program before running +* B) Translates and executes code line by line +* C) Creates a separate executable file +* D) Only works for compiled languages + +**2. What is a key advantage of using a compiler?** + +* A) Faster startup time +* B) Faster overall execution after compilation +* C) Easier debugging +* D) No need to compile + +**3. Python is primarily an interpreted language. What is a typical outcome of that?** + +* A) Code runs faster than compiled languages +* B) You can test code quickly without a build step +* C) You cannot run code on different operating systems +* D) Errors are always caught before execution + +--- + +## Answer Key + +**1. B** — An interpreter translates and executes code line by line. + +**2. B** — Compiled programs run faster after the initial compilation because the translation is done upfront. + +**3. B** — Because Python is interpreted, you can run and test code immediately without a separate compilation step, which speeds up development.