Python - Worksheet 3
Python - Worksheet 3
Assignment 3
Task 1
Let’s start by writing some text.
A conditional statement is used to handle conditions in your program. These statements guide the
program while making decisions based on the conditions encountered by the program. In this article,
we will explore three conditional statements in Python: if statement, if-else statements, if-elif-else
ladder.
Python also has some predefined conditional statements. A conditional statement as the name
suggests itself, is used to handle conditions in your program. These statements guide the program
if statement
if-else statement
if-elif-else ladder
1
Let’s discuss each of them in detail.
- if Statement
The if statement is a conditional statement in Python used to determine whether a block of code will be
executed. If the program finds the condition defined in the if statement true, it will execute the code
block inside the if statement.
Syntax:
if condition:
if i % 2 != 0:
if i % 2 == 0:
2
Output:
Sample Output
if-else Statement
if statement executes the code block when the condition is true. Similarly, the else statement works in
conjuncture with the if statement to execute a code block when the defined if condition is false.
Syntax:
if condition:
else:
3
Python Example: Python program to check if a number is odd or even.
if i%2 != 0:
# if the no. is not odd then the no. is even therefore print "even"
else:
Output:
4
if-elif-else ladder
The elif statement is used to check for multiple conditions and execute the code block within if any of
the conditions are evaluated to be true.
The elif statement is similar to the else statement in that it is optional. Still, unlike the else statement,
multiple elif statements can be in a block of code following an if statement.
if condition1:
elif condition2:
..
..
else:
Task 2
string ="JBCNInternationalSchoolChembur"
# matches "JBCN"
if string == "JBCN":
5
# matches "JBCNInternational"
elif string=="JBCNInternationalSchoolChembur":
else:
Output:
6
The fourth condition is true
Code:
Task 3
Class activity
1. To check whether the person is eligible to vote age should be 18 and above.
Code:
7
Output:
Code:
Output:
8
3. To check whether the student has passed or failed minimum marks is 50.
Code:
Output:
9
Task 4
Write a Python program that asks the user to enter the name of an animal. Once the animal
name is entered, the program should print out the sound that animal makes. If the animal is
not recognized, the program should print "Sorry, I don't know what sound that animal makes!"
Use the following animal sounds:
Code:
Output:
1
Task 3
Creating a Report Card
You are a teacher responsible for preparing report cards for your students. You need to create a report
card for one of your students, showcasing their performance in five subjects: Math, English, Science,
History, and Art. Each subject has a total of 100 marks. Your report card should include the student's
name, percentage grade for each subject, and the total percentage.
Grades: Above 90 - A*
89 – 70 --- A
69 – 60 --- B
59 – 50 --- C
49 – 40 --- D
1
Constant
Create a Python program to generate a report card for the student named Jones Smith. The program
should calculate the percentage grade for each subject based on the marks obtained and subject
average then print out the report card.
Dear Student's Name you have received Marks Obtained out of Total Marks with an average
SubjectAverage and received percentage Percentage and have secured a grade Grade.
Code
Output
Variable
Create a Python program to generate a report card for the student as per the user input. The program
should calculate the percentage, grade as per the marks obtained and then print out the report card.
1
Dear Student's Name you have received Marks Obtained out of Total Marks and received
percentage Percentage and have secured a grade Grade.
Ensure the program is well-structured and readable, with appropriate comments to explain the code
logic.
Code
Output