Python Conditional Statements

Conditional Statements in Python: Structuring Decisions with Precision

Apr 12, 2026

Conditional Statements


Conditional statements in Python are used to make decisions in a program. They allow the code to execute different blocks based on whether a specified condition is `True` or `False`.

In simple terms, they control the flow of execution depending on conditions.


If Statement

The if statement is the simple decision-making statement. This type of control statement is also called a one-way selection statement, because it consists of a condition and just a statement or block of statements. It is used to decide whether certain statements will be executed or not i.e if a condition is true then a block of statements is executed otherwise not. The Syntax of the if statement is:

if condition:
    # Statements to execute if condition is true

The if decision control flow statement starts with if keyword and ends with a colon. The condition in an if statement should be a boolean expression. Here, the condition after evaluation will be either true or false. If the value is true then it will execute the block of statements below it otherwise not. The condition can be written in brackets ( ) also. Python uses indentation to identify a block. All the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. So the block under an if statement will be identified as shown in the below.

if condition:
    statement1
    statement2

Example:

# Test whether a given number is even or not
n = int(input("Enter the number: "))
if n % 2 == 0:
    print(n, 'is even number')
print('End')

Output:

Enter the number: 6
6 is even number
End

If-else Statement

The if statement condition is true it will execute a block of statements otherwise not. If we want to do something else if the condition is false then the else statement is useful. The else statement executes a block of code when the if condition is false. else statement is optional. An else statement does not have any condition.

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

Example:

# Test whether a given number is even or not
n = int(input("Enter the number: "))
if n % 2 == 0:
    print(n, 'is even number')
else:
    print(n, ' is odd')
print('Execution is completed')

Output:

Enter the number: 5
5  is odd
Execution is completed

If-elif-else

The user can decide among multiple options. The if statements are executed from the top down. As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed. If none of the conditions is true, then the final else statement will be executed.

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

Example:

# Print Grades for a Score between 0.0 and 1.0
# Grade-A(>=0.9), B(>=0.7), C(<0.7)
# If the Score Is Out of Range, Print an Error
score = float(input('Enter your score: '))
if score < 0 or score > 1:
    print('Wrong input is given')
elif score >= 0.9:
    print('Your Grade is A')
elif score >= 0.7:
    print('Your Grade is B')
else:
    print('Your Grade is C')

Output:

Enter your score: 0.8
Your Grade is B

Nested-if

A nested if is an if statement that is the target of another if statement. Nested if statements mean an if statement inside another if statement. Python allows us to use nested if statements.

if (condition1):
    # Executes when condition1 is true
    if (condition2):
        # Executes when condition2 is true

Example:

# Program to Check if a given year is a leap year
year = int(input('Enter year: '))
if year % 4 == 0:
    if year % 100 == 0:
        if year % 400 == 0:
            print(f'{year} is a leap year')
        else:
            print(f'{year} is not leap year')
    else:
        print(f'{year} is a leap year')
else:
    print(f'{year} is not a leap year')

Output:

Enter year: 2012
2012 is a leap year