Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
12 views

Python LAB 04998

The document discusses conditional statements and decision making in Python. It covers the if, if-else and if-elif-else statements. The if statement executes a block of code if a condition is true. The if-else statement executes one block if the condition is true and another block if it is false. The if-elif-else statement checks multiple conditions and executes the block for the first true condition. Exercises are included to practice these statements by correcting code snippets and writing Python programs to solve problems using conditional logic.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python LAB 04998

The document discusses conditional statements and decision making in Python. It covers the if, if-else and if-elif-else statements. The if statement executes a block of code if a condition is true. The if-else statement executes one block if the condition is true and another block if it is false. The if-elif-else statement checks multiple conditions and executes the block for the first true condition. Exercises are included to practice these statements by correcting code snippets and writing Python programs to solve problems using conditional logic.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Programming Fundamentals (CS-116L) SSUET/QR/114

LAB # 04

DECISIONS

OBJECTIVE
To get familiar with the concept of conditional statement for simple decision making.

THEORY
Decision making statements in programming languages decides the direction of flow of
program execution.
The if…elif…else statement is used in Python for decision making.

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

As a general rule, we express a condition using python’s ‘relational’ operators. The


relational operators allow us to compare two values to see whether they are equal to
each other, unequal, or whether one is greater than the other. Here is how they look and
how they are evaluated in python.

This expression Is true if


x==y x is equal to y
x!=y x is not equal to y
x<y x is less than y
x>y x is greater than y
x<=y x is less than or equal to y
x>=y x is greater than or equal to y

Example:
# python program to illustrate If statement
1
Programming Fundamentals (CS-116L) SSUET/QR/114

i = 10
if (i > 15):
print (i, "is greater than 15")
print ("I am not greater")
Output:
>>> %Run task1.py
I am not greater
>>>

The if-else Statement


We can use the else statement with if statement to execute a block of code when the
condition is false.

Syntax
if (condition):
# Executes this block if
# condition is true
else:
# Executes this block if
# condition is false

Example:
# python program to illustrate If else statement
i = 20;
if (i < 15):
print (i,“is smaller than 15")
print ("i'm in if Block")
else:
print (i,"is greater than 15")
print ("i'm in else Block")
print ("i'm not in if and not in else Block")

Output:
>>> %Run task2.py
20 is greater than 15
i'm in else Block
i'm not in if and not in else Block
>>>

The if-elif-else Statement


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 (condition):
statement
elif (condition):
statement
.
2
Programming Fundamentals (CS-116L) SSUET/QR/114

.
else:
statement

Example:
# Python program to illustrate if-elif-else
i = 30
if (i == 10):
print ("i is 10")
elif (i == 20):
print ("i is 20")
elif (i == 30):
print ("i is 30")
else:
print ("i is not present")
Output:
>>> %Run task3.py
i is 30
>>>

3
Programming Fundamentals (CS-116L) SSUET/QR/114

EXERCISE

A. Point out the errors, if any, and paste the output also in the following Python
programs.
1. Code
a = 500,b,c;
if ( a >= 400 ):
b = 300
c = 200
print( "Value is:", b, c )

Correct Code:
a = 500;
if ( a >= 400 ):
b = 300
c = 200
print( "Value is:", b, c )

Output:

2. Code
&number = eval(input("Enter an integer: "))
print(type(number))
if number % 5 == 0
print("HiFive")
else
print("No answer")

Correct Code:
number = eval(input("Enter an integer: "))
print(type(number))
if number % 5 == 0:
print("HiFive")
else:
print("No answer")

4
Programming Fundamentals (CS-116L) SSUET/QR/114

Output:

3. Code
if score >= 60.0
grade = 'D'
elif score >= 70.0
grade = 'C'
elif score >= 80.0
grade = 'B'
elif score >= 90.0
grade = 'A'
else:
grade = 'F'
Correct Code:
score = int(input("Score: "))
if score >= 60.0:
print("grade = 'D'")
elif score >= 70.0:
print(grade = 'C')
elif score >= 80.0:
print(grade = 'B')
elif score >= 90.0:
print(grade = 'A')
else:
print(grade = 'F')

Output:

B. What would be the output of the following programs:


1. Code
requested_topping = 'mushrooms'
if requested_topping != 'anchovies':
print("Hold the anchovies!")
5
Programming Fundamentals (CS-116L) SSUET/QR/114

Output:

2. Code
num = 3
if num >= 0:
print("Positive or Zero")
else:
print("Negative number")

Output

3. 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

6
Programming Fundamentals (CS-116L) SSUET/QR/114

C. Write Python programs for the following:

1. Any integer is input through the keyboard. Write a program to find out whether it is
an odd number or even number.
Code:
a = int(input("Enter Integer:"))
if a % 2 == 0:
print("a is Even")
else:
print("a is Odd")

Output:

2. Write a program that asks for years of service and qualification from the user and
calculates the salary as per the following table:
Years of Service Qualifications Salary
>= 10 Masters 150,000
>= 10 Bachelors 100,000
< 10 Masters 100,000
< 10 Bachelors 70,000

Code:
service = int(input("Enter Years of Services: "))
qual = input("Enter Qualification: ").lower()
if service >=10 and qual == 'masters':
print("Your Salary is 150000")
elif service >=10 and qual == 'bachelors':
print("Your Salary is 100000")
elif service <10 and qual == 'masters':
print("Your Salary is 100000")
elif service <10 and qual == 'bachelors':
print("Your Salary is 70000")
else:
print("An Erorr Occured")
Output:
7
Programming Fundamentals (CS-116L) SSUET/QR/114

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 a
toddler.
• If the person is at least 4 years old but less than 13, print a message that the person is a
kid.
• If the person is at least 13 years old but less than 20, print a message that the person is
a teenager.
• If the person is at least 20 years old but less than 65, print a message that the person is
an adult.
• If the person is age 65 or older, print a message that the person is an elder.

Code: Output:
person = int(input("Enter Person’s Age"))
if person<2:
print("The Person is Baby")
elif person>=2 and person<4:
print("Person is toddler")
elif person<13 and person>=4:
print("Person is Teenager")
elif person<20 and person>=13:
print("Person is Adult")
elif person<65 and person>=20:
print("Person is Adult")
elif person>=65:
print("Person is Elder")

8
Programming Fundamentals (CS-116L) SSUET/QR/114

You might also like