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 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 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 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 check whether the entered Year’s value is 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 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(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.