Tag: Python if condition examples

  • Python If Statement for Beginners – Learn with Fun Coding Exercises!

    🚀 Python is fun for all ages! Today, let’s learn about the if condition in Python with easy-to-understand examples and outputs!

    The if statement helps the computer make decisions just like we do in real life. Let’s explore with examples!

    1. Basic If Condition

    Example: Check if a number is positive.

    num = int(input("Enter a number: "))  
    
    if num > 0:  
        print("This is a positive number!")  
    

    Input:

    5
    

    Output:

    This is a positive number!
    

    🔹 Explanation: If the number is greater than 0, the message is printed. Otherwise, nothing happens.

    2. If-Else Condition

    Example: Check if a number is positive or negative.

    num = int(input("Enter a number: "))  
    
    if num > 0:  
        print("This is a positive number!")  
    else:  
        print("This is a negative number!")  
    

    Input:

     '-3
    

    Output:

    This is a negative number!
    

    🔹 Explanation: If the number is greater than 0, it prints “positive”; otherwise, it prints “negative”.

    3. If-Else Condition

    Example: Check if a number is positive, negative, or zero.

    num = int(input("Enter a number: "))  
    
    if num > 0:  
        print("This is a positive number!")  
    elif num == 0:  
        print("This is zero!")  
    else:  
        print("This is a negative number!")  
    

    Input:

     0
    

    Output:

    This is zero!
    

    🔹 Explanation: If the number is greater than 0, it prints “positive”. If it is 0, it prints “zero”. Otherwise, it prints “negative”.

    4. Nested If Condition

    Example: Check if a person is eligible to vote

    age = int(input("Enter your age: "))  
    
    if age >= 18:  
        if age >= 60:  
            print("You are a senior citizen and can vote!")  
        else:  
            print("You can vote!")  
    else:  
        print("You are too young to vote!")  
    
    

    Input:

    65

    Output:

    You are a senior citizen and can vote!
    

    🔹 Explanation: If the number is greater than 0, it prints “positive”. If it is 0, it prints “zero”. Otherwise, it prints “negative”.

    5. Even or Odd Number Check

    num = int(input("Enter a number: "))  
    
    if num % 2 == 0:  
        print("This is an even number!")  
    else:  
        print("This is an odd number!")  
    

    Input:

    7

    Output:

    This is an odd number!
    

    🔹 Explanation: If the number is divisible by 2, it’s even. Otherwise, it’s odd.

    6.Check If a Year is a Leap Year

    year = int(input("Enter a year: "))  
    
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):  
        print("This is a leap year!")  
    else:  
        print("This is not a leap year!")  
    

    Input:

    2024

    Output:

    This is a leap year!
    

    🔹 Explanation: A year is a leap year if it is divisible by 4 but not by 100 unless also divisible by 400.

    7. Check If a Person Can Enter a Theme Park Ride

    height = int(input("Enter your height in cm: "))  
    
    if height >= 120:  
        print("You can go on the ride!")  
    else:  
        print("Sorry, you are not tall enough for the ride. ")  
    

    Input:

    110

    Output:

    Sorry, you are not tall enough for the ride. 
    

    🔹 Explanation: If the height is 120 cm or more, the person can go on the ride. Otherwise, they cannot.

    8. Check If a Student Passes the Exam

    Example: Check if a person is eligible to vote

    score = int(input("Enter your score: "))  
    
    if score >= 50:  
        print("Congratulations! You passed the exam! ")  
    else:  
        print("Sorry, you failed. Try again next time! ")  
    

    Input:

    45

    Output:

    Sorry, you failed. Try again next time!
    

    🔹 Explanation: If the score is 50 or more, the student passes; otherwise, they fail.

    4. Check If a Number is Between Two Values

    num = int(input("Enter a number: "))  
    
    if 10 <= num <= 20:  
        print("Your number is between 10 and 20!")  
    else:  
        print("Your number is outside the range!")  
    

    Input:

    15

    Output:

    Your number is between 10 and 20!
    

    🔹 Explanation:The condition 10 <= num <= 20 checks if the number falls within this range.

    10. Simple Yes/No Decision Game

    choice = input("Do you like Python? (yes/no): ").lower()  
    
    if choice == "yes":  
        print("That's awesome! Python is great! ")  
    else:  
        print("No worries! Maybe you'll like it someday! ")  
    

    Input:

    yes

    Output:

    That's awesome! Python is great!
    

    🔹 Explanation: The program asks for input and checks if the answer is “yes” or “no”.

    Final Thoughts: Why If Condition is Important?

    ✅ The if condition allows us to make decisions in Python, just like in real life!
    ✅ You can use if, if-else, if-elif-else, and nested if conditions to create smart programs!
    ✅ These examples help beginners, students, and even adults learn Python easily!

    🚀 Want more fun Python lessons? Comment below and share this post with friends! 😊 Happy Coding! 🐍💻