Python If-Else Statement

The if-else statement in Python allows you to make decisions in your code. It lets you execute different blocks of code based on whether a condition is true or false. This is one of the most essential features in programming and helps you build more dynamic, responsive programs.

With an if statement, you can check a condition, and if it evaluates to true, Python will execute the block of code inside it. If the condition is false, you can use an else block to execute a different set of instructions.



What You'll Learn

In this section, you'll learn how to use the if and else statements in Python to create simple decision-making logic. We’ll explore the syntax and give practical examples to help you understand how these statements work.


Understanding the if-else Statement

The basic syntax for the if-else statement is:

python
if condition:
    # Block of code to execute if condition is True
else:
    # Block of code to execute if condition is False
  • condition: The expression that you want to evaluate (it should return a Boolean value, True or False).
  • if: This block runs if the condition evaluates to True.
  • else: This block runs if the condition evaluates to False. It’s optional, but can be useful when you need a default case.

Example 1: Basic If Statement

You can use an if statement to check if a number is positive:

python
num = 1
if num > 0:
    print("The number is positive.")

How It Works:

  • number = 1: We define a variable number with the value 1.
  • if number > 0: : This condition checks if the value of number is greater than 0. If True, it prints that the number is positive.

Output

The number is positive.
Example-1 Flowchart Image
Flowchart showing the decision process for checking if a number is positive.

Example 2: Using If-Else to Check Even or Odd

Here’s an example where we check if a number is even or odd:

python
number = 2
if number % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

How It Works:

  • number = 2: We define a number to check.
  • if number % 2 == 0:: This condition checks if the remainder when dividing the number by 2 is zero (i.e., if the number is even).
  • else: If the condition is False (the number is odd), the code in the else block will run.

Output

The number is even.
Example-2 Flowchart Image
Flowchart illustrating how the program determines if a number is even or odd.

Example 3: Checking for Equality

You can also use if-else to check if two values are equal:

python
x = 5
y = 5
if x == y:
    print("x and y are equal.")
else:
    print("x and y are not equal.")

How It Works:

  • x = 5: We define two variables to compare.
  • if x == y:: This condition checks if x is equal to y. If True, it prints that they are equal.
  • else: If the condition is False, it prints that they are not equal.

Output

x and y are equal.
Example-3 Flowchart Image
Flowchart showing the equality comparison between two variables.

Example 4: Using If-Else-If (elif) to Check Positive, Negative, or Zero

Here’s an example where we check if a number is positive, negative, or zero:

python
number = 0

if number > 0:
    print("The number is positive.")
elif number < 0:
    print("The number is negative.")
else:
    print("The number is zero.")

How It Works:

  • number = 0: We define a number to check.
  • if number > 0:: This condition checks if the number is positive.
  • elif number < 0:: If the number is not positive, this checks if it's negative.
  • else: If neither condition is true, the number must be zero, and the code in the else block runs.

Output

The number is zero.
Example-4 Flowchart Image
Flowchart depicting how a number is evaluated as positive, negative, or zero.

Example 5: Using Nested If-Else to Check Positive, Negative, or Zero, and Even or Odd

Here’s an example where we check if a number is positive, negative, or zero, and if it's positive, we also check if it’s even or odd:

python
number = 4

if number > 0:
    if number % 2 == 0:
        print("The number is positive and even.")
    else:
        print("The number is positive and odd.")
elif number < 0:
    print("The number is negative.")
else:
    print("The number is zero.")

How It Works:

  • number = 4: We define a number to check.
  • if number > 0:: First, we check if the number is positive.
  • if number % 2 == 0:: If the number is positive, we check if it’s even using a nested `if` statement.
  • else: If the number is positive but not even, we know it must be odd.
  • elif number < 0: : If the number is negative, the code in this block runs.
  • else: If the number is neither positive nor negative, it must be zero, and the code in this block will run.

Output

The number is positive and even.
Example-5 Flowchart Image
Flowchart showing nested if-else logic to determine positivity and parity of a number.

Exercises

Try out the following exercises to practice using the if-else statement:

1. Write a program that checks if a number is even or odd.
python
# Exercise 1: Check if a number is even or odd
number = 7
if number % 2 == 0:
    print("The number is even.")
else:
    print("The number is odd.")

2. Write a program that checks if a person is eligible to vote (age >= 18).
python
# Exercise 2: Check voting eligibility
age = 20
if age >= 18:
    print("Eligible to vote.")
else:
    print("Not eligible to vote.")

3. Write a program that prints "Hello, World!" if a variable is True, and "Goodbye!" if False.
python
# Exercise 3: Print based on condition
isTrue = True
if isTrue:
    print("Hello, World!")
else:
    print("Goodbye!")

*Tip: Experiment with different conditions and statements to see how the code behaves!


Frequently Asked Questions

What is an if-else statement in Python?

An if-else statement in Python is used to execute different blocks of code based on whether a condition evaluates to True or False. It's a fundamental concept in decision-making logic.


What is the difference between if, elif, and else?

if checks the initial condition, elif checks additional conditions if the previous ones were false, and else runs a block of code when all previous conditions are false. You can have multiple elif blocks but only one if and one else.


Can I use multiple conditions in a single if statement?

Yes! You can use logical operators like and, or, and not to combine multiple conditions within a single if statement.


What happens if I forget to indent the code under an if statement?

Python relies on indentation to define code blocks. If you forget to indent the code under an if or else, you’ll get an IndentationError.


Can if-else statements be nested in Python?

Yes, you can nest if-else statements inside each other to create more complex decision structures. Just make sure to manage your indentation properly.



What's Next?

Next, you'll dive into while loops in Python, which allow you to repeat a block of code as long as a certain condition is true. You'll learn how to write while loops, control their execution, and use them effectively to build more dynamic and flexible programs.