Paramclasses Academy - Blog

Tutorials Details

←
Post 5 of 20
→

Python Conditional Statements: if, else, elif, Nested If & Practical Examples

Python Conditional Statements: if, else, elif, Nested If & Practical Examples

October 16, 2026

Python Conditional Statements: if, else, elif, Nested If & Practical Examples

Introduction to Conditional Statements in Python

Conditional statements allow a Python program to make decisions based on whether a condition is true or false. They help control the flow of a program and execute different blocks of code depending on specific conditions.

Python provides if, else, and elif statements to implement decision-making logic. You can also use nested if statements to check multiple conditions.

In this tutorial, you will learn how conditional statements work, understand their syntax, and practise them with simple Python examples.

1. The if Statement in Python

The if statement executes a block of code only when its condition evaluates to True.

Syntax:

if condition:
    # Code executes when the condition is True

Example:

age = 20

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

Output:

You are eligible to vote.

Since age is 20, the condition age >= 18 is true, so Python executes the indented statement.

2. The if-else Statement

The if-else statement provides two possible paths. Python executes the if block when the condition is true and the else block when it is false.

Example:

age = 16

if age >= 18:
    print("You are an adult.")
else:
    print("You are not an adult.")

Output:

You are not an adult.

This structure is useful when a program needs to choose between two alternatives.

3. The if-elif-else Statement

The elif keyword means “else if.” It allows you to check multiple conditions in sequence.

Python checks the conditions from top to bottom and executes the first matching block. If none of the conditions is true, the else block executes.

Example:

marks = 75

if marks >= 90:
    print("Grade A")
elif marks >= 75:
    print("Grade B")
elif marks >= 50:
    print("Grade C")
else:
    print("Needs Improvement")

Output:

Grade B

This example demonstrates a simple grading system. The order of the conditions matters because Python stops after finding the first true condition.

4. Nested if Statements

A nested if statement is an if statement placed inside another if statement. It allows a program to check an additional condition after the first condition is satisfied.

Example:

age = 22
has_id = True

if age >= 18:
    if has_id:
        print("Entry allowed.")
    else:
        print("Please show your ID.")
else:
    print("Entry not allowed.")

Output:

Entry allowed.

Nested conditions are useful when a decision depends on multiple levels of checks. For complex conditions, logical operators such as and and or may sometimes provide a simpler alternative.

5. Using Logical Operators with Conditions

Logical operators help combine multiple conditions in a single statement.

  • and: Both conditions must be true.

  • or: At least one condition must be true.

  • not: Reverses a Boolean result.

Example:

age = 25
has_license = True

if age >= 18 and has_license:
    print("You can drive.")
else:
    print("You are not eligible to drive.")

Output:

You can drive.

Here, both conditions must be true for the program to display the permission message.

6. Checking Positive, Negative, or Zero

Conditional statements can classify numbers based on their values.

Example:

number = -5

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

Output:

Negative number

This is a common beginner programming exercise that demonstrates multiple conditions.

7. Checking Even or Odd Numbers

The modulus operator % returns the remainder after division. If a number has a remainder of zero when divided by 2, it is even; otherwise, it is odd.

Example:

number = 8

if number % 2 == 0:
    print("Even number")
else:
    print("Odd number")

Output:

Even number

You can change the value of number to test the program with different integers.

8. Python Indentation in Conditional Statements

Indentation is essential in Python because it defines which statements belong to a code block. Python commonly uses four spaces for each indentation level.

Correct example:

age = 20

if age >= 18:
    print("Eligible")
    print("Condition is satisfied")

print("Program finished")

The first two print() statements belong to the if block. The last statement is outside the block, so it executes regardless of whether the condition is true.

Incorrect indentation can cause an IndentationError or change how the program behaves.

9. Practical Example: Student Result System

The following program accepts a student's marks and displays a result category.

marks = int(input("Enter your marks (0-100): "))

if marks < 0 or marks > 100:
    print("Invalid marks. Enter a value from 0 to 100.")
elif marks >= 90:
    print("Result: Grade A")
elif marks >= 75:
    print("Result: Grade B")
elif marks >= 50:
    print("Result: Grade C")
else:
    print("Result: Needs Improvement")

This example combines user input, comparison operators, logical operators, and conditional statements. It also checks whether the entered marks fall within the valid range.

Note that int() expects a valid whole-number input. If the user enters non-numeric text, Python raises a ValueError; input validation with exception handling can be added to make the program more robust.

10. Practice Questions

Try solving these exercises to improve your understanding:

  1. What is a conditional statement in Python?

  2. What is the difference between if and if-else?

  3. What is the purpose of the elif keyword?

  4. What is a nested if statement?

  5. Why is indentation important in Python?

  6. Write a program to check whether a number is positive, negative, or zero.

  7. Write a program to check whether a number is even or odd.

  8. Create a program that checks whether a person is eligible to vote.

  9. Write a program that assigns grades based on marks.

  10. Write a program that checks whether a number is divisible by both 3 and 5.

Conclusion

Conditional statements are an essential part of Python programming because they allow programs to make decisions and execute code based on specific conditions. By understanding if, else, elif, nested conditions, logical operators, and indentation, you can build programs that respond intelligently to different inputs.

Practise these examples and experiment with different conditions to strengthen your Python fundamentals.

←
Post 5 of 20
→