CLASS 9_AI_python_IF_else (1)
CLASS 9_AI_python_IF_else (1)
If else condition
Why we need “if” condition?
a = 33
b = 200
if b > a:
print("b is greater than a")
Indentation
Python relies on indentation (whitespace at the
beginning of a line) to define scope in the code. Other
programming languages often use curly-brackets for this
purpose.
Example
If statement, without indentation (will raise an error):
a = 33
b = 200
if b > a:
print("b is greater than a") # you will get an error
Questions
n=int(input("Enter a number:"))
if(n>0):
print(n,"is positive number")
elif(n==0):
print(n,"is equal to zero")
else:
print(n,"is a negative number")
#Program to reads two numbers and an arithmetic operator and displays
the computed result.
marks=float(input("Enter marks:"))
if marks<25:
print("F")
elif marks>=25 and marks<45:
print("E")
elif marks>=45 and marks<50:
print("D")
elif marks>=50 and marks<60:
print("C")
elif marks>=60 and marks<80:
print("B")
else:
print("A")
#Program to print the day name of the week using if-elif condition
day=int(input("enter the number between 1-7:"))
if day == 1:
print("Monday")
elif day == 2:
print("Tuesday")
elif day == 3:
print("Wednesday")
elif day == 4:
print("Thursday")
elif day == 5:
print("Friday")
elif day == 6:
print("Saturday")
elif day == 7:
print("Sunday")
else:
print("invalid")