Python If Else Condition: The Ultimate Guide

If Else Condition: The statement if tests a particular condition. Whenever that condition evaluates to true, an action or set of actions is executed, and when the condition becomes false, the else block is executed.
The if-elif-else statement is used when we have more than one condition to check at the same time.

1. Write a program to calculate the grade from the students’ percentage:
If the percentage is more than or equal to 85, then it’s an A Grade, if the percentage is more than or equal to 75, then it’s a B Grade, if the percentage is more than or equal to 65, then it’s a C Grade, if the percentage is more than or equal to 45, then it’s a D Grade. Otherwise, fail.
CODE
percent=int(input("Enter Percentage (without%): "))
if percent >=85:
    print(percent,"%","=","A Grade")
elif percent >=75:
    print(percent,"%","=","B Grade")
elif percent >=65:
    print(percent,"%","=","C Grade")
elif percent >=45:
    print(percent,"%","=","D Grade")
else:
    print(percent,"%","=","Fail")
OUTPUT
If Else Condition
2. Write a program to check for leap years.
CODE
year=int(input("Enter Year: "))
if year % 4==0:
    print(year,"is a leap year.")
else:
    print(year,"is not a leap year.")
OUTPUT
If Else Condition
3. Write a program by using an If Else Condition to check the eligibility of a driving license.
CODE
age=int(input("Enter Age: "))
if age >=18:
    print(age,"=","You are eligible for Driving License.")
else:
    print(age,"=","You are not eligible for Driving License.")
OUTPUT
If Else Condition
4. Write a program to check if a number is positive, negative, or zero.
CODE
num=int(input("Enter Number: "))
if num >0:
    print(num,"=","Positive Number")
elif num <0:
    print(num,"=","Negative Number")
else:
    print(num,"=","The number is zero")
OUTPUT
If Else Condition

HTML Important Practical Questions

5. Write a program to check if a number is divisible by both 3 and 5.
CODE
num=int(input("Enter Number:"))
if num %3==0 and num %5==0:
    print(num,"=","Divisible by both 3 and 5.")
else:
    print(num,"=","Not Divisible by both 3 and 5.")
OUTPUT
If Else Condition
6. Write a program to find the largest number among two user inputs by using an If Else Condition.
CODE
a=int(input("Enter 1st Number: "))
b=int(input("Enter 2nd Number:"))
if a>b:
    print(a,"is larger than",b)
else:
    print(b,"is larger than",a)
OUTPUT
If Else Condition
7. Write a program to check whether a number is even or odd.
CODE
num=int(input("Enter Number: "))
if num %2==0:
    print(num,"=","Even Number")
else:
    print(num,"=","Odd Number")
OUTPUT
If Else Condition
8. Write a program to check if a person is eligible to vote.
CODE
age=int(input("Enter Age: "))
if age >=18:
    print(age,"=","You are eligible for vote.")
else:
    print(age,"=","You are not eligible for vote.")
OUTPUT
If Else Condition
9. Write a program to calculate and classify BMI.
CODE
w=float(input("Enter Weight in kg: "))
h=float(input("Enter Height in m: "))
bmi=w/(h**2)
if bmi <18.5:
    print(bmi,"=","You are Underweight.")
elif bmi <24.9:
    print(bmi,"=","You have Normal Weight.")
elif bmi <29.9:
    print(bmi,"=","You are overweight.")
else:
    print(bmi,"=","You are obese.")
OUTPUT
If Else Condition
10. Write a program to calculate wages as follows:
Get four inputs from the employee (name, working hours/day, total working hours/day, and Bonus type (yes/no)).
Show the total wages as per 150/hours, and if the bonus is yes, then provide 200 rupees.
CODE
name=input("Enter Employee Name: ")
working=int(input("Enter Working Hours: "))
total=int(input("Enter Total Working Hours: "))
bonus=input("Enter Bonus Type e.g. Yes, No:")
result=150*working
b=0
if bonus=="Yes":
    b=200
final=result+b
print("Employee Name","=",name)
print("Working Hours","=",working)
print("Total Working Hours","=",total)
print("Bonus","=",bonus)
print("Wages","=",result)
print("Bonus","=",b)
print("Total Wages","=",final)
OUTPUT
If Else Condition
Share your love
Sleepy Expert
Sleepy Expert
Articles: 26

Leave a Reply

Your email address will not be published. Required fields are marked *