Python if.else Statement | Explained With Examples

Python provides several decision-driven statements to control the flow of a program based on some specific conditions. The if..else is one of the conditional/decision-driven statements used to test a specific condition. In Python, the if block executes when the specified condition is true, while the else block executes when the given condition is false. The “If” statements are used in Python along with “else” and “elif” statements.

This Python guide will give you a detailed analysis of Python’s “if-else” statement with numerous examples. The following points are demonstrated in this article:

So let’s start his guide!

What is Python if-else Statement?

The “if” statement, along with the combination of the “else” statement, is used to execute both the “True” and “False” conditions of the given expression. When the given condition becomes “True”, then the statement written inside the “if” block executes. But when the condition of the “if” statement becomes “False”, then the statement written inside the “else” block will be executed. The syntax is shown in the below snippet:

if expression:
   code statement(s)
else:
   code statement(s)

In the above syntax, the “if” statement is used along with the condition or expression, and the code statements are written inside the “if” block. The statement executes only when the condition is fulfilled/satisfied.

Let’s have a look at the first example of the simple “if-else” statement:

Example 1: Simple if-else Statement in Python

In the following example code, the simple if-else statement is initialized with a condition to demonstrate its working:

Code:

value = 4

if value < 0:
    print("The Given Value is Negative number")
else:
    print("The Given Value is Positive or Zero")

In the snippet:

  • A variable named “value” is initialized with a positive integer in the program.
  • The “if” statement is defined with the condition to check if the value is smaller than or equal to “0”. If Yes, then the statements are written inside the “if” block executes.
  • Similarly, if the condition of the “if” statement does not satisfy, then the else block code executes and returns the result accordingly.
  • In this case, the “if” statement does not satisfy the specified criteria, so the else block statement executes.

Output:

The above output shows that the input value is positive or zero

Example 2: Nested if-else Statement in Python

In the example given below, the nested “if-else” statements are used to verify the condition and execute their respective block statement.

Code:

value = 4

if value >= 0:
    if value == 0:
        print("The Given Value is Zero")
    else:
        print("The Given Value is Positive number")
else:
    print("The Given Value is Negative number")

In the above-given code:

  • The “nested if-else” statement is initialized with conditions in the program.
  • If the “if” statement condition becomes true, then the program will look into the nested condition and execute the statement according to the nested condition.
  • In the above code, if the input value is greater or equal to “0,” then the outer condition is satisfied, and now the Python interpreter checks the nested condition. In the nested condition, if the value is equal to “0,” then the “if” block statement executes; otherwise, if the value is greater than zero, then the “else” block statement executes.

Output:

The above output shows that the given value is a positive number.

Example 3: if-elif-else Statement in Python

In the example given below, the “if-else” statement with multiple “elif” statements is used in the Python program.

Marks = 66

if Marks <= 40:
   print('F Grade' ,Marks)
elif Marks <= 50:
   print('D Grade' ,Marks)

elif Marks <= 60:
   print('C Grade',Marks)

elif Marks <= 70:
   print('B Grade',Marks)

elif Marks <= 80:
   print('B+ Grade',Marks)

elif Marks <= 90:
   print('A Grade',Marks)

elif Marks <= 100:
   print('A+ Grade',Marks)
else:
   print('Invalid Number',Marks)

Code:

In the above code:

  • The integer value is initialized in the program and stored in a variable named “Marks”.
  • The “if” statement with multiple “elif” statements is initialized with conditions.
  • The “elif” statement executes the block code if their respective condition is fulfilled.

Output:

The above output shows the grade based on the input number.

That’s it from this guide!

Conclusion

In Python, the “if-else” statement can execute a block of code when the given condition becomes “True” or “False”. When the condition becomes “True,” the “if” block executes, and when the condition becomes “False,” the “else” block executes. The “Nested if-else” statement executes multiple combinations of conditions and shows the most accurate results or to-the-point decisions. The “elif” statement is used in Python when multiple “True ”conditions are used. This article presented a detailed overview of Python’s “if-else” statement with numerous examples.