Example : A Python program to display a larger value from two accepted values using an if statement.
num1 = input("Enter first value = ")
num2 = input("Enter second value = ")

if num1 > num2:
  print(num1, "is greater than", num2)
if num1 < num2:
  print(num2, "is greater than", num1)
if num1 == num2:
  print("Both values are Equal")

Output:
  Enter first value = 20
  Enter second value = 20
  Both values are Equal
Example : A Python program to display the largest value from the accepted three values using an if statement.
num1 = input("Enter first value = ")
num2 = input("Enter second value = ")
num3 = input("Enter third value = ")

if num1 > num2 and num1 > num3:
  print(num1, "is greater than", num2, "and", num3)
if num2 > num1 and num2 > num3:
  print(num2, "is greater than", num1, "and", num3)
if num3 > num1 and num3 > num2:
  print(num3, "is greater than", num1, "and", num2)
if num1 == num2 == num3:
  print("All the three values are equal")

Output:
  Enter first value = 10
  Enter second value = 10
  Enter third value = 10
  All the three values are equal
Example : A Python program to display a message when a condition is True/False using an if-else statement.
age = input("Enter your age value = ")

if int(age) > 18:
  print("Eligible for Vote")
else:
  print("Not eligible for Vote")

Output:
  Enter your age value = 45
  Eligible for Vote
Example : A Python program to display the user-accepted single value as Odd or Even using an if-else statement.
num1 = input("Enter a value = ")
if int(num1) % 2 == 0:
  print (num1, "is Even no.")
else:
  print (num1, "is Odd no.")

Output:
  Enter a value = 23
  23 is Odd no.
Example : A Python program to check whether the entered Year’s value is a Leap Year or not using an if-else statement.
year = int(input("Enter a year : "))

if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
    print(year, "is a leap year")
else:
    print(year, "is not a leap year")

Output:

Enter a year : 2001
2001 is not a leap year
Example : A Python program to display a student’s grade appearing in an exam using an if-elif-else statement.
tmarks = input("Enter your obtained total marks between 300 to 900 = ")

if int(tmarks) > 900 or int(tmarks) < 300:
  print("Entered Total marks are Incorrect, Check Again and Re-enter")
elif int(tmarks) >= 700 and int(tmarks) <= 900:
  print("A++ Grade")
elif int(tmarks) >= 600 and int(tmarks) <= 699:
  print("A Grade")
elif int(tmarks) >= 500 and int(tmarks) <= 599:
  print("B Grade")
elif int(tmarks) >= 400 and int(tmarks) <= 499:
  print("C Grade")
else:
  print("Not Satisfactory")

Output:
  Enter your obtained total marks between 300 to 900 = 600
  A Grade
Example : A Python program to count the no. of Vowels, Consonants, and Spaces from Sentences using an if-elif-else statement.
text = input("Enter a sentence: ").lower()

vowels = consonants = space = 0

for char in text:
    if char in "aeiou":
        vowels = vowels + 1
    elif char.isalpha():
        consonants = consonants + 1
    elif char == ' ':
    #elif char.isspace():
        space= space + 1
        
print("Vowels count is :", vowels,", Consonants count is :", consonants, "and space is =", space)

Output:

Enter a sentence: India is my Country.
Vowels count is : 6 , Consonants count is : 10 and space is = 3
Example : A Python program to display the names of days using a (Switch)Match Case Statement.
value = int(input("Enter your choice between 1-7 = "))

match value:

    case 1:
        print ("Sunday")
    case 2:
        print ("Monday")
    case 3:
        print ("Tuesday")
    case 4:
        print ("Wednesday")
    case 5:
        print ("Thursday")
    case 6:
        print ("Friday")
    case 7:
        print("Saturday")
    case default:
        print ("something")
    #case _:
        #print ("something")

Output: 
  Enter your choice between 1-7 = 4
  Wednesday

------------------  OR  -------------------

value = int(input("Enter your choice between 1-35 = "))

match value:
    case 1 | 2 | 3 | 4| 5:
        print("Sunday")
    case 6| 7| 8| 9| 10:
        print("Monday")
    case 11| 12| 13| 14| 15:
        print("Tuesday")
    case 16| 17| 18| 19| 20:
        print("Wednesday")
    case 21| 22| 23| 24| 25:
        print("Thursday")
    case 26| 27| 28| 29| 30:
        print("Friday")
    case 31| 32| 33| 34| 35:
        print("Saturday")
    case _:
        print("Wrong Choice")

Output:
Enter your choice between 1-35 = 23
Thursday

------------------  OR  -------------------

value = int(input("Enter your choice between 10-45 = "))

match value:
    case _ if 10 <= value <= 15:
        print("Sunday")
    case _ if 16 <= value <= 20:
        print("Monday")
    case _ if 21 <= value <= 25:
        print("Tuesday")
    case _ if 26 <= value <= 30:
        print("Wednesday")
    case _ if 31 <= value <= 35:
        print("Thursday")
    case _ if 36 <= value <= 40:
        print("Friday")
    case _ if 41 <= value <= 45:
        print("Saturday")
    case _:
        print("Wrong Choice")

Output:
Enter your choice between 10-45 = 70
Wrong Choice

------------------  OR  -------------------

value = (input("Enter your small vowel letters = "))

match value:

    case "a":
        print ("First vowel letter a ")
    case "A":
        print("First vowel letter A ")
    case "e":
        print ("Second vowel letter e ")
    case "E":
        print("Second vowel letter E ")
    case "i":
        print ("Third vowel letter i ")
    case "I":
        print("Third vowel letter I ")
    case "o":
        print ("Fourth vowel letter o ")
    case "O":
        print("Fourth vowel letter O ")
    case "u":
        print("Fifth vowel letter u ")
    case "U":
        print("Fifth vowel letter U ")
    case default:
        print ("Other letters")
    #case _:
        #print ("something")

Output:
  Enter your small vowel letters = U
  Fifth vowel letter U

------------------  OR  -------------------

value = (input("Enter your small vowel letters = "))

match value:
    
    case "a" | "A":
        print ("First vowel letter a ")
    case "e" | "E":
        print ("Second vowel letter e ")
    case "i" | "I":
        print ("Third vowel letter i ")
    case "o" | "O":
        print ("Fourth vowel letter o ")
    case "u" | "U":
        print("Fifth vowel letter u ")
    case default:
        print ("Wrong Choices")
    #case _:
        #print ("something")

Output:
Enter your small vowel letters = e
Second vowel letter e 

------------------  OR  -------------------

value1 = int(input("Enter First Number = "))
value2 = int(input("Enter Second Number = "))

choice=input("Enter your operation(add,sub,mul,div,mod) = ").lower()

match choice:

    case "add":
        print (value1 + value2)
    case "sub":
        print(value1 - value2)
    case "mul":
        print (value1 * value2)
    case "div":
        print(value1 / value2)
    case "mod":
        print (value1 % value2)
    case default:
        print ("Invalid Choice, Re-enter cor ṣrect choice again")
    #case _:
        #print ("Invalid Choice, Re-enter correct choice again")
   
Output:
  Enter First Number = 45
  Enter Second Number = 6
  Enter your operation = mod
  3

NB: 'match' keyword is available from python version 3.10

Some Important links:

Loading

Categories: Python Theory

0 Comments

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.