Skip to content

Commit 900741a

Browse files
committed
Added interactive quizzes in Python docs modules
1 parent d3f2e08 commit 900741a

22 files changed

+10046
-71
lines changed

docs/python/conditional-statements-python.md

Lines changed: 206 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,37 @@ else:
8080

8181
---
8282

83+
### 🧠 Quiz 1: Basic Conditionals
84+
85+
**Question 1:** What will be the output of the following code?
86+
```python
87+
x = 7
88+
if x > 10:
89+
print("Large")
90+
else:
91+
print("Small")
92+
```
93+
94+
<details>
95+
<summary>Click to reveal answer</summary>
96+
97+
**Answer:** `Small`
98+
99+
**Explanation:** Since 7 is not greater than 10, the condition `x > 10` is False, so the `else` block executes and prints "Small".
100+
</details>
101+
102+
**Question 2:** What does `elif` stand for in Python?
103+
104+
<details>
105+
<summary>Click to reveal answer</summary>
106+
107+
**Answer:** "else if"
108+
109+
**Explanation:** `elif` is short for "else if" and is used to check multiple conditions in sequence.
110+
</details>
111+
112+
---
113+
83114
## Nested `if` Statements
84115

85116
You can put an `if` statement inside another `if` statement.
@@ -110,6 +141,44 @@ Adult
110141

111142
---
112143

144+
### 🧠 Quiz 2: Ternary Operator & Nested Conditions
145+
146+
**Question 1:** What will the variable `result` contain after this code runs?
147+
```python
148+
temperature = 25
149+
result = "Hot" if temperature > 30 else "Cold"
150+
```
151+
152+
<details>
153+
<summary>Click to reveal answer</summary>
154+
155+
**Answer:** `"Cold"`
156+
157+
**Explanation:** Since 25 is not greater than 30, the condition is False, so the value after `else` ("Cold") is assigned to `result`.
158+
</details>
159+
160+
**Question 2:** What will be printed by this nested if statement?
161+
```python
162+
x = 12
163+
if x > 5:
164+
if x < 15:
165+
print("Valid")
166+
else:
167+
print("Too high")
168+
else:
169+
print("Too low")
170+
```
171+
172+
<details>
173+
<summary>Click to reveal answer</summary>
174+
175+
**Answer:** `Valid`
176+
177+
**Explanation:** First, `x > 5` is True (12 > 5), so we enter the outer if block. Then, `x < 15` is also True (12 < 15), so "Valid" is printed.
178+
</details>
179+
180+
---
181+
113182
## Logical Operators in Conditions
114183

115184
You can combine multiple conditions using `and`, `or`, and `not`.
@@ -140,6 +209,45 @@ if "apple" in fruits:
140209

141210
---
142211

212+
### 🧠 Quiz 3: Logical Operators
213+
214+
**Question 1:** What will be the output?
215+
```python
216+
a = 10
217+
b = 20
218+
if a > 5 and b < 15:
219+
print("Both true")
220+
else:
221+
print("At least one false")
222+
```
223+
224+
<details>
225+
<summary>Click to reveal answer</summary>
226+
227+
**Answer:** `At least one false`
228+
229+
**Explanation:** While `a > 5` is True (10 > 5), `b < 15` is False (20 is not less than 15). Since `and` requires both conditions to be True, the result is False and the else block executes.
230+
</details>
231+
232+
**Question 2:** What does this code print?
233+
```python
234+
x = 8
235+
if not x == 8:
236+
print("Not eight")
237+
else:
238+
print("Is eight")
239+
```
240+
241+
<details>
242+
<summary>Click to reveal answer</summary>
243+
244+
**Answer:** `Is eight`
245+
246+
**Explanation:** `x == 8` is True, but the `not` operator negates it to False. So the if condition fails and the else block runs, printing "Is eight".
247+
</details>
248+
249+
---
250+
143251
## Indentation Rules
144252

145253
In Python, indentation is important for defining code blocks.
@@ -178,7 +286,7 @@ Write a Python program to check whether a number is **even** or **odd**.
178286

179287
#### 3. **Age Eligibility for Voting**
180288

181-
Write a program to take a persons age as input and check if they are **eligible to vote** (18 years or older).
289+
Write a program to take a person's age as input and check if they are **eligible to vote** (18 years or older).
182290

183291

184292
#### 4. **Largest of Two Numbers**
@@ -229,10 +337,104 @@ Write a program to input a single character and check whether it is:
229337

230338
Write a program that asks the user to enter a **username** and **password**.
231339

232-
* If the username is `"admin"` and the password is `"12345"`, print **“Login Successful”**.
233-
* Otherwise, print **“Invalid credentials”**.
340+
* If the username is `"admin"` and the password is `"12345"`, print **"Login Successful"**.
341+
* Otherwise, print **"Invalid credentials"**.
342+
343+
---
344+
345+
### 🧠 Quiz 4: Code Output Prediction
346+
347+
**Question 1:** What will this code output?
348+
```python
349+
marks = 82
350+
if marks >= 90:
351+
print("A")
352+
elif marks >= 75:
353+
print("B")
354+
elif marks >= 60:
355+
print("C")
356+
else:
357+
print("D")
358+
```
359+
360+
<details>
361+
<summary>Click to reveal answer</summary>
362+
363+
**Answer:** `B`
364+
365+
**Explanation:** The code checks conditions in order. Since 82 is not ≥ 90 but is ≥ 75, it prints "B" and stops checking further conditions.
366+
</details>
367+
368+
**Question 2:** Predict the output:
369+
```python
370+
fruits = ["apple", "banana", "cherry"]
371+
if "mango" in fruits:
372+
print("Found")
373+
else:
374+
print("Not found")
375+
```
376+
377+
<details>
378+
<summary>Click to reveal answer</summary>
379+
380+
**Answer:** `Not found`
381+
382+
**Explanation:** "mango" is not in the fruits list, so the condition is False and the else block executes.
383+
</details>
234384

385+
---
386+
387+
### 🧠 Quiz 5: Challenge Questions
388+
389+
**Question 1:** What's wrong with this code?
390+
```python
391+
x = 5
392+
if x > 3:
393+
print("Greater")
394+
```
395+
396+
<details>
397+
<summary>Click to reveal answer</summary>
398+
399+
**Answer:** Missing indentation
400+
401+
**Explanation:** The print statement must be indented to be inside the if block. Python uses indentation to define code blocks, and this code will raise an `IndentationError`.
402+
</details>
403+
404+
**Question 2:** What will be printed?
405+
```python
406+
year = 2024
407+
if year % 4 == 0 and year % 100 != 0:
408+
print("Leap")
409+
elif year % 400 == 0:
410+
print("Leap")
411+
else:
412+
print("Not Leap")
413+
```
414+
415+
<details>
416+
<summary>Click to reveal answer</summary>
417+
418+
**Answer:** `Leap`
419+
420+
**Explanation:** 2024 is divisible by 4 (2024 % 4 == 0) and not divisible by 100 (2024 % 100 == 24, not 0), so the first condition is True and "Leap" is printed.
421+
</details>
422+
423+
**Question 3:** Write a single line of code using a ternary operator to assign "Pass" to a variable `result` if `score` is 50 or more, otherwise assign "Fail".
424+
425+
<details>
426+
<summary>Click to reveal answer</summary>
427+
428+
**Answer:**
429+
```python
430+
result = "Pass" if score >= 50 else "Fail"
431+
```
432+
433+
**Explanation:** The ternary operator format is `value_if_true if condition else value_if_false`.
434+
</details>
435+
436+
---
235437

236438
## Conclusion
237439

238-
Conditional statements are essential for decision-making in programs. Mastering `if`, `elif`, and `else` allows you to control your program's logic effectively.
440+
Conditional statements are essential for decision-making in programs. Mastering `if`, `elif`, and `else` allows you to control your program's logic effectively.

0 commit comments

Comments
 (0)