Home Python Tutorial Python if else

Python if else

by anupmaurya
5 minutes read

The Python if else statement executes a block of code, if a specified condition holds true. If the condition is false, another block of code can be executed using the else statement.

Python if else is commonly used with operators, like the comparison or logical operators to create the conditional statements.

Python if Statement

The if statement is used to test a particular condition and if the condition is true, it executes a block of code known as if-block. The condition of if statement can be any valid logical expression that can be either evaluated to true or false.

Python if Statement Syntax

if test expression:
    statement(s)

Here, the program evaluates the test expression and will execute statement(s) only if the test expression is True.

If the test expression is False, the statement(s) is not executed.

Example of if

#input an= number check whether it is even
num = int(input("enter the number?"))  

#num=4

if num%2 == 0:  
    print("Number is even")  

Output

Number is even

Python if-else statement

The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition.

If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.

Python if..else Statement Syntax

if test expression:
    Body of if
else:
    Body of else

Example of if…else

# Program checks if the number is positive or negative
# And displays an appropriate message

num = 3

# Try these two variations as well. 
# num = -5
# num = 0

if num >= 0:
    print("Positive or Zero")
else:
    print("Negative number")

Output

Positive or Zero

Python elif statement

The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. We can have any number of elif statements in our program depending upon our need. However, using elif is optional.

The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement.

Python elif Statement Syntax

if test expression:
    Body of if
elif test expression:
    Body of elif
else: 
    Body of else

Example of elif

'''In this program, 
we check if the number is positive or
negative or zero and 
display an appropriate message'''

num = 3.4

if num > 0:
    print("Positive number")
elif num == 0:
    print("Zero")
else:
    print("Negative number")

Output

Positive number

Python Nested if statements

We can have a if...elif...else statement inside another if...elif...else statement. This is called nesting in computer programming.

Any number of these statements can be nested inside one another. Indentation is the only way to figure out the level of nesting. They can get confusing, so they must be avoided unless necessary.

Python elif Statement Syntax

if test expression:
       if test expression:
              Body of if
      else test expression:
              Body of else
else: 
    Body of else

Python Nested if Example

'''In this program, we input a number
check if the number is positive or
negative or zero and display
an appropriate output using nested if statement'''

num = float(input("Enter a number: "))
if num >= 0:
    if num == 0:
        print("Zero")
    else:
        print("Positive number")
else:
    print("Negative number")

Output 1

Enter a number: 6
Positive number

You may also like

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.