ENGG1003
Digital Literacy and
Computational Thinking – P
Lab 09 Python Data Processing
2022-23 Term 2
Laboratory Environment
We assume you are using Python IDLE locally
installed on your computer
Virtual machine provided by MS Azure is also
okay
You are supposed to have gone through the
lecture contents
2
Activities
Defining and calling functions
Iteration: for-loop
Read & process data stored in list/tuple format
Bonus: conversion between units
Upload & submit your saved .py file on blackboard
3
Academic Honesty
You must read and observe the University Guideline on Academic
Honesty (https://www.cuhk.edu.hk/policy/academichonesty/)
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
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 work,
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:
https://www.cuhk.edu.hk/policy/academichonesty/
4
Mandatory Tasks
5
Scenario
Suppose you are now a student helper in the PHED
department, or working as an intern within the health service
sector
You need to help your senior/boss in processing some data
the team has previously collected over some experiment
participants
Good news is the data set is already tidied-up, which implies:
Each row corresponds to a record
Each column captures an attribute
The data set is 2-dimensional: only the height (in
centimeters) and weight (in kilograms) are captured
We are going to process any given data set (in such format)
via Python
6
Task 1: Calculate BMI by Function
Recall from earlier laboratories, the BMI measurement is defined as
We have done this via excel earlier; we will also do it via Python here
Important notes on the above definition:
Weight is measured in kilogram
Height is in meter scale
Function definition in python:
# define a function with input parameters
def myfunc(para1, para2):
...
...
return output
7
Task 1: Calculate BMI by Function
# define a function with input parameters
def myfunc(para1, para2):
...
...
return output
Should be noticeable that (para1, para2) are parameters
corresponding to the height and weight in our task at hand;
Naturally, output will be our target calculation of BMI
Moreover, we have defined for you the name of myfunc as
calBMI, DON’T CHANGE IT!!!!!!
8
Task 1: Calculate BMI by Function
Fill in this
blank!
The sample code should look like the above
You may attempt this task by modifying the given line(s)
Notice the variable bmi should be overwritten upon finishing the task
Pay attention to unit conversion!
9
Task 1: Expected Output
LHS is the expected output of the correctly crafted python function
We can validate the result by using excel spreadsheet, viz. RHS
Good that you guys (generally) performed well in previous Excel’s
labs!
Keep up the good work :-)
10
Task 2: Body Check
Body Status BMI
Underweight below 18.5
Normal at least 18.5 BUT below 25.0
Overweight at least 25.0 BUT below 30.0
Obesity 30.0 or above
According to the WHO body status can be classified by
one’s BMI
The classification follows the above table
Our task: build a function bodyCheck such that once
height and weight are input, it calculates one’s BMI by
calBMI and prints the corresponding body status
11
Task 2: Sample Code
Fill in these
blanks!!
12
Task 2: Expected Output
13
Task 3: Automation of Data Processing
heigh bodyCheck bmi = 20.7
weight
t
1 163 55 bodyCheck bmi = 28.3
2 155 68
bodyCheck bmi = 39.7
3 153 93
bodyCheck bmi = 13.4
4 181 44
Notice that we have been performing the task over few records only
Each time we have to manually input the data for each record in the console
This is fine for 4 data records but certainly not for 100,000,000 records!
14
Task 3: Automation of Data Processing
heigh bmi = 20.7
weight
t
1 163 55 bmi = 28.3
2 155 68 process()
bmi = 39.7
3 153 93
4 181 44 bmi = 13.4
avg = 25.54
Now we proceed to process the whole data set automatically
We set out to do so by iteration
We will also be able to capture the average BMI as a byproduct
15
Task 3: Automation of Data Processing
We will assume all input data are tidy up as a List of
Tuples
Suppose there are m data records
The first data record is stored as (h1, w1)If you forgot what List and
Tuple are and how to use
The second is (h2, w2), and so on…
them…
The m-th data record will be (hm, wm)
Check your lecture slides!
The student data set shall admit the form:
[(h1, w1), (h2, w2), ..., (hm, wm)]
Notice that each record is separated by a comma; and
together they are collected by a pair of square brackets
16
List of Tuples: Example
heigh
weight
t
1 163 55
2 155 68
3 153 93
4 181 44
To give you a numerical example, consider the data set described above
We can input the data like the RHS picture
When we type X[i] it gives us the i-th tuple where i starts from 0
Each X[i] is a tuple of the form (h, w)
Consult the lecture notes on how to use for-loop to iterate through list X
17
Task 3: Automation of Data Processing
Fill in these
blanks
with a for-loop!!
Let me give you some hints:
You should call the function bodyCheck(..) in every iteration
avg_bmi can be calculated using the accumulator total
You may use the function len(..) to calculate the length of list
18
Sample Output
BMI of a person with height=168 cm, weight=74 kg is: 26.218820861678008
A weight of 168 cm and 74 kg...
Your BMI is estimated as 26.218820861678008
Body Status: Overweight
Body check result: 26.218820861678008
Student Data: [(168, 74), (183, 61), (162, 65), (166, 46)]
A weight of 168 cm and 74 kg...
Your BMI is estimated as 26.218820861678008
Body Status: Overweight We will test and check your
A weight of 183 cm and 61 kg... functions with other data
Your BMI is estimated as 18.21493624772313 sets!
Body Status: Underweight
A weight of 162 cm and 65 kg...
Your BMI is estimated as 24.76756591982929
Body Status: Normal
A weight of 166 cm and 46 kg...
Your BMI is estimated as 16.693279140659023
Body Status: Underweight
The average BMI of this batch of data is 21.473650542472363
19
Bonus Task
20
Scenario
Alongside centimeters, people also express unit of length in inches
This bonus part exploits the conversion between inches and
centimeters:
1 inches = 2.54 centimeters
Task: write a Python function to:
Receive a string as input
Detect all length measurements in the unit inches
Convert each length to centimeters scale
Return the converted output as a list of split strings
Example:
Sample input: “The model is 10 inches tall”
Expected output: “The model is 25.4 centimeters tall”
(by given code that joins the returned list of split strings)
21
Task
Goal: know how to modify selective parts of a python list
Assume: all numbers are followed by the unit "inches"
Possible approach:
Split the string into individual words
Inspect all words in the string, look for some numeric
If you find a number, that must be followed by “inches”
Replace the numeric value N with a new value using conversion N*2.54
Replace the following word “inches” with “centimeters”
You may consider using these functions: split(), isnumeric(),
int(), using a for-loop with indices, etc.
22
Hints and Tips
You might have used split() to obtain a list of words from the input
string
Numbers from the output list are string type values also, you will need
to convert the number from a string to a suitable type, before
calculation
Ending Remark
Words in text processing are also known as tokens
Spaces are common separators between tokens
23
Blackboard Submission
Login Blackboard course ENGG1003
Go to Lab 09 Python Data Processing online
submission form
Under [Attach Files], Browse and select your saved
files lab09.py and lab09bonus.py from your newly
created folder
Do NOT Write Submission or type in Comments box
Click [Submit] button