# match-case statementcommand = "start"match command: case "start": print("Starting...") case "stop": print("Stopping...") case _: print("Unknown")#ans: Starting...
# multiple valuesstatus_code = 201match status_code: case 200 | 201: print("Success") case 404: print("Not Found") case _: print("Other")#ans: Success
# underscore is wildcardx = 100match x: case 1: print("One") case 2: print("Two") case _: print("Default")#ans: Default
# no match, no outputx = 5match x: case 1: print("One") case 2: print("Two")#ans: (nothing)
# what prints?x = 2match x: case 1: print("One") case 2: print("Two") case _: print("Other")#ans: Two
# default case?x = 100match x: case 1: print("One") case _: print("Default")#ans: Default
# multiple values?x = 201match x: case 200 | 201: print("Success") case _: print("Other")#ans: Success
# no match?x = 5match x: case 1: print("One") case 2: print("Two")#ans: nothing (no default, no match)
# first match wins?x = 1match x: case 1: print("First") case 1: print("Second")#ans: First
# match with string?cmd = "quit"match cmd: case "start": result = "Starting" case "quit": result = "Quitting"#ans: result is "Quitting"
# wildcard position?x = 999match x: case _: print("Any") case 999: print("999")#ans: Any (wildcard matches first)
# or pattern?x = 503match x: case 500 | 502 | 503: print("Server Error")#ans: Server Error
# match None?x = Nonematch x: case None: print("Is None") case _: print("Not None")#ans: Is None
# match with guard? (advanced)x = 15match x: case n if n > 10: print("Big") case _: print("Small")#ans: Big
Google tag (gtag.js)