if-else practice questions in Python

Not all the questions require if-else. Judge yourself and then apply accordingly.

WAP stands for Write A Program

  1. WAP to display the last digit of a number. (Don’t use indexing for this) 
  2. WAP to check if a single character is a vowel or not.
  3. WAP to check if a number is even or odd where the number is taken as input.
  4. WAP to check if the 3rd last character of a string is a vowel or not.
  5. Check if the first and last number of a list is same or not. Take a pre-defined list in the code itself.
  6. Calculate income tax for the input income by adhering to the Indian rules.
  7. Write a Python program to check whether a string starts with vowel or not.
  8. WAP to calculate percentage of a student through 5 subjects. Take marks as input from the user.
    Using percentage print which grade the student has scored.
  9. WAP to convert a celsius value to fahrenheit. Take celsius value as input.
  10. WAP to calculate the surface area of a triangle. Take the length of sides as input.
  11. WAP to print the day based on the number input.
    For example: if input = 1, output = Monday
  12. WAP to check whether a person is eligible to vote or not based on their age.
  13. WAP to calculate the count of elements in a string.
  14. WAP to check using the sides of a triangle to tell if it is equilateral triangle or not.
  15. WAP to check if the 2nd last digit of numerical input from the user is divisible by 3 or not.
  16. Write a Python program that takes two numbers as input from the user and prints the larger of the two numbers.

Solutions

				
					# WAP to display the last digit of a number. (Don’t use indexing for this)
num = int(input("Enter a number: "))
if num < 10:
    print(num)
else:
    last = num % 10
    print(last)
				
			
				
					# WAP to check if a single character is a vowel or not. Take the character as input from the user.
ch = input('Enter a single character: ')
if ch.lower() in "aeiou":
    print("It is Vowel")
else:
    print("It is not a vowel")
				
			
				
					# WAP to check if a number is even or odd where number is taken as input.
num = int(input("Enter a number"))
if num % 2 == 0:
    print("Given number is even")
else:
    print("Given number is odd")
				
			
				
					# WAP to check if 3rd last character of a string is a vowel or not.
word = input("Enter a word")
if len(word)<=2:
    print("Given word is too small")
else:
    char = word[-3]
    if char.lower() in "aeiou":
        print("It is a Vowel")
    else:
        print("Not a vowel")
				
			
				
					# Check if the first and last number of a list is same or not.
l = [1, 2, 3, 46, 9, 2, 1, 5, 5]
if l[0] == l[-1]:
    print("First and Last number is same")
else:
    print("They are not same")
				
			
				
					# Calculate income tax for the input income by adhering to the Indian rules.
# there is no tax on income till Rs. 250000, 
# 5% on between Rs. 250001 to Rs. 500000, 
# 10% on between Rs. 500001 to Rs. 1000000,
# 20% on between Rs. 1000001 to Rs. 2000000,
# 30% on between Rs. 2000001 to Rs. 3000000,
# 40% on above Rs. 3000001

income = int(input("Enter your income"))
if income <= 250000:
    total_tax = 0
elif income>250000 and income<=500000:
    taxableincome = income-250000
    total_tax = taxableincome*0.05
elif income>=500001 and income<=1000000:
    taxableincome = income-500000
    tax1=(taxableincome/100)*10
    total_tax= tax1+12500
elif income>=1000001 and income<=2000000:
    taxableincome = income-1000000
    tax1=(taxableincome/100)*20
    total_tax= tax1+12500+50000
elif income>=2000001 and income<=3000000:
    taxableincome = income-2000000
    tax1=(taxableincome/100)*30
    total_tax= tax1+12500+50000+200000
elif income>=3000001 :
    taxableincome = income-3000000
    tax1=(taxableincome/100)*40
    total_tax= tax1+12500+50000+200000+300000
print("Payable tax: ", total_tax)
				
			
				
					# Write a Python program to check whether a string starts with vowel or not.
word = input("Enter a Word")
if word[0].lower() in "aeiou":
    print("True")
else:
    print("False")
				
			
				
					# WAP to calculate percentage of a student through 5 subjects. Take marks as input from the user.
# Using percentage print which grade the student has scored.

m = int(input("Enter number for maths: "))
p = int(input("Enter number for phy: "))
c = int(input("Enter number for chem: "))
b = int(input("Enter number for bio: "))
e = int(input("Enter number for eng: "))
sub_marks = m + p + c + b + e
total_mark=int(input("Enter Maximum marks"))
perc = (sub_marks/total_mark)*100

if perc>90:
    print("You have got A+ Grade")
elif perc<=90 and perc>80:
    print("You have got A Grade")
elif perc<=80 and perc>70:
    print("You have got B+ Grade")
elif perc<=70 and perc>60:
    print("You have got B Grade")
elif perc<=60 and perc>50:
    print("You have got C+ Grade")
elif perc<=50 and perc>40:
    print("You have got C Grade")
elif perc<=40 and perc>33:
    print("You have got D Grade")
else:
    print("You are Fail")
				
			
				
					# WAP to convert a celsius value to fahrenheit. Take celsius value as input.
cel = float(input("Enter temperature in celsius:"))
Fahr = (cel*9/5)+32
print("In Fahrenheit the temperature is:",Fahr)
				
			
				
					# WAP to calculate the surface area of a triangle. Take the length of sides as input.
a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
c = float(input("Enter side c: "))

# Check if the sides form a valid triangle before calculating area
if a + b > c and a + c > b and b + c > a:
    s = (a + b + c) / 2  # Semi-perimeter
    area = (s * (s - a) * (s - b) * (s - c))**(1/2)
    print(f"Surface area of Triangle is : {area}")
else:
    print("Invalid triangle sides!")
				
			
				
					# WAP to print the day based on the number input.
# For example: if input = 1, output = Monday
d = {1:"Monday", 2:"Tuesday", 3:"Wednesday",4:"Thrusday", 5:"Friday", 6:"Saturday", 7:"Sunday"}
num=int(input("Enter a number"))
if num<7 and num>=1:
    print(d[num])
else:
    print("Invalid Number")
				
			
				
					# WAP to check whether a person is eligible to vote or not based on their age.
age = int(input("Enter your age"))
if age >= 18:
    print("You are eligible for voting")
else:
    print("You are not eligible for voting")
				
			
				
					# WAP to calculate the count of elements in a string.
word = input("Enter a Word")
count = 0
for i in word:
    count = count+1
print("length of Given word is",count)
				
			
				
					# WAP to check using the sides of a triangle to tell if it is equilateral triangle or not.
a = float(input("Enter side a: "))
b = float(input("Enter side b: "))
c = float(input("Enter side c: "))
if a == b == c:
    print("Given Triangle is Equilateral Triangle")
else:
    print("Given Triangel is not a Euilateral Triangle")
				
			
				
					# WAP to check if the 2nd last digit of numerical input from the user is divisible by 3 or not.
num = int(input("Enter a number:"))
if num<10:
    print("Not divisible by zero")
else:
    num = num % 100
    num = num // 10
    if num % 3 == 0:
        print("It is divisible by 3")
    else:
        print("It is not divisible by 3")  
				
			
				
					# Write a Python program that takes two numbers as input from the user and prints the larger of the two numbers.
num1 = int(input("Enter first number"))
num2 = int(input("Enter second number"))
if num1 > num2:
    print("First number is greater than second")
elif num1 == num2:
    print("Both are equal")
else:
    print("Second number is greater than first")