0% found this document useful (0 votes)
6 views4 pages

Lab 12

Uploaded by

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

Lab 12

Uploaded by

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

COMSATS University Islamabad, Wah Campus

Department of Computer Science

Applications of ICT

Lab Manual: 12
Python: Conditional Structure
CLO-5(SO-2,4)
Objective: The objective of this lab will be to learn about conditional statements with the help of examples
and learning tasks

Activity Outcomes: The activities provide hands - on practice with the following topics
● Implement an if statement.
● Implement an if-else statement.
● Implement an if-elif statement
● Nest if-else statements.

1) Useful concepts:
Condition statements allow us to write code that behaves differently in different scenarios.

1. The most basic conditional statement is an if statement. The syntax of an if statement is given
below. The code inside the if statement would only execute if the condition is fulfilled i.e the
condition inside the round brackets returns true.

2. We can have another scenario in which in one condition we want to do one thing but in another
condition, we want to do something else. This can be done by using if-else. The syntax is given
under. The else statement runs only when the condition corresponding to the if block returns
false.

3. When we have multiple conditions and we want to write different code for each of them, we can
use if elif else. If we have 3 conditions lets say, the first condition would be checked using an if
statement, the second with the elif statement and then third can either be checked with an elif
again or just else as there is no other scenario.
4. The nested if statement can be used to implement multiple alternatives. The statement given in
below Figure, for instance, assigns a letter value to the variable grade according to the score,
with multiple alternatives.

Activity 1: Python program to illustrate an if statement.

Activity 2: Python program to illustrate an if else statement.

Activity 3: Python program to illustrate an elif statement.


Activity 4: Python program to illustrate nested if else statements.

Activity 5: Write a program to find out the Chinese zodiac sign for a given year. The Chinese
zodiac sign is based on a 12-year cycle, and each year in this cycle is represented by an animal
—monkey, rooster, dog, pig, rat, ox, tiger, rabbit, dragon, snake, horse, and sheep.
Lab Tasks:

1. Write a program to check whether an integer is positive, negative, or zero.


2. Write a program to input marks of five subjects Physics, Chemistry, Biology, Mathematics,
and Computer. Calculate percentage and grade according to following:
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F
3. Write a program to check whether the triangle is equilateral, isosceles or scalene triangle.
4. Write a program to check whether a year is a leap year or not.
5. Write a dummy authentication system program in which you accept user inputs for email and
password. Let's say the correct email and password are [email protected] and abc respectively.
If the email and password entered are correct it should display “User is logged in”. If the
email is correct, then prompt the user that the password is not correct. If the password is
correct then prompt the user to enter the correct email. If both are incorrect then display the
corresponding message.

You might also like