Blog

Python if else condition

If else is the condition used in different programming language, Here in python we use if ,elif and else keywords to understand by interpreter.

Mostly used python if else conditions

  • Equals: a == b
  • Not Equals: a != b
  • Less than: a < b
  • Less than or equal to: a <= b
  • Greater than: a > b
  • Greater than or equal to: a >= b

Simple if else program :

a=30
b= 20
if a>b:
    print('a is greatest number')
else:
    print(' b is greatest number')

In the above example when the condition is satisfied then only the python print the output, the above output is : ” a is greatest number”

Example using only if else (no elif) to find the greatest number among 3 numbers :

x = 10
y = 20
z = 30

if x >= y:
    if x >= z:
        greatest = z
    else:
        greatest = z
else:
    if y >= z:
        greatest = y
    else:
        greatest = z

print(f'Greatest number among {x},{y} and {y} is', greatest)

Output
Greatest number among 10, 20 ,30 is 30

Exam result using if elif and else condition:

print("==================enter the marks =============")
nep = int(input("enter the number of nepali subject"))
math = int(input("enter the number of math subject"))
sci = int(input("enter the number of science subject"))
eng = int(input("enter the number of english subject"))
pop = int(input("enter the number of population subject"))

print("==================  =============")
sum = nep + math + sci + eng + pop

if nep < 35:
    print('you are fail in nepali subject')

if math < 35:
    print('you are fail in math subject')

if sci < 35:
    print('you are fail in science subject')

if eng < 35:
    print('you are fail in english subject')

if pop < 35:
    print('you are fail in population subject')

print(f'total mark={sum}')
percentage = sum / 5
print(f'percentage= {percentage}')
if percentage > 35 and percentage < 45:
    print('pass')
elif percentage > 45 and percentage < 60:
    print('second division')
elif percentage > 60 and percentage < 75:
    print('first division')
elif percentage > 75 and percentage < 85:
    print('first division')
else:
    print("topper")

Output