free geoip
33

Python If-Else Statements Exercises

Python is one of the most popular programming languages, especially for beginners who want to learn coding logically and efficiently.…

Python is one of the most popular programming languages, especially for beginners who want to learn coding logically and efficiently. One of the core building blocks in Python programming is the if-else statement. This control flow allows developers to create conditions, make decisions, and build interactive programs. In this article, you will find a collection of Python if-else statements exercises complete with code examples and real-life cases that will strengthen your understanding.

What is If-Else in Python?

The if-else statement in Python allows you to execute a block of code when a condition is true, and another block of code when the condition is false. This logical decision-making process is essential for building applications, handling user input, and automating tasks.

# Basic if-else example
number = 10

if number > 5:
    print("The number is greater than 5")
else:
    print("The number is less than or equal to 5")

In the example above, Python checks the condition number > 5. Since the number is 10, the first block executes and prints the message.

Why Practice If-Else Exercises?

  • To improve logical thinking skills in problem-solving.
  • To understand real-life decision-making in programming.
  • To prepare for coding interviews and competitive programming.
  • To strengthen your foundation for advanced Python concepts.

Exercise 1: Check Odd or Even Number

This classic problem helps beginners understand conditions using the modulus operator %.

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

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

Case: If you input 7, the output will be “Odd”. If you input 12, the output will be “Even”.

Exercise 2: Find the Largest of Two Numbers

Sometimes you need to compare two values and determine which one is greater.

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

if a > b:
    print("First number is larger")
else:
    print("Second number is larger or equal")

Case: If a = 20 and b = 15, the output will be “First number is larger”.

Exercise 3: Positive, Negative, or Zero

This exercise introduces multiple conditions using if-elif-else.

num = int(input("Enter a number: "))

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

Case: If you input -5, it prints “Negative number”.

Exercise 4: Check for Leap Year

Leap year calculation is a classic exercise using nested if-else logic.

year = int(input("Enter a year: "))

if (year % 4 == 0):
    if (year % 100 != 0) or (year % 400 == 0):
        print(year, "is a Leap Year")
    else:
        print(year, "is not a Leap Year")
else:
    print(year, "is not a Leap Year")

Case: Input 2020 → “Leap Year”, Input 1900 → “Not a Leap Year”.

Exercise 5: Grade Evaluation

A simple example to categorize students based on their scores.

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

if score >= 90:
    print("Grade: A")
elif score >= 75:
    print("Grade: B")
elif score >= 60:
    print("Grade: C")
else:
    print("Grade: D")

Case: Input 88 → “Grade: B”.

Exercise 6: Simple Login Validation

This example simulates a basic login system using if-else.

username = input("Enter username: ")
password = input("Enter password: ")

if username == "admin" and password == "1234":
    print("Login Successful")
else:
    print("Invalid Credentials")

Case: Input “admin” and “1234” → “Login Successful”.

Comparison Table of If-Else vs Switch-Case (Other Languages)

FeatureIf-Else (Python)Switch-Case (C/Java)
FlexibilityHandles complex conditionsBest for multiple equality checks
SyntaxSimple and readableRequires structured case blocks
Nested ConditionsSupportedLimited readability
Python AvailabilityYesNo built-in switch-case

Conclusion

Practicing Python if-else statement exercises is an excellent way to build your problem-solving skills and deepen your knowledge of programming logic. By mastering these exercises, you can handle real-world tasks like input validation, calculations, and decision-making in applications. Keep experimenting with variations of these exercises and combine them with loops and functions for more advanced learning.

For further reading, you can check Python’s official documentation on control flow.

rysasahrial

Leave a Reply

Your email address will not be published. Required fields are marked *