Conditional statements help a program make decisions based on different conditions. In this Python conditional statements practice Questions with Solutions set, you’ll learn how to use if, if...else, if...elif...else, and nested if statements through beginner-friendly Python programs.
1. Python Program to Check Whether a Number is Positive or Negative
Problem Statement
Write a Python program to check whether a given number is positive or negative.
Python Solution
# Python Program to Check Whether a Number is Positive or Negative
# Taking input from the user
number = float(input("Enter a number: "))
# Checking the number
if number > 0:
print("Positive Number")
elif number < 0:
print("Negative Number")
else:
print("The number is Zero")
Sample Output
Enter a number: 25
Positive Number
Explanation
The program checks whether the entered number is greater than or equal to zero.
Concepts Covered
- if…else statement
2. Python Program to Check Whether a Number is Even or Odd
Problem Statement
Write a Python program to check whether a number is even or odd.
Python Solution
number = int(input("Enter a number: "))
if number % 2 == 0:
print("Even Number")
else:
print("Odd Number")
Sample Output
Enter a number: 18
Even Number
Explanation
If the remainder after dividing by 2 is zero, the number is even.
Concepts Covered
- if…else
- Modulus operator
3. Python Program to Check Voting Eligibility
Problem Statement
Write a Python program to check whether a person is eligible to vote. The minimum voting age is 18 years.
Python Solution
age = int(input("Enter your age: "))
if age >= 18:
print("Eligible for Voting")
else:
print("Not Eligible for Voting")
Sample Output
Enter your age: 20
Eligible for Voting
Explanation
The program compares the user’s age with the minimum voting age.
Concepts Covered
- Comparison operator
- if…else
4. Python Program to Find the Largest of Two Numbers
Problem Statement
Write a Python program to find the larger of two numbers entered by the user.
Python Solution
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
if num1 > num2:
print("Largest Number:", num1)
else:
print("Largest Number:", num2)
Sample Output
Enter first number: 35
Enter second number: 18
Largest Number: 35
Explanation
The program compares two numbers and displays the greater value.
Concepts Covered
- Comparison operators
- if…else
5. Python Program to Find the Largest of Three Numbers
Problem Statement
Write a Python program to find the largest among three numbers using if...elif...else.
Python Solution
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
if num1 >= num2 and num1 >= num3:
print("Largest Number:", num1)
elif num2 >= num1 and num2 >= num3:
print("Largest Number:", num2)
else:
print("Largest Number:", num3)
Sample Output
Enter first number: 45
Enter second number: 18
Enter third number: 30
Largest Number: 45
Explanation
The program compares all three numbers and prints the largest one.
Concepts Covered
- if…elif…else
- Logical operators
6. Python Program to Check Whether a Year is a Leap Year
Problem Statement
Write a Python program to check whether a given year is a leap year.
Python Solution
year = int(input("Enter a year: "))
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print("Leap Year")
else:
print("Not a Leap Year")
Sample Output
Enter a year: 2024
Leap Year
Explanation
A leap year is divisible by 400 or divisible by 4 but not by 100.
Concepts Covered
- Nested conditions
- Logical operators
7. Python Program to Calculate Grade Based on Marks
Problem Statement
Write a Python program to display the grade based on the following criteria:
- 90 and above → A
- 75–89 → B
- 60–74 → C
- 40–59 → D
- Below 40 → Fail
Python Solution
marks = int(input("Enter marks: "))
if marks >= 90:
print("Grade A")
elif marks >= 75:
print("Grade B")
elif marks >= 60:
print("Grade C")
elif marks >= 40:
print("Grade D")
else:
print("Fail")
Sample Output
Enter marks: 82
Grade B
Explanation
if...elif...else helps check multiple conditions in sequence.
Concepts Covered
- Multiple conditions
- if…elif…else
8. Python Program to Check Whether a Character is a Vowel or Consonant
Problem Statement
Write a Python program to check whether a character entered by the user is a vowel or a consonant.
Python Solution
character = input("Enter a character: ").lower()
if character in "aeiou":
print("Vowel")
else:
print("Consonant")
Sample Output
Enter a character: a
Vowel
Explanation
The in operator checks whether the character exists in the string "aeiou".
Concepts Covered
- if…else
- Membership operator
9. Python Program to Check Username and Password
Problem Statement
Write a Python program to check whether the entered username and password are correct.
- Username:
admin - Password:
12345
Python Solution
username = input("Enter username: ")
password = input("Enter password: ")
if username == "admin" and password == "12345":
print("Login Successful")
else:
print("Invalid Username or Password")
Sample Output
Enter username: admin
Enter password: 12345
Login Successful
Explanation
The program checks two conditions using the and operator.
Concepts Covered
- Logical operators
- Multiple conditions
10. Python Program to Check the Largest Among Four Numbers
Problem Statement
Write a Python program to find the largest among four numbers entered by the user.
Python Solution
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
num3 = int(input("Enter third number: "))
num4 = int(input("Enter fourth number: "))
largest = num1
if num2 > largest:
largest = num2
if num3 > largest:
largest = num3
if num4 > largest:
largest = num4
print("Largest Number:", largest)
Sample Output
Enter first number: 12
Enter second number: 35
Enter third number: 28
Enter fourth number: 42
Largest Number: 42
Explanation
The program updates the value of largest whenever it finds a bigger number.
Concepts Covered
- Multiple
ifstatements - Comparison operators
11. Python Program to Calculate Grade Based on Marks
Problem Statement
Write a Python program to accept a student’s marks and display the corresponding grade using conditional statements.
Grading Criteria:
- 90 and above → Grade A
- 80–89 → Grade B
- 70–79 → Grade C
- 60–69 → Grade D
- Below 60 → Grade F
Python Solution
marks = float(input("Enter your marks: "))
if marks >= 90:
print("Grade: A")
elif marks >= 80:
print("Grade: B")
elif marks >= 70:
print("Grade: C")
elif marks >= 60:
print("Grade: D")
else:
print("Grade: F")
Sample Output
Enter your marks: 85
Grade: B
Explanation
The program checks the marks against multiple conditions using the if-elif-else ladder and displays the appropriate grade.
Concepts Covered
- if Statement
- elif Statement
- else Statement
- Comparison Operators
12. Python Program to Check Whether a Year is a Leap Year
Problem Statement
Write a Python program to check whether a given year is a leap year.
Python Solution
year = int(input("Enter a year: "))
if (year % 400 == 0) or (year % 4 == 0 and year % 100 != 0):
print("Leap Year")
else:
print("Not a Leap Year")
Sample Output
Enter a year: 2024
Leap Year
Explanation
A leap year is divisible by 400 or divisible by 4 but not divisible by 100.
Concepts Covered
- Nested Conditions
- Logical Operators
- Modulus Operator
13. Python Program to Check Eligibility for a Loan
Problem Statement
Write a Python program to determine whether a person is eligible for a loan based on age and monthly income.
Conditions:
- Age must be at least 21 years.
- Monthly income must be ₹30,000 or more.
Python Solution
age = int(input("Enter your age: "))
income = float(input("Enter your monthly income: "))
if age >= 21 and income >= 30000:
print("Eligible for Loan")
else:
print("Not Eligible for Loan")
Sample Output
Enter your age: 28
Enter your monthly income: 45000
Eligible for Loan
Explanation
The program uses the logical and operator to ensure both eligibility conditions are satisfied.
Concepts Covered
- Logical Operators
- Conditional Statements
- Real-world Applications
14. Python Program to Find the Second Largest Number
Problem Statement
Write a Python program to find the second largest number among three numbers.
Python Solution
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
c = int(input("Enter third number: "))
numbers = [a, b, c]
numbers.sort()
print("Second Largest Number:", numbers[1])
Sample Output
Enter first number: 15
Enter second number: 40
Enter third number: 28
Second Largest Number: 28
Explanation
The program stores the numbers in a list, sorts them, and displays the second largest value.
Concepts Covered
- Conditional Logic
- Lists
- Sorting
15. Python Program to Calculate Income Tax
Problem Statement
Write a Python program to calculate income tax using the following rules:
- Income up to ₹2,50,000 → No Tax
- ₹2,50,001 to ₹5,00,000 → 5%
- ₹5,00,001 to ₹10,00,000 → 20%
- Above ₹10,00,000 → 30%
Python Solution
income = float(input("Enter your annual income: "))
if income <= 250000:
tax = 0
elif income <= 500000:
tax = income * 0.05
elif income <= 1000000:
tax = income * 0.20
else:
tax = income * 0.30
print("Income Tax: ₹", tax)
Sample Output
Enter your annual income: 650000
Income Tax: ₹ 130000.0
Explanation
The program calculates tax based on the income slab using the if-elif-else ladder.
Concepts Covered
- if-elif-else
- Real-world Programming
- Conditional Statements
Frequently Asked Questions (FAQs)
1. What are conditional statements in Python?
Conditional statements allow a program to make decisions based on specific conditions. They execute different blocks of code depending on whether a condition is True or False.
2. What are the different types of conditional statements in Python?
Python provides the following conditional statements:
ifif...elseif...elif...else- Nested
if
These help handle simple and complex decision-making.
3. What is the difference between if, elif, and else?
ifchecks the first condition.elifchecks additional conditions if the previous ones are false.elseexecutes when none of the conditions are true.
4. Can we use multiple conditions in an if statement?
Yes. You can combine multiple conditions using logical operators such as and, or, and not.
Example:
age = 22
citizen = True
if age >= 18 and citizen:
print("Eligible to Vote")
5. What is a nested if statement?
A nested if statement is an if statement placed inside another if statement. It is useful when one condition depends on another.
6. Why are conditional statements important in Python?
Conditional statements enable programs to make decisions, validate user input, control program flow, and solve real-world problems such as grading systems, login authentication, banking applications, and business logic.
7. Where are conditional statements used in real-world applications?
Conditional statements are widely used in:
- Login and Authentication Systems
- Banking and Finance Applications
- E-commerce Websites
- Student Result Management Systems
- Hospital Management Systems
- Machine Learning Models
- Data Validation and Automation Scripts
- Game Development
Chapter Summary
After completing this chapter, you have learned:
ifstatementif...elsestatementif...elif...elsestatement- Nested conditions
- Comparison operators
- Logical operators
- Decision-making in Python
- Solving real-world problems using conditional statements
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
