Python Practice › If-Else Programs
Python If-Else Programs with Solutions
64 if-else programs that practise decisions in Python: comparisons, elif chains, nested conditions and combining tests with and / or. Each solution tests several inputs so you can see every branch run.
Before you start
Read the If…Else and Match Case lesson first if the topic is new to you.
All programs
- #1Check if a number is divisible by 3
Write a Python program to check whether a given number is divisible by 3.Easy - #2Numbers from 1 to 30 divisible by 3
Write a Python program to print all the numbers from 1 to 30 that are divisible by 3.Easy - #3Assign grades from marks
Write a Python program to assign a grade from total marks: below 40 Fail, 40-49 C, 50-59 B, 60-69 A, 70-79 A+, 80-89 A++, 90-100 Excellent, anything else Invalid.Easy - #4Check divisibility by 3 and 5
Write a Python program to check whether a number is divisible by both 3 and 5.Easy - #5Square a number divisible by 11
Write a Python program to print the square of a number if it is divisible by 11.Easy - #6Check a prime number
Write a Python program to check whether a given number is prime.Medium - #7Check odd or even
Write a Python program to check whether a number is odd or even.Easy - #8Check if a number is in the Fibonacci series
Write a Python program to check whether a given number is part of the Fibonacci series (1, 1, 2, 3, 5, 8, 13, …).Medium - #9Check username and password
Write a Python program to check login with a given username and password.Easy - #10Validate a user id in a list
Write a Python program to check whether a user id is present in a list of user ids.Easy - #11Square or cube depending on divisibility
Write a Python program that prints the square of a number if it is divisible by 2, or its cube if it is divisible by 3.Easy - #12Interview process with nested if-else
Write a Python program that describes a two-round interview: a candidate who passes round 1 can take round 2, and passing both means placed. Use nested if-else.Easy - #13Check if a number is in a list
Write a Python program to check whether a given number is present in a list of numbers.Easy - #14Largest of three numbers
Write a Python program to find the largest of three numbers.Easy - #15Check voting eligibility
Write a Python program to check whether a person is eligible to vote (age 18 or above).Easy - #16Check a palindrome number
Write a Python program to check whether a number is a palindrome.Easy - #17Check a palindrome string
Write a Python program to check whether a string is a palindrome, ignoring case.Easy - #18Pass or fail
Write a Python program that reads marks and prints Pass if they are greater than 35, otherwise Fail.Easy - #19Check if a number is positive
Write a Python program to check whether a number is positive and print True or False.Easy - #20Check if a number is negative
Write a Python program to check whether a number is negative and print True or False.Easy - #21Positive or negative, even or odd
Write a Python program to check whether a number is positive or negative and even or odd.Easy - #22Larger of two numbers
Write a Python program to print the larger of two numbers.Easy - #23Check an uppercase character
Write a Python program to check whether a character is uppercase.Easy - #24Check a lowercase character
Write a Python program to check whether a character is lowercase.Easy - #25Check if a value is an integer
Write a Python program to check whether a given value is an integer.Easy - #26Check if a value is a float
Write a Python program to check whether a given value is a float.Easy - #27Check if a value is a string
Write a Python program to check whether the given input is a string.Easy - #28Print 10 to 15 except 13
Write a Python program to print all the numbers from 10 to 15 except 13.Easy - #29Electricity bill with slabs
Write a Python program to calculate an electricity bill. Units 1-50 cost Rs 0.50, units 51-100 Rs 0.75, units 101-250 Rs 1.25 and units above 250 Rs 1.50 each. Add a 17% surcharge.Medium - #30Check a leap year
Write a Python program to check whether a year is a leap year.Easy - #31Fizz Buzz for multiples of 2 and 3
Write a Python program that prints “FizzBuzz” for multiples of both 2 and 3, “Fizz” for multiples of 2 and “Buzz” for multiples of 3; otherwise print the number.Easy - #32Check a vowel
Write a Python program to check whether a letter is a vowel.Easy - #33Check a consonant
Write a Python program to check whether a letter is a consonant.Easy - #34Days in a month
Write a Python program to convert a month name to its number of days.Easy - #35Equilateral triangle check
Write a Python program to check whether a triangle is equilateral (all three sides equal).Easy - #36Scalene triangle check
Write a Python program to check whether a triangle is scalene (all three sides different).Easy - #37Isosceles triangle check
Write a Python program to check whether a triangle is isosceles (at least two equal sides).Easy - #38Season from a month
Write a Python program that reads a month name and prints its season (Indian seasons: Winter Dec-Feb, Summer Mar-May, Monsoon Jun-Sep, Post-monsoon Oct-Nov).Easy - #39Round a float to 2 decimals
Write a Python program that checks whether a value is a float and, if so, rounds it to 2 decimal places.Easy - #40Check divisibility by 12
Write a Python program to check whether a number is divisible by 12.Easy - #41Check if a number is the square of 6
Write a Python program to check whether a number is the square of 6, and in general whether it is a perfect square.Easy - #42Check if a number is the cube of 3
Write a Python program to check whether a number is the cube of 3, and in general whether it is a perfect cube.Easy - #43Check if two numbers are equal
Write a Python program to check whether two numbers are equal.Easy - #44Check a complex number
Write a Python program to check whether a value is of complex type.Easy - #45Check a boolean value
Write a Python program to check whether a value is of boolean type.Easy - #46Check if a value is a list
Write a Python program to check whether a value is a list.Easy - #47Check if a value is a dictionary
Write a Python program to check whether a value is a dictionary.Easy - #48Roller coaster eligibility
Write a Python program to check whether a person can ride a roller coaster: eligible when age is greater than 12.Easy - #49Which group of ten a number belongs to
Write a Python program that splits 1-100 into 10 groups (1-10, 11-20, … 91-100) and finds which group a number belongs to, using nested if-else.Medium - #50Bonus eligibility
Write a Python program that gives a 10% bonus to employees who have served more than 4 years.Easy - #51Is the rectangle a square?
Write a Python program that takes the length and breadth of a rectangle and checks whether it is a square.Easy - #52Discount on a bill
A shop gives 20% off bills above 2000 and 10% off bills above 1000. Write a Python program to calculate the discount.Easy - #53Absolute value
Write a Python program to print the absolute value of a number.Easy - #54Exam attendance eligibility
Write a Python program that allows a student into the exam only if attendance is at least 75%.Easy - #55Is the last digit divisible by 4?
Write a Python program to check whether the last digit of a number is divisible by 4.Easy - #56Print 1 for Hello and 0 for Bye
Write a Python program that prints 1 if the user enters Hello and 0 if the user enters Bye.Easy - #57Road tax by car price
Write a Python program to display road tax: price below 5,00,000 pays 15,000; below 10,00,000 pays 50,000; below 15,00,000 pays 80,000.Easy - #58Day name from a number
Write a Python program that reads a number from 1 to 7 and prints the day (1 = Sunday, 2 = Monday, … 7 = Saturday).Easy - #59Monuments of a city
Write a Python program that accepts a city name (Pune or Mumbai) and displays its monuments.Easy - #60Senior citizen check
Write a Python program to check whether a person is a senior citizen (age greater than 60).Easy - #61Smallest of three numbers
Write a Python program to find the smallest of three numbers.Easy - #62Is the water boiling?
Write a Python program that reads a temperature in Fahrenheit and says whether water is boiling (212 °F or more).Easy - #63Calculator with an operator
Write a Python program that accepts two numbers and an operator (+, -, *, /) and prints the result.Medium - #64Allot a stream from marks
Write a Python program that allots a stream: marks above 85 Science, above 70 Commerce, 35 to 70 Arts, below 35 Fail.Easy