HTML CSS JAVASCRIPT PYTHON JAVA

Python If Else

Python if...else statements are used to perform different actions based on different conditions. They help in decision making in programs.

Python If Statement

The if statement is used to check a condition. If the condition is true, the code inside the block is executed.

Example
x = 10

if x > 5:
    print("x is greater than 5")
Try this code

Python If Else Statement

The if...else statement executes one block of code if the condition is true, and another block if it is false.

Example
x = 3

if x > 5:
    print("x is greater than 5")
else:
    print("x is less than or equal to 5")
Try this code

Python Elif Statement

The elif keyword is used to check multiple conditions.

Example
x = 5

if x > 5:
    print("Greater than 5")
elif x == 5:
    print("Equal to 5")
else:
    print("Less than 5")
Try this code

Nested If Statements

You can use an if statement inside another if statement.

Example
x = 10

if x > 5:
    if x < 15:
        print("x is between 5 and 15")
Try this code

Short Hand If

Python allows you to write simple if statements in one line.

Example
x = 10

if x > 5: print("x is greater than 5")
Try this code

Why If Else is Important