Friday, December 27, 2024
Google search engine
HomeLanguagesPython if elif else Statement with Example

Python if elif else Statement with Example

In this post, you will learn python if, if else, if elif else statement and python if statement multiple conditions (python Nested if statement) in detail with example.

When you do programming in any programming language. So many times you have to put conditions in your programs. So for this one should know about the condition statement. In this tutorial, we will teach you about the python decision-making statement/condition statement only.

What are if elif else statement in Python?

This is a Decision-making statement in python programming. Which is used to execute code only if a certain condition is fulfilled?

Python if Statement

In Python, If Statement is used for decision making. It will execute the block of code only when the IF statement is true.

Python if Statement Syntax

if test expression:
    statement(s)

Python if Statement Flowchart

Example of python if statement

num = 5
if num > 0:
    print(num, "is a positive number.")

print("This is always printed.")

num = -5
if num > 0:
    print(num, "is a positive number.")
    
print("This is also always printed.")

Explanation of the above program

In the above example, num > 0 is the test condition. The group of code is executed only if the test condition is met True.

In the first test condition, When a variable num is greater than zero than the test condition met true and execute a block of code.

In the second test condition, If variable num is equal to -5, the test condition is false and the block of code is not executed, or skipped.

Python if…else Statement

In the If..else statement, the condition test is first of all. If the condition is found to be true, the code inside the if the statement is executed. The code inside the other else statement is executed.

Python if…else Statement Syntax

if test expression:
    STATEMENT1
else:
    STATEMENT2

Python if…else Statement Flowchart

Example of python if…else statement

num = 4

if num%2 == 0:
    print("This is even number")
else:
    print("This is odd number")

Explanation of the above program

We have created a program above that is using the if … else statement.

We have created this program to check even and odd numbers. If the number given in the test condition is even, then the message of “This is even number” will be printed. The message of “This is odd number” will be printed

Python if..elif..else Statement

This statement first tests the condition of if statement. If the condition of if the statement is false. Then the condition test of the elif statement is done. And if the condition of elif statement is also false, then the code inside the else statement is executable.

Syntax of if..elif..else statement

if EXPRESSION1:
    STATEMENT1
elif: EXPRESSION2:
    STATEMENT2
else:
    STATEMENT3

Python if..elif..else Statement Flowchart

Example of python if…else statement

num = int(input('Enter a number: '))

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

Explanation of the above program

In this program, we have taken input from the user.

When the number the user will input is greater than 5, then the code for inside if statement will be execute.

Whne the number the user will input will be greater than 10 or equal to 10 , then the code of the elif statement will be executed.

If both the statement are false, then the code of else statement will be execute.

Python Nested if statements

Nested if statement in python, it means that you can write recursively the if statement even if within the condition.

python if statement multiple conditions examples

num = float(input("Enter a number: "))

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

Explanation of the above program

In this program, we have taken input from the user.

If the number the user will input is greater than 0, then the code for inside if statement will be execute.

after that again, if statement will execute with else statment according to given test condition.

If, the first if statement false, then the else statement will be inside the statement.

RELATED ARTICLES

Most Popular

Recent Comments