|
| 1 | +# Python Control Structures: Master Program Flow🚦🔀 |
| 2 | + |
| 3 | +Control structures determine how your Python programs make decisions and repeat actions. They're the backbone of programming logic! |
| 4 | + |
| 5 | +--- |
| 6 | + |
| 7 | +## 🌟 **1. Conditional Statements (Decision Making)** |
| 8 | + |
| 9 | +Make your programs smart by executing different code blocks based on conditions. |
| 10 | + |
| 11 | +### `if` Statement |
| 12 | +```python |
| 13 | +age = 18 |
| 14 | +if age >= 18: |
| 15 | + print("🎉 You are an adult!") |
| 16 | +# Output: 🎉 You are an adult! |
| 17 | +``` |
| 18 | + |
| 19 | +### `if-else` Statement |
| 20 | +```python |
| 21 | +age = 16 |
| 22 | +if age >= 18: |
| 23 | + print("🍻 Cheers! You can vote.") |
| 24 | +else: |
| 25 | + print("📚 Focus on school!") |
| 26 | +# Output: 📚 Focus on school! |
| 27 | +``` |
| 28 | + |
| 29 | +### `if-elif-else` Ladder |
| 30 | +```python |
| 31 | +score = 87 |
| 32 | + |
| 33 | +if score >= 90: |
| 34 | + grade = 'A' |
| 35 | +elif score >= 80: |
| 36 | + grade = 'B' # ← This executes |
| 37 | +elif score >= 70: |
| 38 | + grade = 'C' |
| 39 | +else: |
| 40 | + grade = 'Need improvement' |
| 41 | + |
| 42 | +print(f"📊 Your grade: {grade}") |
| 43 | +# Output: 📊 Your grade: B |
| 44 | +``` |
| 45 | + |
| 46 | +### Nested `if` |
| 47 | +```python |
| 48 | +num = 15 |
| 49 | + |
| 50 | +if num > 0: |
| 51 | + if num % 2 == 0: |
| 52 | + print("➕ Positive even number") |
| 53 | + else: |
| 54 | + print("➕ Positive odd number") # ← This executes |
| 55 | +else: |
| 56 | + print("❌ Number is not positive") |
| 57 | +``` |
| 58 | + |
| 59 | +--- |
| 60 | + |
| 61 | +## 🔄 **2. Looping Statements** |
| 62 | + |
| 63 | +Automate repetitive tasks efficiently. |
| 64 | + |
| 65 | +### `for` Loop |
| 66 | +```python |
| 67 | +# Loop through a list |
| 68 | +fruits = ["🍎", "🍌", "🍒"] |
| 69 | +for fruit in fruits: |
| 70 | + print(f"Fruit: {fruit}") |
| 71 | + |
| 72 | +# Using range() |
| 73 | +for i in range(3): # 0, 1, 2 |
| 74 | + print(f"Count: {i}") |
| 75 | + |
| 76 | +for i in range(5, 8): # 5, 6, 7 |
| 77 | + print(f"Number: {i}") |
| 78 | + |
| 79 | +for i in range(0, 10, 3): # 0, 3, 6, 9 |
| 80 | + print(f"Step: {i}") |
| 81 | +``` |
| 82 | + |
| 83 | +### `while` Loop |
| 84 | +```python |
| 85 | +count = 0 |
| 86 | +while count < 3: |
| 87 | + print(f"🚀 Launch in {3-count}...") |
| 88 | + count += 1 |
| 89 | +# Output: |
| 90 | +# 🚀 Launch in 3... |
| 91 | +# 🚀 Launch in 2... |
| 92 | +# 🚀 Launch in 1... |
| 93 | +``` |
| 94 | + |
| 95 | +### Nested Loops |
| 96 | +```python |
| 97 | +for row in range(3): |
| 98 | + for col in range(2): |
| 99 | + print(f"📍 Cell ({row},{col})") |
| 100 | +# Outputs all combinations from (0,0) to (2,1) |
| 101 | +``` |
| 102 | + |
| 103 | +--- |
| 104 | + |
| 105 | +## ⚡ **3. Loop Control Statements** |
| 106 | + |
| 107 | +Fine-tune your loop execution. |
| 108 | + |
| 109 | +| Statement | Effect | Example Use Case | |
| 110 | +|------------|---------------------------------|-------------------------------| |
| 111 | +| `break` | Exits the entire loop | Searching → exit when found | |
| 112 | +| `continue` | Skips current iteration | Skip invalid data | |
| 113 | +| `pass` | Does nothing (placeholder) | Structure code before filling | |
| 114 | + |
| 115 | +### `break` Example |
| 116 | +```python |
| 117 | +for num in range(10): |
| 118 | + if num == 5: |
| 119 | + print("🛑 Stopping at 5!") |
| 120 | + break |
| 121 | + print(num) # Prints 0-4 |
| 122 | +``` |
| 123 | + |
| 124 | +### `continue` Example |
| 125 | +```python |
| 126 | +for num in range(5): |
| 127 | + if num == 2: |
| 128 | + print("⏩ Skipping 2") |
| 129 | + continue |
| 130 | + print(num) # Prints 0,1,3,4 |
| 131 | +``` |
| 132 | + |
| 133 | +### `pass` Example |
| 134 | +```python |
| 135 | +for num in range(3): |
| 136 | + if num == 1: |
| 137 | + pass # TODO: Add logic later |
| 138 | + print(num) # Prints 0,1,2 |
| 139 | +``` |
| 140 | + |
| 141 | +--- |
| 142 | + |
| 143 | +## 🎁 **4. Special Features** |
| 144 | + |
| 145 | +### `else` with Loops |
| 146 | +Executes when loop finishes normally (no `break`): |
| 147 | +```python |
| 148 | +for n in range(3): |
| 149 | + print(n) |
| 150 | +else: |
| 151 | + print("✅ Loop completed successfully!") |
| 152 | + |
| 153 | +# Output: |
| 154 | +# 0 |
| 155 | +# 1 |
| 156 | +# 2 |
| 157 | +# ✅ Loop completed successfully! |
| 158 | +``` |
| 159 | + |
| 160 | +### Ternary Operator (One-line `if-else`) |
| 161 | +```python |
| 162 | +age = 20 |
| 163 | +can_vote = "Yes" if age >= 18 else "No" |
| 164 | +print(f"Voting eligible? {can_vote}") # Output: Yes |
| 165 | +``` |
| 166 | + |
| 167 | +### Walrus Operator (Python 3.8+) |
| 168 | +Assign and check in one expression: |
| 169 | +```python |
| 170 | +while (food := input("🍕 Favorite food? ")) != "quit": |
| 171 | + print(f"Yummy {food}!") |
| 172 | +# Keeps asking until user types "quit" |
| 173 | +``` |
| 174 | + |
| 175 | +--- |
| 176 | + |
| 177 | +## 🏆 **Best Practices** |
| 178 | + |
| 179 | +✅ **Keep conditions simple** |
| 180 | +```python |
| 181 | +# Good |
| 182 | +if is_valid and has_permission: |
| 183 | + ... |
| 184 | + |
| 185 | +# Avoid |
| 186 | +if (x > 5 and y < 10) or (z == 0 and not flag): |
| 187 | + ... |
| 188 | +``` |
| 189 | + |
| 190 | +✅ **Limit nesting depth** |
| 191 | +```python |
| 192 | +# Use functions instead of: |
| 193 | +if condition1: |
| 194 | + if condition2: |
| 195 | + if condition3: |
| 196 | + ... |
| 197 | +``` |
| 198 | + |
| 199 | +✅ **Choose the right loop** |
| 200 | +- `for` when you know iterations |
| 201 | +- `while` for dynamic conditions |
| 202 | + |
| 203 | +✅ **Prevent infinite loops** |
| 204 | +```python |
| 205 | +# Always have an exit condition! |
| 206 | +while True: |
| 207 | + user_input = input("Type 'exit': ") |
| 208 | + if user_input == "exit": |
| 209 | + break |
| 210 | +``` |
| 211 | + |
| 212 | +✅ **Use descriptive loop variables** |
| 213 | +```python |
| 214 | +# Good |
| 215 | +for student in classroom: |
| 216 | + ... |
| 217 | + |
| 218 | +# Avoid |
| 219 | +for x in y: |
| 220 | + ... |
| 221 | +``` |
| 222 | + |
| 223 | +--- |
| 224 | + |
| 225 | +## 🧩 **Quick Reference Table** |
| 226 | + |
| 227 | +| Structure | Syntax Example | Use Case | |
| 228 | +|-------------------|-----------------------------------|------------------------------| |
| 229 | +| `if-else` | `if x > 0: ... else: ...` | Binary decisions | |
| 230 | +| `elif` ladder | `if x>90: ... elif x>80: ...` | Multiple conditions | |
| 231 | +| `for` loop | `for item in collection: ...` | Iterating known sequences | |
| 232 | +| `while` loop | `while condition: ...` | Until condition changes | |
| 233 | +| `break` | `if found: break` | Early exit | |
| 234 | +| `continue` | `if invalid: continue` | Skip iteration | |
| 235 | + |
| 236 | +--- |
| 237 | + |
| 238 | +Now you're equipped to control your Python programs like a pro! 🐍💪 |
| 239 | +Remember: Good program flow leads to clean, efficient code. Happy coding! 💻✨ |
0 commit comments