DEV Community

Avnish
Avnish

Posted on

Understanding Python's if...else Statement with Examples

Conditional statements are an essential part of programming. They allow a program to execute certain blocks of code only when specific conditions are met. This article will cover the if, if...else, and if...elif...else statements in Python, complete with examples for clarity.


1. Python if Statement

The if statement is used to execute a block of code only if a condition evaluates to True.

Syntax:

if condition:
    # Code to execute if the condition is True
Enter fullscreen mode Exit fullscreen mode
  • The condition is a Boolean expression (e.g., number > 0) that evaluates to either True or False.

Example:

number = int(input('Enter a number: '))

if number > 0:
    print(f'{number} is a positive number.')

print('This statement always executes.')
Enter fullscreen mode Exit fullscreen mode

Output 1:

Enter a number: 10
10 is a positive number.
This statement always executes.
Enter fullscreen mode Exit fullscreen mode

Output 2:

Enter a number: -5
This statement always executes.
Enter fullscreen mode Exit fullscreen mode

2. Indentation in Python

Python uses indentation to define code blocks. Failing to use proper indentation will raise an error.

Correct Example:

x = 1
total = 0

if x != 0:
    total += x
    print(total)  

print("This statement always executes.")
Enter fullscreen mode Exit fullscreen mode

Incorrect Example:

x = 1
total = 0

if x != 0:
total += x
print(total)  # This will cause an indentation error
Enter fullscreen mode Exit fullscreen mode

3. Python if...else Statement

The else clause can be used with if to specify an alternative block of code to execute if the condition evaluates to False.

Syntax:

if condition:
    # Code block if condition is True
else:
    # Code block if condition is False
Enter fullscreen mode Exit fullscreen mode

Example:

number = int(input('Enter a number: '))

if number > 0:
    print('Positive number')
else:
    print('Not a positive number')

print('This statement always executes.')
Enter fullscreen mode Exit fullscreen mode

Output 1:

Enter a number: 10
Positive number
This statement always executes.
Enter fullscreen mode Exit fullscreen mode

Output 2:

Enter a number: -2
Not a positive number
This statement always executes.
Enter fullscreen mode Exit fullscreen mode

4. Python if...elif...else Statement

When there are multiple conditions to evaluate, the if...elif...else statement is used.

Syntax:

if condition1:
    # Code block 1
elif condition2:
    # Code block 2
else:
    # Code block 3
Enter fullscreen mode Exit fullscreen mode

Example:

number = int(input('Enter a number: '))

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

print('This statement always executes.')
Enter fullscreen mode Exit fullscreen mode

Output:

Enter a number: -5
Negative number
This statement always executes.
Enter fullscreen mode Exit fullscreen mode
  • Only one block of code is executed, even if multiple conditions are True.

5. Python Nested if Statements

You can nest an if statement inside another if statement to handle more complex scenarios.

Example:

number = int(input('Enter a number: '))

if number >= 0:
    if number == 0:
        print('Number is 0')
    else:
        print('Number is positive')
else:
    print('Number is negative')
Enter fullscreen mode Exit fullscreen mode

Output:

Enter a number: 5
Number is positive
Enter fullscreen mode Exit fullscreen mode

6. Real-World Example: Grading System

Let’s apply these concepts to a real-world scenario: assigning grades to students based on their scores.

Example:

score = int(input('Enter your score: '))

if score > 90:
    grade = 'A'
elif score > 75:
    grade = 'B'
elif score > 65:
    grade = 'C'
else:
    grade = 'F'

print(f'Your grade is: {grade}')
Enter fullscreen mode Exit fullscreen mode

Output 1:

Enter your score: 88
Your grade is: B
Enter fullscreen mode Exit fullscreen mode

Output 2:

Enter your score: 64
Your grade is: F
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

  • if executes code only when the condition is True.
  • if...else provides an alternative when the condition is False.
  • if...elif...else handles multiple conditions.
  • Proper indentation is crucial in Python to define code blocks.

With these examples and explanations, you should now have a clear understanding of how to use conditional statements in Python effectively!

Top comments (0)