0% found this document useful (0 votes)
14 views

Python - Worksheet 3

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Python - Worksheet 3

Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Python

Name _________________ Grade _________

Assignment 3

Exploring the Effectiveness of the Conditional Statements

Task 1
Let’s start by writing some text.

o Open the blank Python template: https://www.onlinegdb.com/online_python_compiler

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

Python has 3 key Conditional Statements that you should know:

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:

# execute code block

To better understand this, take a look at the below example.

Python Example: Python program to check if a number is odd or even.

if i % 2 != 0:

# if condition is True print "odd"

print (i, "is an odd number")

# check if no. is even

if i % 2 == 0:

# if condition is false print "even"

print (i, "is an even number")

Paste your code here:

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:

# execute code if condition is true

else:

# execute code if condition if False

3
Python Example: Python program to check if a number is odd or even.

# check if no. is odd

if i%2 != 0:

# if condition is True print "odd"

print(i, "is an odd number")

# if the no. is not odd then the no. is even therefore print "even"

else:

# if condition is True print "even"

print(i, "is an even number")

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:

# execute this statement

elif condition2:

# execute this statement

..

..

else:

# if non of the above conditions evaluate to True execute this statement

Task 2

Python Example: Python program to check if a string matches the condition.

# take a sample string

string ="JBCNInternationalSchoolChembur"

# check if value in the string variable

# matches "JBCN"

if string == "JBCN":

print ("The first condition is true")

# check if value in the string variable

5
# matches "JBCNInternational"

elif string == "JBCNInternational":

print("The second condition is true")

# check if value in the string variable

# matches " JBCNInternationalSchool"

elif string == " JBCNInternationalSchool":

print("The third condition is true")

# check if value in the string variable

# matches " JBCNInternationalSchoolChembur"

elif string=="JBCNInternationalSchoolChembur":

print("The fourth condition is true")

# if none of the above

# conditions evaluate to true

# execute the code inside the else block

else:

print ("All the above conditions are false")

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:

2. To check whether the number is zero, positive or negative.

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:

 Dog: "Woof woof!"


 Cat: "Meow!"
 Duck: "Quack quack!"
 Cow: "Moo!"

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

Below 40- E (Fail)

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.

Use the following marks obtained by the student:

Math: 85 English: 75 Science: 90 History: 80 Art: 95

The report card should display the following information:

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.

The report card should display the following information:

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

You might also like