free geoip
34

Python If Statements Practice with Examples

Mastering Python if statements is one of the most important steps for every beginner learning Python programming. Conditional statements help…

Mastering Python if statements is one of the most important steps for every beginner learning Python programming. Conditional statements help us control the flow of our programs by making decisions based on specific conditions. This article will provide a complete guide with examples, exercises, and practical cases to strengthen your understanding.

Python If Statements Practice with Examples

What Are If Statements in Python?

In Python, if statements are used to check whether a condition is true or false. If the condition evaluates to True, the block of code inside the statement will be executed. Otherwise, it will be skipped. This concept is essential for writing dynamic programs that respond to different inputs or scenarios.

Basic Syntax of Python If Statement

if condition:
    # code block executed if condition is True

Example:

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

Output:

You are eligible to vote.

If-Else Statement

Sometimes, you want to run one block of code if the condition is true and another if it is false. That’s where the if-else statement comes in.

age = 16
if age >= 18:
    print("You can vote.")
else:
    print("You are not eligible to vote yet.")

Output:

You are not eligible to vote yet.

If-Elif-Else Statement

When dealing with multiple conditions, you can use the elif keyword (short for “else if”).

score = 75

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

Output:

Grade: B

Nested If Statements

You can also put an if statement inside another if statement. This is called a nested if statement.

age = 20
citizen = True

if age >= 18:
    if citizen:
        print("You are eligible to vote in the election.")
    else:
        print("You must be a citizen to vote.")
else:
    print("You are too young to vote.")

Logical Operators in If Statements

You can combine multiple conditions using logical operators like and, or, and not.

age = 25
has_id = True

if age >= 18 and has_id:
    print("Access granted.")
else:
    print("Access denied.")

Output:

Access granted.

Practical Examples of If Statements

Let’s practice real-world problems to understand how Python if statements work in daily coding.

1. Even or Odd Number

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

2. Check Positive, Negative, or Zero

num = -5
if num > 0:
    print("Positive number")
elif num < 0:
    print("Negative number")
else:
    print("Zero")

3. Simple Login System

username = "admin"
password = "1234"

if username == "admin" and password == "1234":
    print("Login successful!")
else:
    print("Invalid credentials")

4. Check Leap Year

year = 2024

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print("Leap year")
else:
    print("Not a leap year")

5. Grading System with Nested If

score = 95
if score >= 90:
    if score >= 95:
        print("Excellent! Grade A+")
    else:
        print("Grade A")
elif score >= 80:
    print("Grade B")
else:
    print("Grade C or below")

Python If Statement Practice Exercises

Now that you understand the basics, try solving these exercises:

  1. Write a program that checks if a given number is divisible by 3 and 5.
  2. Create a program that asks for the user’s age and determines whether they are a child, teenager, adult, or senior.
  3. Build a script that simulates a simple ATM withdrawal with balance checking.
  4. Write a program that checks if a word is a palindrome.
  5. Make a BMI calculator and classify the result into underweight, normal, overweight, or obese.

Conclusion

Practicing Python if statements is a crucial part of becoming a better programmer. With these examples and exercises, you now have the tools to build more interactive and responsive Python programs. Try the practice exercises and create your own mini-projects to improve your skills further.

For further reading on Python control flow, check out the official Python documentation:
Python Control Flow Documentation.

rysasahrial

Leave a Reply

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