DEV Community

Cover image for Learning Conditional Statements in Python
David Ansa
David Ansa

Posted on

Learning Conditional Statements in Python

Introduction

Conditional statements are an essential part of programming that allows you to execute certain pieces of code based on specific conditions. In Python, these are typically implemented using if, elif, and else statements. This guide will help you understand how to use these statements effectively.

Basic if Statement

The if statement allows you to execute a block of code only if a specified condition is true.

Syntax

if condition:
    # code to execute if condition is true
Enter fullscreen mode Exit fullscreen mode

Example

x = 10
if x > 5:
    print("x is greater than 5")
Enter fullscreen mode Exit fullscreen mode

if-else Statement

The if-else statement allows you to execute one block of code if the condition is true and another block of code if the condition is false.

Syntax

if condition:
    # code to execute if condition is true
else:
    # code to execute if condition is false
Enter fullscreen mode Exit fullscreen mode

Example

x = 3
if x > 5:
    print("x is greater than 5")
else:
    print("x is not greater than 5")
Enter fullscreen mode Exit fullscreen mode

if-elif-else Statement

The if-elif-else statement is used to check multiple conditions. The first condition that evaluates to true will have its block of code executed, and the rest will be skipped.

Syntax

if condition1:
    # code to execute if condition1 is true
elif condition2:
    # code to execute if condition2 is true
else:
    # code to execute if none of the above conditions are true
Enter fullscreen mode Exit fullscreen mode

Example

x = 7
if x > 10:
    print("x is greater than 10")
elif x > 5:
    print("x is greater than 5 but less than or equal to 10")
else:
    print("x is 5 or less")
Enter fullscreen mode Exit fullscreen mode

Nested if Statements

You can also nest if statements within each other to check multiple conditions.

Syntax

if condition1:
    if condition2:
        # code to execute if both condition1 and condition2 are true
Enter fullscreen mode Exit fullscreen mode

Example

x = 8
y = 12
if x > 5:
    if y > 10:
        print("x is greater than 5 and y is greater than 10")
Enter fullscreen mode Exit fullscreen mode

Logical Operators

Logical operators such as and, or, and not can be used to combine multiple conditions in a single if statement.

Example with and

x = 7
y = 12
if x > 5 and y > 10:
    print("Both conditions are true")
Enter fullscreen mode Exit fullscreen mode

Example with or

x = 4
y = 12
if x > 5 or y > 10:
    print("At least one condition is true")
Enter fullscreen mode Exit fullscreen mode

Example with not

x = 4
if not x > 5:
    print("x is not greater than 5")
Enter fullscreen mode Exit fullscreen mode

Simple Program to Illustrate Conditional Statements

Here is a simple Python program to illustrate the use of conditional statements (if, elif, and else).

# Get user input
age = int(input("Enter your age: "))

# Check the age and print the appropriate message
if age < 0:
    print("Invalid age")
elif age < 18:
    print("You are a minor.")
elif age <= 65:
    print("You are an adult.")
else:
    print("You are a senior.")
Enter fullscreen mode Exit fullscreen mode

Output

age = int(input("Enter your age: "))

Conclusion

Conditional statements are a fundamental part of controlling the flow of your Python programs. By using if, elif, and else statements effectively, along with logical operators, you can create more complex and useful programs. Practice writing your own conditional statements to become more comfortable with these concepts.

Top comments (0)