0% found this document useful (0 votes)
8 views

Unit3-If-else Statement

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

Unit3-If-else Statement

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Unit-3

Decision making using if-else


statement
If-else statement [Decision
Syntax/Rules: making]
if condition:
statement-1
else:
statement-2

It is used to perform decision making. The condition is evaluated first.


If it is true, statement-1 will be executed.
If it is false, statement-2 will be executed.

Example:
if mark >= 50:
print “PASS…Congrats!” #(statement-1)
else:
print “FAIL….Need to study hard” #(statement-2)
Python Operators

Operators are used to perform operations on variables and


values.
Python divides the operators in the following groups:

 Arithmetic operators (+, -, *, /, %)


 Relational operators (Example as following)
 Logical operators (AND, OR ) and or

Example of Relational Operators:


Conditions are formed using
Relational operators.
> greater than
< less than
>= greater than or equal
<= less than or equal
== equal
!= not equal
Syntax/Rules:
if condition is true:
statement-1
else:
statement-2
Write a Python Program to check whether a student is pass or
fail based on his mark.
(50 and above is considered as pass)
Daddy slide
 num1=int(input("please enter first number = "))
 num2=int(input("please enter second number ="))
 if num1 > num2:
 print ("first number is bigger than second")
elif num1 == num2:
print ("the numbers are the same")
else:

print ("second number is bigger than first")


Practice the following exercise
Write a Python Program to check whether a student is tall or short
based on his height
(160cm and above is considered as tall).

if height >= 160:


print("You are Tall")
else:
print("You are Short")
print("Bye")
Solution:
Nested if-else statement

Write a python program to find the exam grade of a mark


received from the user.
Marks Grade
80-100 A
60-79 B
50-59 C
0-49 F
Solution

mark=float(input("Enter the marks = "))


if mark>=80 and mark<=100:
print("Your Grade is A")
elif mark>=60:
print("Your Grade is B")
elif mark>=50:
print("Your Grade is C")
else:
print("Your Grade is F")
print("BYE")

Output
Enter the marks = Enter the marks = Enter the marks =
90 60 45
Your Grade is A Your Grade is B Your Grade is F
BYE BYE BYE
Solution
mark=float(input("Enter the marks = "))
if mark>=80 and mark<=100:
print("Your Grade is A")
elif mark>=60 and mark<=79:
print("Your Grade is B")
elif mark>=50 and mark<=59:
print("Your Grade is C")
elif mark>=0 and mark<=49:
print("Your Grade is F")
else:
print("Your input is wrong!!")

print("BYE")
Practice program
Solution
Solution

weight=float(input("Enter Weight="))
height=float(input("Enter Height="))
bmi=weight/(height*height)
bmi=round(bmi,2)
print("your BMI=", bmi)
if bmi<18.5:
print("You are under weight; You need to eat well")
elif bmi<25:
print("You are normal weight; Congratulation! Keep
it up")
else:
print("You are overweight; you need to do more
exercise")
Program to check whether a number is even or not.

num = int(input("enter the number?"))


if num%2 == 0: #the number is
divided with 2, and if remainder is 0
then even
print("Number is even...")
else:
print("Number is odd...")
Write a python script to input the
pulse count for 15 seconds of a
person and decide his health from
this count.
Pulse per Health Condition
minute
Greater than POOR
81
70 to 81 AVERAGE
62 to 69 GOOD
49 to 61 EXCELLENT
Below 49 Wrong reading

You might also like