Conditional statements (if, elif, else) in python

Conditional statements (if, elif, else) in python

In conditional statements in Python programming, you can execute different blocks of program code based on testing some Python program based single or multiple conditions as true or false using conditional program logic (if, elif, and else). While the if statement checks a single program condition as true or false, the if, elif, else statement tests a single program condition out of multiple conditions and prints it on the console screen.

Conditional statements (if elif else) in python

So let’s understand the if, elif, else statement better in Python programming.

Python if statement.

If statement is used to test a single condition in Python programming. If the program code given by you if condition is true, then here the if statement executes the source code block inside the given if statement in the program code.

Python if statement.

# If the condition is true then execute this block

Statement

Statement

Statement

Python if statement example.

p = 9

if p > 7:

print(” p value is greater than 7″)

Python elif condition statement.

elif (else if) statement in Python programming Python programmers can test multiple programming conditions simultaneously. If the if statement condition in the given program logic is false, then it tests the condition of the next given elif condition block, if the elif condition here becomes false, then it tests all the given elif program conditions in the same way. Here if any one condition in if elif is true, then it executes the corresponding program source block immediately.

Python if condition.

# it execute the given code when if condition is true

statement

statement

elif condition:

# it check given elif condition when above if condition is false

statement

statement

elif condition:

# similarly it check and Execute given elif condition

statement

statement

else:

# else code of block execute when above if and elif all condition is false

statement

statement

Example.

p = 9

if p > 6:

    print(“p value is greater than 6”)

elif p == 6:

    print(“p values is equal to 6”)

else:

    print(“p value is less than 6”)

Python else Statement.

The else statement in Python programs is used as the end statement of the if or elif statement, and the else statement executes the source code block when none of the logic in the if condition and elif condition is true.

if condition:

# it executes the given if block source code when the condition is true

statement

statement

else:

# it executes the given if block source code when the condition is false

statement

statement

python else statement Example.

p = 9

if p > 1:

print(“p value is greater than 1”)

else:

print(“p value is less than or equal to 1”)

Python nested if statements.

You can test more than one nested condition in an if else statement to handle multiple program conditions or more complex program conditions in a Python program. More than one else logical condition in an if condition is called nested if statement condition.

if condition:

if condition:

# it Execute number of if block if condition are true

Statement

Statement

else condition:

# it Execute the block else block when above if condition block is false

Statement

Statement

else:

# it Execute when above all condition block is false

Statement

Statement

Example.

p = 9

if p > 7:

print(“p values is equal to 9”)

else p == 9:

print(“p value is greater than 7 but not greater than 9”)

else:

print(“p values is smaller than or equal to 7”)

You can use logical operators (and, or, not) to combine multiple programming condition logic within a single if, elif, or while statement in a single program.

In Python, if condition statements test only one condition. Whereas if elif statements execute more than one if and else statements when both the if and elif conditions are false.