Skip to main content

Command Palette

Search for a command to run...

Beginner Exercises for If-Else Statements in Python

Published
2 min read
Beginner Exercises for If-Else Statements in Python
A
I co-founded🫆digizen (fighting internet chaos 🧠), building an AI app to tackle misinformation, and running ideaGeek to turn “I have an idea” into real startups. I also share tech + travel on YouTube (TechNomad & Rz Omar). I touch grass too 🌱… but mostly to debug life 💻

1. Even or Odd

Ask the user for a number. Print if it's even or odd.

2. Greater Number

Ask the user to enter two numbers. Print the greater one.

3. Pass or Fail

Ask the user for a score.
If score >= 50, print “Pass”. Otherwise, print “Fail”.

4. Temperature Check

Ask the user for the temperature.
Print:

  • “Hot” if >30

  • “Warm” if between 20–30

  • “Cold” if <20

5. Leap Year Checker

Ask the user for a year.
Check if it’s a leap year:

  • Divisible by 4, not divisible by 100 unless divisible by 400.

ANSWERS


1. Even or Odd

num = int(input("Enter a number: "))
if num % 2 == 0:
    print("Even")
else:
    print("Odd")

2. Greater Number

a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
if a > b:
    print("Greater number is", a)
else:
    print("Greater number is", b)

3. Pass or Fail

score = int(input("Enter your score: "))
if score >= 50:
    print("Pass")
else:
    print("Fail")

4. Temperature Check

temp = int(input("Enter temperature: "))
if temp > 30:
    print("Hot")
elif temp >= 20:
    print("Warm")
else:
    print("Cold")

5. Leap Year Checker

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

Python

Part 2 of 10

I break down Python basics with clear explanations and practical exercises. Whether you're a beginner or looking to refresh your skills, follow along to strengthen your Python fundamentals through hands-on practice. Let's code and grow together!

Up next

Beginner Exercises for "for Loop" Statements in Python

1. Print 1 to 10 Use a for loop to print numbers 1 to 10. 2. Sum of 1 to 100 Use a for loop to calculate the sum of numbers from 1 to 100. 3. Print Multiples of 3 (1–30) Print all multiples of 3 between 1 and 30. 4. Print Each Character Ask the user ...

More from this blog

T

TechNomad

46 posts

TechNomad is mostly coding exercises, not long boring explanations 😌. I share what I teach and learn through hands-on problems—mainly for my students, but anyone can jump in. If you learn by doing (and breaking things), you’ll fit right in. Less theory, more “try this and see what happens.”