Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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.
Loading