You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Feb 27, 2026. It is now read-only.
Copy file name to clipboardExpand all lines: content/python/concepts/conditionals/conditionals.md
+37Lines changed: 37 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -91,3 +91,40 @@ else:
91
91
```
92
92
93
93
What do you think will happen if `pH` is changed to 7?
94
+
95
+
96
+
```
97
+
## Nested If Statements
98
+
99
+
Nested `if` statements are used when you want to check a condition inside another condition. This is useful when decisions depend on multiple factors. The outer if runs first, and only `if` it evaluates to `True` does the inner `if` get evaluated.
100
+
101
+
Nested `if` statements help make your code more organized and allow more complex decision-making without repeating conditions.
102
+
103
+
```py
104
+
age = 20
105
+
has_membership = True
106
+
107
+
if age >= 18:
108
+
print("You are an adult.")
109
+
110
+
if has_membership:
111
+
print("You can enter the club.")
112
+
else:
113
+
print("You need a membership to enter.")
114
+
else:
115
+
print("You are too young to enter.")
116
+
```
117
+
118
+
Explanation:
119
+
120
+
1. The outer if checks if the person is 18 or older.
121
+
122
+
2. Only if the outer condition is True, the inner if checks whether they have a membership.
123
+
124
+
3. This prevents repeating the age check for every membership condition.
0 commit comments