DEV Community

Ezhil Abinaya K
Ezhil Abinaya K

Posted on

If,elif,else Statements and Practice Programs in Python

Conditional Statements in Python
Conditional statements are used to control the flow of execution in a program based on specific conditions. They allow programs to execute different blocks of code depending on whether a condition evaluates to True or False.
If Statement
If statement is used to execute a block of code only when a specified condition evaluates to True.

age = 20
if age >= 18:
    print("Eligible to vote.")
//Eligible to vote.
Enter fullscreen mode Exit fullscreen mode

Short Hand if
Short-hand if is used to write if statements in a single line. It is useful when only one statement needs to be executed.

age = 20
if age >= 18:print("Eligible to vote.")
//Eligible to vote.
Enter fullscreen mode Exit fullscreen mode

In other language the if statement condition must inside the parentheses. but,in python there is no necessary need to put the parentheses for if().We write if condition or if(condition) also.

mark1=90
mark2=96;
if mark1>mark2:
    print("mark1 is greater")
elif mark2>mark1:
    print("mark2 is greater")
else:
    print("both are equal")
//   
mark2 is greater
Enter fullscreen mode Exit fullscreen mode

elif is nothing it is same as else if . In python we use elif.
Additional Information
In your free time, read the "zen of python".It is a collection guiding principles.
Programs

  1. Write a Program to display the last digit of a number.  2.Write a Program to check if a single character is a vowel or not. Take the character as input from the user.(TBD)
ch = input('Enter a single character: ')
Enter a single character: i
if ch.lower() in "aeiou":
    print("It is Vowel")
else:
    print("It is not a vowel")
//   
It is Vowel
Enter fullscreen mode Exit fullscreen mode

3.Write a Program to check if a number is even or odd where number is taken as input.

num = int(input("Enter a number"))
Enter a number1
if num % 2 == 0:
    print("Given number is even")
else:
    print("Given number is odd")
//
 Given number is odd
Enter fullscreen mode Exit fullscreen mode

4.Write a Program to check if 3rd last character of a string is a vowel or not.(TBD)

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")
Enter fullscreen mode Exit fullscreen mode

5.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")
//
They are not same
Enter fullscreen mode Exit fullscreen mode

6.Calculate income tax for the input income by adhering to the Indian rules.(TBD)
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)
Enter fullscreen mode Exit fullscreen mode

7.Write a Program 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: "))
Enter number for maths: 100
p = int(input("Enter number for phy: "))
Enter number for phy: 80
c = int(input("Enter number for chem: "))
Enter number for chem: 80
b = int(input("Enter number for bio: "))
Enter number for bio: 79
e = int(input("Enter number for eng: "))
Enter number for eng: 89
sub_marks = m + p + c + b + e
total_mark=int(input("Enter Maximum marks"))
Enter Maximum marks400
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")

 //
You have got A+ Grade
Enter fullscreen mode Exit fullscreen mode

8.Write a Program to print the day based on the number input.

d = {1:"Monday", 2:"Tuesday", 3:"Wednesday",4:"Thrusday", 5:"Friday", 6:"Saturday", 7:"Sunday"}
num=int(input("Enter a number"))
Enter a number: 7
if num<7 and num>=1:
    print(d[num])
else:
    print("Invalid Number")
//    
Invalid Number
Enter fullscreen mode Exit fullscreen mode

References
https://www.geeksforgeeks.org/python/conditional-statements-in-python/
https://edslash.com/if-else-practice-questions-in-python/

Top comments (0)