Lab 12
Lab 12
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 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: