Skip to content

Commit c5155c2

Browse files
authored
Update conditionals.py
1 parent c85d646 commit c5155c2

1 file changed

Lines changed: 83 additions & 33 deletions

File tree

‎03-control-flow/conditionals.py‎

Lines changed: 83 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
"""
1+
"""
22
Module: conditionals.py
33
Topic: Conditional Statements in Python
44
Level: Beginner
55
66
This file teaches you about:
77
- if statement
8-
- if-else statement
9-
- if-elif-else statement
8+
- if-else statement
9+
- if-elif-else statement
1010
- Ternary operator
1111
- Nested conditionals
1212
- Logical operators in conditions
@@ -286,54 +286,104 @@ def format_name(name):
286286

287287
# Structural pattern matching (Python 3.10+)
288288
# This is similar to switch-case in other languages
289-
290-
status_code = 404
291-
292-
match status_code:
293-
case 200:
289+
# Note: This requires Python 3.10+, so we use a version check
290+
291+
import sys
292+
if sys.version_info >= (3, 10):
293+
status_code = 404
294+
295+
match status_code:
296+
case 200:
297+
message = "OK"
298+
case 201:
299+
message = "Created"
300+
case 400:
301+
message = "Bad Request"
302+
case 404:
303+
message = "Not Found"
304+
case 500:
305+
message = "Internal Server Error"
306+
case _:
307+
message = "Unknown status"
308+
309+
print(f"Status {status_code}: {message}")
310+
311+
# Match with patterns
312+
point = (3, 4)
313+
else:
314+
# Fallback for Python < 3.10
315+
status_code = 404
316+
if status_code == 200:
294317
message = "OK"
295-
case 201:
318+
elif status_code == 201:
296319
message = "Created"
297-
case 400:
320+
elif status_code == 400:
298321
message = "Bad Request"
299-
case 404:
322+
elif status_code == 404:
300323
message = "Not Found"
301-
case 500:
324+
elif status_code == 500:
302325
message = "Internal Server Error"
303-
case _:
326+
else:
304327
message = "Unknown status"
305328

306-
print(f"Status {status_code}: {message}")
307-
308-
# Match with patterns
309-
point = (3, 4)
310-
311-
match point:
312-
case (0, 0):
329+
print(f"Status {status_code}: {message} (using if-elif for Python < 3.10)")
330+
331+
# Match with patterns - fallback
332+
point = (3, 4)
333+
334+
if sys.version_info >= (3, 10):
335+
match point:
336+
case (0, 0):
337+
print("Origin")
338+
case (x, 0):
339+
print(f"On x-axis at {x}")
340+
case (0, y):
341+
print(f"On y-axis at {y}")
342+
case (x, y):
343+
print(f"Point at ({x}, {y})")
344+
345+
# Match with guards
346+
number = 15
347+
348+
match number:
349+
case n if n < 0:
350+
result = "negative"
351+
case n if n == 0:
352+
result = "zero"
353+
case n if n > 0 and n <= 10:
354+
result = "small positive"
355+
case n if n > 10:
356+
result = "large positive"
357+
case _:
358+
result = "unknown"
359+
360+
print(f"\n{number} is {result}")
361+
else:
362+
# Fallback for Python < 3.10
363+
x, y = point
364+
if x == 0 and y == 0:
313365
print("Origin")
314-
case (x, 0):
366+
elif y == 0:
315367
print(f"On x-axis at {x}")
316-
case (0, y):
368+
elif x == 0:
317369
print(f"On y-axis at {y}")
318-
case (x, y):
370+
else:
319371
print(f"Point at ({x}, {y})")
320372

321-
# Match with guards
322-
number = 15
323-
324-
match number:
325-
case n if n < 0:
373+
# Match with guards - fallback
374+
number = 15
375+
if number < 0:
326376
result = "negative"
327-
case n if n == 0:
377+
elif number == 0:
328378
result = "zero"
329-
case n if n > 0 and n <= 10:
379+
elif number > 0 and number <= 10:
330380
result = "small positive"
331-
case n if n > 10:
381+
elif number > 10:
332382
result = "large positive"
333-
case _:
383+
else:
334384
result = "unknown"
335385

336-
print(f"\n{number} is {result}")
386+
print(f"\n{number} is {result} (using if-elif for Python < 3.10)")
337387

338388
# =============================================================================
339389
# SECTION 9: COMMON PATTERNS

0 commit comments

Comments
 (0)