Python if else statement

Basic if-else statement:

if the situation x > 5 is True, the primary block of code is achieved. Otherwise, the block under the else declaration is done.

                                  
                                    x = 10

if x > 5:
    print("x is greater than 5")
else:
    print("x is not greater than 5")
                                  
                                

Nested if-else statements:

You can nest if-else statements internal every different to create extra complicated situations.

                                  
                                    a = 15

if a > 10:
    print("a is greater than 10")
    
    if a % 2 == 0:
        print("a is even")
    else:
        print("a is odd")
else:
    print("a is not greater than 10")
                                  
                                

Ternary if-else statement:

The ternary if-else announcement is a concise way to jot down an if-else assertion in a single line.

                                      
                                        score = 85
result = "Pass" if score >= 70 else "Fail"
print(result)
                                      
                                    

Practical Example:

In this example, this system tests if the user's age is 18 or older. If it's miles, it prints "You are an person." Otherwise, it prints "You are a minor."

                                      
                                        user_age = 25

if user_age >= 18:
    print("You are an adult.")
else:
    print("You are a minor.")
                                      
                                    

Understanding and using if-else statements is fundamental for constructing conditional logic for your Python packages. They allow you to deal with one of a kind situations and make selections based on situations.