Python If Else Statement Important Practice Questions
Python If Else Statement Important Practice Questions
Q1. Name the keyword which helps in writing code involves condition.
Ans. if
Q3. Is there any limit of statement that can appear under an if block.
Ans. No
Q4. Write a program to check whether a person is eligible for voting or not. (accept age from
user)
Ans.
age=int(input("Enter your age"))
if age >=18:
print("Eligible for voting")
else:
print("not eligible for voting")
Q5. Write a program to check whether a number entered by user is even or odd.
Ans.
num=int(input("Enter your age"))
if num%2==0:
print("Number is Even")
else:
print("Number is Odd")
Q7. Write a program to display "Hello" if a number entered by user is a multiple of five,
otherwise print "Bye".
Ans.
num=int(input("Enter your age"))
if num%5==0:
print("Hello")
else:
print("Bye")
Q8. Write a program to calculate the electricity bill (accept number of unit from user) according
to the following criteria :
Unit Price
First 100 units no charge
Next 100 units Rs 5 per unit
After 200 units Rs 10 per unit
(For example if input unit is 350 than total bill amount is Rs2000)
Ans.
amt=0
nu=int(input("Enter number of electric unit"))
if nu<=100:
amt=0
if nu>100 and nu<=200:
amt=(nu-100)*5
if nu>200:
amt=500+(nu-200)*10
print("Amount to pay :",amt)
Q10. Write a program to check whether the last digit of a number( entered by user ) is
divisible by 3 or not.
Ans.
num=int(input("Enter any number"))
ld=num%10
if ld%3==0:
print("Last digit of number is divisible by 3 ")
else:
print("Last digit of number is not divisible by 3 ")
Python if else Statement Practice Test 2
Q1. Write a program to accept percentage from the user and display the grade according to the
following criteria:
Marks Grade
> 90 A
> 80 and <= 90 B
>= 60 and <= 80 C
below 60 D
Ans.
per = int(input("Enter marks"))
if per > 90:
print("Grade is A")
if per > 80 and per <=90:
print("Grade is B")
if per >=60 and per <= 80:
print("Grade is C")
if per < 60:
print("Grade is D")
Q2. Write a program to accept the cost price of a bike and display the road tax to be paid
according to the following criteria :
Q5. Write a program to accept a number from 1 to 12 and display name of the month and days in
that month like 1 for January and number of days 31 and so on
Ans.
num=int(input("Enter any number between 1 to 7 : "))
if num==1:
print("January")
elif num==2:
print("February")
elif num==3:
print("March")
elif num==4:
print("April")
elif num==5:
print("May")
elif num==6:
print("June")
elif num==7:
print("July")
elif num==8:
print("August")
elif num==9:
print("September")
elif num==10:
print("October")
elif num==11:
print("November")
elif num==12:
print("December")
else:
print("Please enter number between 1 to 12")
Q6. What do you mean by statement?
Ans. Executable lines in a program is called instruction
Q9. Accept any city from the user and display monument of that city.
City Monument
Delhi Red Fort
Agra Taj Mahal
Jaipur Jal Mahal
Ans.
city = input("Enter name of the city")
if city.lower()=="delhi":
print("Monument name is : Red Fort")
elif city.lower()=="agra":
print("Monument name is : Taj Mahal")
elif city.lower()=="jaipur":
print("Monument name is : Jal Mahal")
else:
print("Enter correct name of city")
Q2. Write a program to check whether a person is eligible for voting or not.(voting age >=18)
Ans.
age=int(input("Enter your age"))
if age >=18:
print("Eligible for Voting")
else
print("Not eligible for voting")
Q4. Write a program to find the lowest number out of two numbers excepted from user.
Ans.
num1 = int(input("Enter first number"))
num2 = int(input("Enter second number"))
if num1 > num2:
print("smaller number is :", num2)
else:
print("smaller number is :", num1)
Q5. Write a program to find the largest number out of two numbers excepted from user.
Ans.
num1 = int(input("Enter first number"))
num2 = int(input("Enter second number"))
if num1 > num2:
print("greater number is :", num1)
else:
print("greater number is :", num2)
Q6. Write a program to check whether a number (accepted from user) is positive or negative.
Ans.
num1 = int(input("Enter first number"))
if num1 > 0 :
print("Number is positive")
else:
print("Number is negative")
Q7. Write a program to check whether a number is even or odd.
Ans.
num1 = int(input("Enter first number"))
if num1%2==0:
print("Number is even")
else:
print("Number is odd")
Q8. Write a program to display the spell of a digit accepted from user (like user input 0 and
display ZERO and so on)
Q8. Write a program to whether a number (accepted from user) is divisible by 2 and 3 both.
Ans.
num1 = int(input("Enter first number"))
if num1%2==0 and num1%3==0:
print("Number is divisible by 2 and 3 both")
else:
print("Number is not divisible by both")
Q9. Write a program to find the largest number out of three numbers excepted from user.
Ans.
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("Greatest number is ", num1)
if num2 > num1 and num2 > num3:
print("Greatest number is ", num2)
if num3 > num2 and num3 > num1:
print("Greatest number is ", num3)
Python if else Statement Practice Test 4
Q1. Accept the temperature in degree Celsius of water and check whether it is boiling or not
(boiling point of water in 100 oC.
Ans.
Ans. Pass
Q4. In python, a block is a group of _______statement having same indentation
level.(consecutive/alternate)
Ans. Consecutive
Q5. Out of ―elif‖ and ―else if‖, which is the correct statement in python?
Ans. elif
Q6. Accept the age of 4 people and display the youngest one?
Ans.
Ans. If none of the condition is true in elif ladder then the else part will execute
Q8. Accept the age of 4 people and display the oldest one.
Ans.
Ans.
k=0
num1 = int(input("Enter any number"))
if num1 == 0 or num1 == 1:
k=1
for i in range(2,num1):
if num1%i == 0:
k=1
if k==1:
print("number is not prime")
else:
print("number is prime")
Q10. Write a program to check a character is vowel or not.
Ans.
Q1. Accept the following from the user and calculate the percentage of class attended:
Ans.
Below 25 —- D
25 to 45 —- C
45 to 50 —- B
50 to 60 –– B+
60 to 80 — A
Above 80 –- A+
Ans.
Ask user for their salary and years of service and print the net bonus amount.
Ans.
Q4. Accept the marked price from the user and calculate the Net amount as(Marked Price –
Discount) to pay according to following criteria:
Marked Discount
Price
>10000 20%
>7000 15%
and
<=10000
<=7000 10%
Python if else
Ans.
na=0
d=0
mp=int(input("Enter marked price"))
if mp > 10000:
d = 20/100*mp
if mp > 7000 and mp <= 10000:
d = 15/100*mp
if mp <= 7000:
d = 10/100*mp
na = mp-d
print("Net amount to pay ", na)
Q5. Write a program to accept percentage and display the Category according to the following
criteria :
Percentage Category
< 40 Failed
>=40 & Fair
<55
>=55 & Good
<65
>=65 Excellent
Python if else
Ans.
Q6. Accept three sides of a triangle and check whether it is an equilateral, isosceles or scalene
triangle.
Note :
Ans.
Q7. Write a program to accept two numbers and mathematical operators and perform
operation accordingly.
Like:
Enter operator : +
Your Answer is : 16
Ans
num1=int(input("Enter first number"))
if op=='+':
if op=='-':
if op=='*':
if op=='/':
if op=='%':
if op=='**':
if op=='//':
Q1. Accept three numbers from the user and display the second largest number.
Ans.
if (num1 > num2 and num1 < num3) or (num1 < num2 and num1 > num3):
if (num2 > num1 and num2 < num3) or (num2 < num1 and num2 > num3):
if (num3 > num2 and num3 < num1) or (num3 < num2 and num3 > num1):
(triangle is possible only when sum of any two sides is greater than 3rd side)
Ans.
print("Triangle is possible")
else:
Python if else
What will the above code print if the variables i, j, and k have the following values?
(a) i = 3, j = 5, k = 7
(c) i = 8, j = 15, k = 12
(e) i = 3, j = 5, k = 17
Ans.
(a) 5 5 7
(b) 9 -5 9
(c) 8 12 12
(d) 13 13 13
(e) 5 5 17
(f) 17 15 17
Q4. Accept the electric units from user and calculate the bill according to the following rates.
Ans.
if ut <=100:
amt = 0
amt = (ut-100) *2
else:
11 to 15 days : Rs 4/day
Ans.
amt = nd * 2
amt = nd * 3
amt = nd * 4
else:
amt = nd * 5
First 10 Km Rs11/km
Ans.
if kmc <=10 :
amt = kmc * 11
Q7. Accept the marks of English, Math and Science, Social Studies Subject and display the
stream allotted according to following
a=True
b=True
c=True
d=True
1. print(c)
2. print(d)
3. print(not a)
4. print(not b )
5. print(not c )
6. print(not d)
7. print(a and b )
8. print(a or b )
9. print(a and c)
10. print(a or c )
11. print(a and d )
12. print(a or d)
13. print(b and c )
14. print(b or c )
15. print(a and b or c)
16. print(a or b and c )
17. print(a and b and c)
18. print(a or b or c )
19. print(not a and b and c)
20. print(not a or b or c )
21. print(not (a and b and c))
22. print(not (a or b or c) )
23. print(not a and not b and not c)
24. print(not a or not b or not c )
25. print(not (not a or not b or not c))
Ans.
1. True
2. True
3. False
4. False
5. False
6. False
7. True
8. True
9. True
10. True
11. True
12. True
13. True
14. True
15. True
16. True
17. True
18. True
19. False
20. True
21. False
22. False
23. False
24. False
25. True