B.Tech Students: Apply for Live Programming Internship C, C++, Java, Python ,Web page Designing, PHP Python match case Statement | LiveCodeProgramming

Python match case Statement

Python introduced match-case in version 3.10 as a more readable and structured way to handle multiple conditions, similar to switch-case in other languages.

Python 3.10 से match-case एक नया feature है, जो कई conditions को manage करने के लिए switch-case जैसा है।

Example: Match Day Name

day = "Monday"

match day:
    case "Monday":
        print("Start of the week")
    case "Friday":
        print("Weekend is coming")
    case "Sunday":
        print("Relax, it's Sunday")
    case _:
        print("Regular weekday")

Output:

Start of the week

Dynamic Input Example

num = int(input("Enter a number: "))

match num:
    case 1:
        print("One")
    case 2:
        print("Two")
    case 3:
        print("Three")
    case _:
        print("Other number")

Output (for input 2):

Two