0% found this document useful (0 votes)
8 views20 pages

ENGG1003 Lab09 PythonDataProcessing

Uploaded by

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

ENGG1003 Lab09 PythonDataProcessing

Uploaded by

masonyeung1228
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd

ENGG1003

Digital Literacy and


Computational Thinking
–P

Lab 09 Python Data Processing


2024-25 Term 1
Setup Python on Your
Computer
(Recap)
 Download and follow the steps on
Blackboard
 Lab 01 -
InstallationGuide_python_Win11_macOS.pptx
 Windows users: slides 1 – 6
 macOS users: slides 7 – 15

 You may also work on your VM !


Save Your Work Properly
(Recap)
 Setup your own filing system: create a folder
for your works in ENGG1003
 Keep on your own computer, e.g., Documents
 Keep on portable storage such as USB drive
 Keep on cloud storage such as OneDrive
(CUHK O365)

 Folder structure is hierarchical, i.e., tree-like


with branches called sub-folders
Lab 09 Activities

 In this lab, you will practice the following python


concepts
 Python tuples
 Python lists
 Defining and calling functions
 Solving problems using if: elif: else:
 Python dictionary
Academic Honesty
 You must read and observe the University Guideline on Academic
Honesty ([Link]
 You should NOT share your file to others, regardless of your
intention
 You should NOT obtain other’s works by any means
 Submitting the wrong file by “accident” will NOT be accepted as an
excuse – so please double-check which file you have submitted
 You are welcome seeking help at IT Clinic (HCA 328) during office
hours.
Important : Please read and observe the declaration statement when you upload and submit your work on
Blackboard.

I declare that the lab work here submitted is original except for source material
explicitly acknowledged, and that the same or closely related material has not been
previously submitted for another course.

I also acknowledge that I am aware of University policy and regulations on honesty in


academic works, and of the disciplinary guidelines and procedures applicable to breaches
of such policy and regulations, as contained in the website.

University Guideline on Academic Honesty:

[Link]

According to Adoption of AI Approach #2 announced by the course teacher, I have used AI


tools in completing specified part(s) of this work with prior permission from the course
teacher. Such AI usage has been properly referenced and related inputs such as prompts
have been provided. All else have been done by me, without the use of AI tools.
Lab Tasks Overview
 In this lab, we will work python concepts such as tuples, lists and
python functions and other advanced datatypes.
 Understanding the application problem, which is to estimate
rainstorm signal based on raw rainfall data, will help you
completing your tasks
 In this application there are 3 basic tasks and 1 bonus task.
 Task 1: Create a tuple, a list and perform initialization
 Task 2: Update the data in the list and perform statistics operations
 Task 3: Complete a rainstormSignal() function and call the function
 Bonus Task 4: Create a dictionary for extra printing
Ø To start, please download the provided python file from
BlackBoard.
Ø Submission: Submit the final version (one file only) containing
your work for all Tasks, no matter you attempt Bonus Task 4 or
not.
Ø Save all your works from time-to-time.
Task 1: Creating Tuples &
Lists
To start, download the “[Link]” file from Blackboard.
Launch IDLE Python, click File  Open  browse and pick
[Link] and click open.
1. Create a tuple “regions” and store the following regions:
Shatin Tuen Mun Mong Kok Sai Kung Stanley

2. Create one extra empty list i.e., rainfall, to store regional


rainfall data.
3. Initialize rainfall data to 0.0 with the help of python loop
4. Print the region/rainfall pairs stored in the tuple and list
with the help of python loop
Task 1: Hints
# Task 1: Dealing with tuples
# Create a tuple and store the regions
regions = ('Shatin', ) in all regions into one single tuple
Fill

# Create an empty list for storing the regional rainfall later


rainfall =

# Find the number of regions


num_of_regions = Use len( ) function to find the number of regions

# Append initial rainfall values


for index in : Complete the for loop by using range() function
Use append() function to add zeros into the empty (rainfall) list

# Print all regions and rainfall values


print("Rainfall Readings:") Complete the for loop and print each item in the tuple
for index in : and list
print( ) # e.g. "Shatin : 0.0 mm"
Task 1 – Check your
output
Output
Rainfall Readings:
Shatin : 0.0 mm
Tuen Mun : 0.0 mm
Mong Kok : 0.0 mm
Sai Kung : 0.0 mm
Stanley: 0.0 mm
==============================================

 Completion Reminder:
 After completing task 1, save your work in “[Link]”
 Make sure the “[Link]” file has a complete code of task 1.
Task 2: Dealing with Lists
You must work in the same “[Link]” file in which you
completed task 1
In Task 2 deals with user inputted rainfall values
1. Use the list created in Task 1, i.e., rainfall.
A. Request the user to input rainfall for the 5 regions individually
B. Use for loop and update corresponding item of the rainfall list
2. Do the following statistics operations:
A. Calculate and print the total rainfall using Python sum() function
B. Calculate and print the average rainfall
C. Calculate and print the median rainfall using Python median()
function*

Python sum() Function [Link]


Python median() Function [Link]
Task 2: Hints
# Task 1: Completed
# Task 2: Dealing with Lists
# Get the rainfall for each region from the user
Complete the for loop to get index of different
for in : regions
message = "Enter the rainfall for " + +Fill" in
: "one region item into the
message
regional_rainfall = float(input(message))
Update one rainfall item of the (rainfall)
list

# Calculate the total rainfall


You can use sum() function to calculate the
total_rainfall = total_rainfall
print(“Total rainfall is: ” , total_rainfall)

# Calculate the average rainfall


average_rainfall = You can use total_rainfall and use num_of_regions to
calculate the average_rainfall
# Print the average rainfall
print(“Average rainfall is: ” , average_rainfall)

# Calculate the median rainfall


median_rainfall = You can use median function to calculate the
median_rainfall
# Print the median rainfall
print(“Median rainfall is: ” , median_rainfall)
Task 2 – Check your
output
Sample Output 1
Enter the rainfall (mm) for Shatin :
Sample Output 2
Enter the rainfall (mm) for Shatin : 70
90 Enter the rainfall (mm) for Tuen Mun :
Enter the rainfall (mm) for Tuen 60
Mun : 70 Enter the rainfall (mm) for Mong Kok :
Enter the rainfall (mm) for Mong 40
Kok : 50 Enter the rainfall (mm) for Sai Kung :
Enter the rainfall (mm) for Sai 75
Kung : 30 Enter the rainfall (mm) for Stanley : 0
Enter the rainfall (mm) for Stanley : Total rainfall in 5 regions is : 245.0
10 mm
Total rainfall in 5 regions is : Average rainfall: 49.0 mm
250.0 mm Sample Output 3 60.0 mm
Median rainfall:
Average rainfall: 50.0 mm Enter the rainfall (mm) for Shatin : 71
 Completion Reminder:
Median rainfall: 50.0 mm Enter the rainfall (mm) for Tuen Mun :
 After completing task 2, save 72
Enter the rainfall (mm) for Mong Kok : 0
your work in “[Link]”
Enter the rainfall (mm) for Sai Kung : 0
 Make sure the “[Link]” file Enter the rainfall (mm) for Stanley : 80
has a complete code for task 2 Total rainfall in 5 regions is : 223.0
mm
and task 1.
Average rainfall: 44.6 mm
Median rainfall: 71.0 mm
Python function recap
 A python function works in a following way:
Anything that starts with # is a comment which is not
executed
# define a function named kg_2_lb
​ Define the function
first
def kg_2_lb(weight_kg):​
weight_lb = weight_kg * 2.2
return weight_lb​

# call/ use the function​

peter_weight_lb = kg_2_lb(57) 125.4


print(peter_weight_lb)
Call the function to check the
result
 For more details, refer to the lecture slides.
Task 3 : Python Functions
 You must work in the same “[Link]” file in which you
completed task 2. Task 3 follows task 2.
 In task 3, you have to create a rainstormSignal () function.
 The rainstormSignal() function takes the median rainfall and
lookup the rainstorm signal based on the following table:

Median Rainfall Rainstorm


Signal
> 70 Black
50 < median rainfall ≤ Red
Note whether 70
"larger than" or 30 < median rainfall ≤ Amber
"less than or equal
to" should be used 50
in different median rainfall below None
situations.
30
Task 3 Follows Task 2
# Task 1: Completed
# Task 2: Completed
def rainstormSignal(rainfall):
if Put a condition for
rainfall
# is greater than 70
signal shall be 'Black'
elif
Put a condition for rainfall > 50 and
# rainfall ≤ 70
signal shall be 'Red'
elif
What other
#
conditions we'll
signal shall be ...
need too?
else :
#
return signal
signal shall be ...

Remember to add extra 4 spaces/


Tab indentation here!

# Call the rainstormSignal() function Type here to call the rainstormSignal(____)


rainstorm_signal = function

# Print the rainstorm signal


print("The rainstorm signal is : ", rainstorm _signal)
Task 3 – Check your
output
Sample Output 1 Sample Output 2
Enter the rainfall (mm) for Shatin : Enter the rainfall (mm) for Shatin : 70
90 Enter the rainfall (mm) for Tuen Mun :
Enter the rainfall (mm) for Tuen 60
Mun : 70 Enter the rainfall (mm) for Mong Kok :
Enter the rainfall (mm) for Mong 40
Kok : 50 Enter the rainfall (mm) for Sai Kung :
Enter the rainfall (mm) for Sai 75
Kung : 30 Enter the rainfall (mm) for Stanley : 0
Enter the rainfall (mm) for Stanley : Total rainfall in 5 regions is : 245.0
10 mm
Total rainfall in 5 regions is : Average rainfall: 49.0 mm
250.0 mm Median rainfall: 60.0 mm
Average rainfall: 50.0 mm ========================================
Sample Output 3
 Completion
Median rainfall: Reminder:
50.0 mm The Rainstorm Signal is : Red
Enter the rainfall (mm) for Shatin : 71
=====================================
Enter the rainfall (mm) for Tuen Mun :
===  After completing task 3, save
your work in “[Link]” 72
The Rainstorm Signal is : Amber
Enter the rainfall (mm) for Mong Kok : 0
 Make sure the “[Link]” file Enter the rainfall (mm) for Sai Kung : 0
has a complete code for task 3, Enter the rainfall (mm) for Stanley : 80
task 2 and task 1. Total rainfall in 5 regions is : 223.0
mm
Average rainfall: 44.6 mm
Median rainfall: 71.0 mm
========================================
Bonus Task 4: Dealing with
Function
 You need to create a dictionary to print the
meaning of rainstorm signal on the screen
 Be careful the data types in your code.
 You must work in the same “[Link]” file in
which you completed task 3, task 2 and task 1
Bonus Task 4: Dealing with
Dictionary
 Note: Bonus task 4 follows task 3 which follows task 2.
 In task 4, you create a dictionary to print the description
of the rainstorm signal on the screen
 The rainstorm signal meaning is indicated in the
following table:
Rainstorm Meaning
Signal
Black Very heavy rain has fallen, >70 mm/hour
Red Heavy rain has fallen, >50 mm/hour
Amber Quite heavy rain has fallen, >30 mm/hour
None No heavy rain has fallen
 For more details on dictionary, refer to the lecture slides.
Bonus Task 4 – Check your output
Sample Output 1 Sample Output 2
Enter the rainfall (mm) for Shatin : Enter the rainfall (mm) for Shatin : 70
90 Enter the rainfall (mm) for Tuen Mun :
Enter the rainfall (mm) for Tuen 60
Mun : 70 Enter the rainfall (mm) for Mong Kok :
Enter the rainfall (mm) for Mong 40
Kok : 50 Enter the rainfall (mm) for Sai Kung :
Enter the rainfall (mm) for Sai 75
Kung : 30 Enter the rainfall (mm) for Stanley : 0
Enter the rainfall (mm) for Stanley : Total rainfall in 5 regions is : 245.0
10 mm
Total rainfall in 5 regions is : Average rainfall: 49.0 mm
250.0 mm Median rainfall: 60.0 mm
Average rainfall: 50.0 mm ========================================
Median rainfall: 50.0 mm The Rainstorm
Sample OutputSignal
3 is : Red
===================================== ========================================
 Completion Reminder: Enter
Heavy the
rainrainfall (mm)>50
has fallen, formm/hour
Shatin : 71
===
Enter the rainfall (mm) for Tuen Mun :
The Rainstorm Signal is : Amber
 After completing bonus task 4, 72
=====================================
save your work in “[Link]” Enter the rainfall (mm) for Mong Kok : 0
===
Enter the rainfall (mm) for Sai Kung : 0
Quite heavy rain has fallen, >30
Enter the rainfall (mm) for Stanley : 80
mm/hour Make sure the “[Link]” file

has a complete code for bonus Total rainfall in 5 regions is : 223.0
task 4, task 3, task 2 and task mm
Average rainfall: 44.6 mm
1.
Median rainfall: 71.0 mm
========================================
The Rainstorm Signal is : Black
File submission

 After completing the Bonus Task 4, upload and submit a


SINGLE FILE “lab09” (.py Python code file) on Blackboard
 Make sure the submitted file contains complete code of Bonus
Task 4, including also the complete code of Tasks 1, 2 and 3.
 Important:
 If you submitted an incorrect file to Blackboard, you may get 0
rainfall. Make sure your functions in Tasks 1, 2 and 3 are intact
and NOT corrupted in your Task 4 attempt.

You might also like