All categories
    PythonBeginner

    Conditionals

    Python conditional statements: if, elif, else, ternary expressions, comparison operators, and the match statement for control flow.

    01if, elif, else

    main.py
    score = 72
    if score >= 90:
        print("A")
    elif score >= 70:
        print("B")
    else:
        print("C")
    Output
    B

    02Ternary expression

    main.py
    age = 20
    status = "adult" if age >= 18 else "minor"
    print(status)
    Output
    adult

    03Logical operators

    main.py
    a, b = 5, 10
    print(a < b and b < 20)
    print(a > b or b > 5)
    print(not a == b)
    Output
    True
    True
    True

    04Membership testing

    main.py
    fruits = ["apple", "banana"]
    print("apple" in fruits)
    print("cherry" not in fruits)
    Output
    True
    True

    05Match statement

    main.py
    command = "start"
    match command:
        case "start":
            print("Starting")
        case "stop":
            print("Stopping")
        case _:
            print("Unknown")
    Output
    Starting