Practice CornerIf Statements

If Statements

Conditional statements allow programs to make decisions based on specific conditions. The if statement in Python is one of the most basic and essential tools for implementing logic in your code. Here are some beginner-friendly exercises to practice conditional statements.

Practice Problems

Problems Index

  1. Input a number and check if it is positive, negative, or zero.
  2. Input your age and check if you are eligible to vote (age 18 or older).
  3. Input a number and check if it is even or odd.
  4. Input three numbers and find the largest among them.
  5. Input a year and check if it is a leap year.
  6. Input your marks and check if you passed or failed (passing marks = 40).
  7. Input two numbers and check if the first is divisible by the second.
  8. Input the temperature in Celsius and check if it’s cold (below 15°C), warm (15°C-25°C), or hot (above 25°C).
  9. Input a character and check if it is a vowel or a consonant.
  10. Input a number and check if it is a multiple of 5.

Try It Yourself

Now that you’ve reviewed the problems, practice them below using the code runner!

Pyground

Play with Python!

Output:

Solutions

Solutions

  1. Input a number and check if it is positive, negative, or zero.

    Show Code
    number = int(input("Enter a number: "))
    if number > 0:
        print("The number is positive.")
    elif number < 0:
        print("The number is negative.")
    else:
        print("The number is zero.")
  2. Input your age and check if you are eligible to vote (age 18 or older).

    Show Code
    age = int(input("Enter your age: "))
    if age >= 18:
        print("You are eligible to vote.")
    else:
        print("You are not eligible to vote.")
  3. Input a number and check if it is even or odd.

    Show Code
    number = int(input("Enter a number: "))
    if number % 2 == 0:
        print("The number is even.")
    else:
        print("The number is odd.")
  4. Input three numbers and find the largest among them.

    Show Code
    num1 = int(input("Enter the first number: "))
    num2 = int(input("Enter the second number: "))
    num3 = int(input("Enter the third number: "))
    if num1 >= num2 and num1 >= num3:
        print("The largest number is:", num1)
    elif num2 >= num1 and num2 >= num3:
        print("The largest number is:", num2)
    else:
        print("The largest number is:", num3)
  5. Input a year and check if it is a leap year.

    Show Code
    year = int(input("Enter a year: "))
    if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
        print("It is a leap year.")
    else:
        print("It is not a leap year.")
  6. Input your marks and check if you passed or failed (passing marks = 40).

    Show Code
    marks = int(input("Enter your marks: "))
    if marks >= 40:
        print("You passed the exam.")
    else:
        print("You failed the exam.")
  7. Input two numbers and check if the first is divisible by the second.

    Show Code
    num1 = int(input("Enter the first number: "))
    num2 = int(input("Enter the second number: "))
    if num2 != 0 and num1 % num2 == 0:
        print("The first number is divisible by the second.")
    else:
        print("The first number is not divisible by the second.")
  8. Input the temperature in Celsius and check if it’s cold (below 15°C), warm (15°C-25°C), or hot (above 25°C).

    Show Code
    temp = float(input("Enter the temperature in Celsius: "))
    if temp < 15:
        print("It’s cold.")
    elif temp <= 25:
        print("It’s warm.")
    else:
        print("It’s hot.")
  9. Input a character and check if it is a vowel or a consonant.

    Show Code
    char = input("Enter a character: ").lower()
    if char in 'aeiou':
        print("The character is a vowel.")
    else:
        print("The character is a consonant.")
  10. Input a number and check if it is a multiple of 5.

    Show Code
    number = int(input("Enter a number: "))
    if number % 5 == 0:
        print("The number is a multiple of 5.")
    else:
        print("The number is not a multiple of 5.")