|
1 | | -""" |
| 1 | +""" |
2 | 2 | Module: conditionals.py |
3 | 3 | Topic: Conditional Statements in Python |
4 | 4 | Level: Beginner |
5 | 5 |
|
6 | 6 | This file teaches you about: |
7 | 7 | - if statement |
8 | | -- if-else statement |
9 | | -- if-elif-else statement |
| 8 | +- if-else statement |
| 9 | +- if-elif-else statement |
10 | 10 | - Ternary operator |
11 | 11 | - Nested conditionals |
12 | 12 | - Logical operators in conditions |
@@ -286,54 +286,104 @@ def format_name(name): |
286 | 286 |
|
287 | 287 | # Structural pattern matching (Python 3.10+) |
288 | 288 | # 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: |
294 | 317 | message = "OK" |
295 | | - case 201: |
| 318 | + elif status_code == 201: |
296 | 319 | message = "Created" |
297 | | - case 400: |
| 320 | + elif status_code == 400: |
298 | 321 | message = "Bad Request" |
299 | | - case 404: |
| 322 | + elif status_code == 404: |
300 | 323 | message = "Not Found" |
301 | | - case 500: |
| 324 | + elif status_code == 500: |
302 | 325 | message = "Internal Server Error" |
303 | | - case _: |
| 326 | + else: |
304 | 327 | message = "Unknown status" |
305 | 328 |
|
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: |
313 | 365 | print("Origin") |
314 | | - case (x, 0): |
| 366 | + elif y == 0: |
315 | 367 | print(f"On x-axis at {x}") |
316 | | - case (0, y): |
| 368 | + elif x == 0: |
317 | 369 | print(f"On y-axis at {y}") |
318 | | - case (x, y): |
| 370 | + else: |
319 | 371 | print(f"Point at ({x}, {y})") |
320 | 372 |
|
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: |
326 | 376 | result = "negative" |
327 | | - case n if n == 0: |
| 377 | + elif number == 0: |
328 | 378 | result = "zero" |
329 | | - case n if n > 0 and n <= 10: |
| 379 | + elif number > 0 and number <= 10: |
330 | 380 | result = "small positive" |
331 | | - case n if n > 10: |
| 381 | + elif number > 10: |
332 | 382 | result = "large positive" |
333 | | - case _: |
| 383 | + else: |
334 | 384 | result = "unknown" |
335 | 385 |
|
336 | | -print(f"\n{number} is {result}") |
| 386 | + print(f"\n{number} is {result} (using if-elif for Python < 3.10)") |
337 | 387 |
|
338 | 388 | # ============================================================================= |
339 | 389 | # SECTION 9: COMMON PATTERNS |
|
0 commit comments