|
| 1 | +# 🗓️ Day 13 — Iterative Statements (For Loop) |
| 2 | + |
| 3 | +## 🎯 Topics Covered |
| 4 | +- What is a `for` Loop? |
| 5 | +- Syntax of `for` Loop |
| 6 | +- Using `range()` Function |
| 7 | +- Iterating over Strings, Lists, and Tuples |
| 8 | + |
| 9 | +--- |
| 10 | + |
| 11 | +## 🔁 What is a `for` Loop? |
| 12 | + |
| 13 | +- The **`for` loop** is used when you want to **iterate (repeat)** over a sequence like a list, tuple, string, or range of numbers. |
| 14 | +- Unlike the `while` loop (which depends on a condition), the `for` loop runs **for a specific number of iterations** or elements in a sequence. |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## 🧠 Syntax: |
| 19 | +```python |
| 20 | +for variable in sequence: |
| 21 | + # block of code |
| 22 | +``` |
| 23 | + |
| 24 | +### ✅ Here: |
| 25 | + |
| 26 | +- variable → takes the value of each element from the sequence |
| 27 | +- sequence → can be a list, tuple, string, or range() |
| 28 | + |
| 29 | +--- |
| 30 | + |
| 31 | +## 💻 Examples — Using range() |
| 32 | + |
| 33 | +The range() function generates a sequence of numbers. |
| 34 | + |
| 35 | +```python |
| 36 | +for i in range(1, 6): |
| 37 | + print(i) |
| 38 | +``` |
| 39 | + |
| 40 | +## 🧠 Output: |
| 41 | +```python |
| 42 | +1 |
| 43 | +2 |
| 44 | +3 |
| 45 | +4 |
| 46 | +5 |
| 47 | +``` |
| 48 | + |
| 49 | +## ✅ Explanation: |
| 50 | + |
| 51 | +- range(1, 6) → starts from 1 and stops before 6. |
| 52 | +- The loop executes 5 times (for 1 to 5). |
| 53 | + |
| 54 | +--- |
| 55 | + |
| 56 | +## 💻 Example 2 — Iterating Over a String |
| 57 | +```python |
| 58 | +for ch in "PYTHON": |
| 59 | + print(ch) |
| 60 | +``` |
| 61 | + |
| 62 | +🧠 Output: |
| 63 | +```python |
| 64 | +P |
| 65 | +Y |
| 66 | +T |
| 67 | +H |
| 68 | +O |
| 69 | +N |
| 70 | +``` |
| 71 | + |
| 72 | +> ✅ The loop goes through each character of the string "PYTHON" one by one. |
| 73 | +
|
| 74 | +--- |
| 75 | + |
| 76 | +## 💻 Example 3 — Iterating Over a List |
| 77 | +```python |
| 78 | +fruits = ["apple", "banana", "cherry"] |
| 79 | + |
| 80 | +for fruit in fruits: |
| 81 | + print(fruit) |
| 82 | +``` |
| 83 | + |
| 84 | +## 🧠 Output: |
| 85 | +```python |
| 86 | +apple |
| 87 | +banana |
| 88 | +cherry |
| 89 | +``` |
| 90 | + |
| 91 | +> ✅ Each element in the list fruits is printed in order. |
| 92 | +
|
| 93 | +--- |
| 94 | + |
| 95 | +## 💻 Example 4 — Using range() with Step Value |
| 96 | + |
| 97 | +You can specify a step to skip numbers in between. |
| 98 | +```python |
| 99 | +for i in range(2, 11, 2): |
| 100 | + print(i) |
| 101 | +``` |
| 102 | + |
| 103 | +## 🧠 Output: |
| 104 | +```python |
| 105 | +2 |
| 106 | +4 |
| 107 | +6 |
| 108 | +8 |
| 109 | +10 |
| 110 | +``` |
| 111 | + |
| 112 | +> ✅ The loop runs from 2 to 10 with a step of 2 (even numbers only). |
| 113 | +
|
| 114 | +--- |
| 115 | + |
| 116 | +## 🧠 Summary |
| 117 | + |
| 118 | +| Concept | Description | Example | |
| 119 | +| -------------------------- | --------------------------- | ------------------------- | |
| 120 | +| `for` Loop | Iterates through a sequence | `for x in range(5):` | |
| 121 | +| `range(start, stop, step)` | Generates numbers | `range(1, 10, 2)` | |
| 122 | +| Nested Loop | Loop inside another loop | `for i in... for j in...` | |
| 123 | +| `break` | Stops loop immediately | `if i==5: break` | |
| 124 | +| `continue` | Skips current iteration | `if i==3: continue` | |
| 125 | +| `pass` | Does nothing (placeholder) | `for i in range(5): pass` | |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +📘 *Next step (Day 14):* |
| 130 | + |
| 131 | +I’ll explore **Control Structure - Jumping Statements**. |
0 commit comments