Example : A Python program to display a larger value from accepted two 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 are Equal")

Output:
  Enter first value = 20
  Enter second value = 20
  Both are Equal
Example : A Python program to display the largest value from 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 values are equal")

Output:
  Enter first value = 10
  Enter second value = 10
  Enter third value = 10
  All the values are equal
Example : A Python program to display a larger value from accepted two values 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 accepted single value is 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 display the Grade of a student 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 display the name 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 = (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  -------------------

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

choice=input("Enter your operation = ")

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 ("Other letters")
    #case _:
        #print ("something")

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.