Python − if-else Statement

In computer programming, the if statement is considered a conditional statement. It will function to run the block of code only when the specific condition is met. This article has listed the Python -if-else statement.

Hence, the conditional statement in Python language will determine the direction of the program execution.

Types of Control Flow in Python

Several types of control flow are given below:

a. The if statement

b. The if-else statement

c.The nested-if statement

d. The if-elif-else ladder

What is Python if statement?

The if statement is referred to as the simple decision-making statement. Hence, it can decide whether a certain statement or block of statements will be executed.

Syntax:

if condition:
   # Statements to execute if
   # condition is true

However, this condition after doing the evaluation will come as either true or False whereas, if statements will accept boolean values. The block of statements will run only if the value comes true.

It is known that Python will use indentation to find a block. Look at the example provided below to identify the block under the if statement.

if condition:
   statement1
statement2
# Here if the condition is true, if block 
# will consider only statement1 to be inside 
# its block.

Example of Python if statement

The Python if statement will be executed, only if the condition that exists in the if statement came false.

# Example 1: Basic if statement
x = 10

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

Output

x is greater than 5

What is Python’s If-Else Statement?

The if-else statement will inform us that the condition statement is true and will run the block of statements. Otherwise, it won’t run the code.

Whereas, it is possible to use the if statement when the condition becomes false.

Syntax of Python If-Else.

if (condition):
    # Executes this block if
    # condition is true
else:
    # Executes this block if
    # condition is false

Using Python if-else statement

The else statement will follow the block of code and will run when the condition that is present in the if statement is false.

# Example of if-else statement
age = 18

if age >= 18:
    print("You are eligible to vote.")
else:
    print("You are not eligible to vote yet.")

Output

You are eligible to vote.

Python if else statement in a List Comprehension

Let us look into the example given below:

# Example of if-else statement in a List Comprehension
numbers = [1, 2, 3, 4, 5, 6]

# Create a new list where even numbers are squared, and odd numbers are cubed
result = [x**2 if x % 2 == 0 else x**3 for x in numbers]

print(result)

Output

[1, 4, 27, 16, 125, 36]

What is a Needed-if statement in Python?

A nested if is referred to as an if statement that is the target of another statement. A nested if statement refers to the if statement inside another if statement.

Python will add the nest if statements within the if statements

Syntax

if (condition1):
   # Executes when condition1 is true
   if (condition2): 
      # Executes when condition2 is true
   # if Block is end here
# if Block is end here

Example of Python Nested if statement

The example provided below will illustrate the nested if conditions in the code. Therefore, the conditions will be executed one by one.

# Example of nested if statement
x = 10
y = 5

if x > 5:
    print("x is greater than 5")
    
    if y > 2:
        print("y is greater than 2")
    else:
        print("y is not greater than 2")

else:
    print("x is not greater than 5")

Output

x is greater than 5
y is greater than 2

What is Python if-else-else Ladder?

The user can plan among the multiple options. If statements will be run from the top down.

The statements will be run only if the conditions are true. The rest of the ladder will be bypassed. Otherwise, the final statement will be run.

Syntax

if (condition):
    statement
elif (condition):
    statement
.
.
else:
    statement

Example of Python if-else-else ladder

# Example of if-else-else ladder
x = 15

if x > 10:
    print("x is greater than 10")
elif x > 5:
    print("x is greater than 5 but not 10")
else:
    print("x is not greater than 5")

Output

x is greater than 10

What is a Short Hand if statement?

Shorthand will be used when there is only a single statement to be run.

if condition: statement

Example

# Shorthand version using ternary conditional operator
x = 15

result = "x is greater than 10" if x > 10 else "x is greater than 5 but not 10" if x > 5 else "x is not greater than 5"

print(result)

Output

i is less than 15

What is a Short Hand if-else statement?

This short-hand if-else statement functions to write the if-else statements in a single line. Hence, only one statement will be necessary for both the if and else blocks.

Syntax

statement_when_True if condition else statement_when_False

Example

# Shorthand version using ternary conditional operator
x = 8

result = "x is greater than 5" if x > 5 else "x is not greater than 5"

print(result)

Output

x is greater than 5

Conclusion

To conclude, the if-else statement in Python provides a straightforward way to introduce decision-making into your code. It allows you to execute different blocks of code based on whether a given condition is true or false.

However, the simplicity of the syntax makes it easy to understand, and it forms the foundation for more complex control flow structures in Python. By using if-else, you can create flexible and readable code that adapts to various scenarios, enhancing the logic and functionality of your Python programs.

Python if-else Statement- FAQs

Q1. What is the if-else statement in Python?

Ans. In Python, if-else helps make decisions in your code. If a condition is true, a specific set of code runs; if false, another set of code runs. It’s a way to choose between different actions based on whether a condition is met or not, making your code more adaptable and easy to follow.

Q2. Can you write 2 if statements in Python?

Ans. Yes, it is possible to write the if statement inside the other if statement.

Q3. What is the syntax of if else if?

Ans. If a condition is true, the code inside the IF statement runs. Otherwise, the code inside the ELSE statement runs.
If a condition is true, the code inside the IF statement runs. After that, the code inside the ELSE-IF statement is checked.

Hridhya Manoj

Hello, I’m Hridhya Manoj. I’m passionate about technology and its ever-evolving landscape. With a deep love for writing and a curious mind, I enjoy translating complex concepts into understandable, engaging content. Let’s explore the world of tech together

Leave a Comment