Lab 10
Lab 10
LAB # 10
DECISION MAKING STATEMENT
OBJECTIVE
To get familiar with the concept of conditional statement for simple decision making.
THEORY
The if Statement
Like most languages, python uses the keyword if to implement the decision control
instruction. It is used to decide whether a certain statement or block of statements will
be executed or not i.e if a certain condition is true then a block of statement is executed
otherwise not. The general form of if statement looks like this:
Syntax:
if condition:
# Statements to
execute if # condition
is true
Example:
Output:
We can use the else statement with if statement to execute a block of code when
thecondition is false.
Syntax
if (condition):
# Executes this
block if # condition
is true
else:
# Executes this
block if # condition
is false
Example:
The elif is short for else if. It allows us to check for multiple expressions.
If the condition for if is False, it checks the condition of the next elif block and so on.
If all the conditions are False, body of else is executed.
Syntax
if
(conditi
on):
stateme
nt
elif
(conditio
n):
statement
.
.
else:
statement
Example:
EXERCISE
1. Code
a = 500,b,c;
if ( a >= 400 ):
b = 300
c = 200
print( "Value is:", b, c )
Output:
2. Code
Output:
1. Code
num = 3
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")
Output
2. Code
age = 15
if age < 4:
price = 0
elif age < 18:
price = 1500
else:
price = 2000
print("Your admission cost is Rs" + str(price) + ".")
Output
Lab Tasks:
1. Any integer is input through the keyboard. Write a program to find out whether
it isan odd number or even number.
2. Write a program that asks for years of service and qualification from the user
andcalculates the salary as per the following table:
3. Write an if-elif-else chain that determines a person’s stage of life, take input
value for the variable age, and then apply these conditions:
If the person is less than 2 years old, print a message that the person is a baby.
If the person is at least 2 years old but less than 4, print a message that the
person is atoddler.
If the person is at least 4 years old but less than 13, print a message that the
person is akid.
If the person is at least 13 years old but less than 20, print a message that
the person isa teenager.
If the person is at least 20 years old but less than 65, print a message that
the person isan adult.
If the person is age 65 or older, print a message that the person is an elder.