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

ENGG1003_Lab08_PythonBasics (1)

Lab 08 of the ENGG1003 course focuses on Python basics, including setting up Python, saving work, and practicing with variables, user input, and conditional statements. Students will solve three problems involving triangle formation, hurricane classification, and growth order calculations, and submit their work via Blackboard. The lab also includes an optional fun exercise using the turtle graphics library.

Uploaded by

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

ENGG1003_Lab08_PythonBasics (1)

Lab 08 of the ENGG1003 course focuses on Python basics, including setting up Python, saving work, and practicing with variables, user input, and conditional statements. Students will solve three problems involving triangle formation, hurricane classification, and growth order calculations, and submit their work via Blackboard. The lab also includes an optional fun exercise using the turtle graphics library.

Uploaded by

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

ENGG1003

Digital Literacy and


Computational Thinking - P

Lab 08 Python Basics


2022-23 Term 2
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 08 Activities

 Practice using variables and expressions


interactively

 Use of input(), int() and float() commands

 Solving two problems using if: elif: else:

 Last problem, print number sequences using for:

 Upload and submit your saved files to Blackboard

 Extras
Variables and Expressions
temperature = 24.6
humidity = 0.75 Key-in OR copy-and-
print(temperature > 24) paste
print(humidity > 0.85) one line at a time!
 Practice on print(temperature >= 24 or humidity < 0.7)
print(22 < temperature < 28)
IDLE (Python)
approx_dew_point = temperature - (100 - humidity * 100) /
print('Dew point =', approx_dew_point)
Try different test
User Input cases.
What do you see?
 File  New File, enter the following code:
Name? Mike
Year? 2
name = input("Name? ")
GPA? 2.99
year = int( input("Year? ") )
gpa = float( input("GPA? ") )
print("Year", year, "friend", name, "!") Name? Ann
if gpa >= 3.0: Year? 4
print("Marvel!")
GPA? 3.78
elif gpa > 2.2:
print("Keep up!") Name? Pete
else: Year? 1
print("Work hard!") GPA? 2.1

 Run  Run Module [F5]

after Save As… friend.py in your working folder


BEWARE:
Assignment with =
Comparison with ==
Solve Problem One: Don’t miss the colon in
3 Lengths Form a Triangle?conditionals and indent
with 4 spaces!

 Create a New File triangle.py


Triangle Inequality
 Write some code to Theorem: The sum of the
 ask the user for 3 integers (int), a, b and c lengths of any two sides of
a triangle is greater than
 check if they can form a triangle (using the the length of the third
theorem provided) side.

 b a+b>c
print the message ”Formed a triangle!" or a
a+c>b
"Not a triangle!" b+c>a
 Run, test and verify your work thoroughly c
and carefully (think of some more test
cases):
a? 2 a? 2 a? 3
b? 3 b? 5 b? 6
c? 4 c? 10 c? -5
Formed a triangle! Not a triangle! Not a triangle!
Category Wind Speed (mph)

Solve Problem Two: 1


2
>= 74 and < 96
>= 96 and < 111
Hurricane 3 >= 111 and < 131

 4 >= 131 and < 155


Create a New File hurricane.py
5 155 and above
 Write some code to
 ask the user for a wind speed value (float), w
 check and print whether it qualifies as a hurricane, and
if so, whether it is a Category 1, 2, 3, 4, or 5 hurricane
using the provided table of wind speeds (based on the
Saffir-Simpson scale).
 Run, test and verify your work thoroughly and
carefully:
Wind speed (mph): 73.5 Wind speed (mph): 111.1
Not a hurricane Class 3 hurricane

Wind speed (mph): 74 Wind speed (mph): 200


Class 1 hurricane Class 5 hurricane

Wind speed (mph): 96.0 Wind speed (mph): -1


Class 2 hurricane Not a hurricane
This exercise lets us visualize
how fast some simple math
Solve Problem Three: functions (like two times,
square and cube of a number)

Order of Growth grow with the number.

Revision: how to calculate


 Create a New File growth.py powers of a number in
 Python?
Write some code to
 ask the user for ONE integer (int), n
 print a table of the values of i, 2i, i2, i3 for i = 1, 2, 3, ...,
n using the for i in range(…): construct
 Use tabs ('\t' characters) to line up columns
 Run, test and verify your work thoroughly and
carefully.
 See sample results on the next page.
Assume user is cooperative that n is
always a positive integer less than
or equal to 30.
Hint: A print() function call can print
Solve Problem Three: multiple things with a space between them
by default, e.g.,
Order of Growth print(1, 2, 3) prints
 1 2 3
Sample program outputs:
n? 25 You can set the sep parameter to change
1 2 1 1
2 4 4 8 the separator between the arguments to
3 6 9 27 something else of your choice, e.g., a tab.
4 8 16 64
5 10 25 125
6 12 36 216 print(1, 2, 3, sep='\t') prints
7
8
14
16
49
64
343
512
1 2 3
9 18 81 729
10 20 100 1000
11 22 121 1331
12 24 144 1728 n? 10
13 26 169 2197 1 2 1 1
14 28 196 2744
2 4 4 8
15 30 225 3375
16 32 256 4096 3 6 9 27
17 34 289 4913 4 8 16 64
18 36 324 5832 5 10 25 125
19 38 361 6859
20 40 400 8000
6 12 36 216
21 42 441 9261 7 14 49 343
22 44 484 10648 8 16 64 512
23 46 529 12167 9 18 81 729
24 48 576 13824
25 50 625 15625 10 20 100 1000
Blackboard Submission

 Login Blackboard course ENGG1003


 Go to Lab 08 Python Basics online submission form
 Under [Attach Files], Browse and select your saved
files triangle.py, hurricane.py and growth.py from
your newly created folder
 Do NOT Write Submission or type in Comments
box
 Click [Submit] button
Extras for Fun (no Bonus:)
for: Spiral
 Download and Run the given file spiral_bw.py

import turtle as pet

pet.speed(0)

for x in range(1, 181):

if x % 2 == 0:
pet.pencolor('black')
else:
pet.pencolor('blue')

pet.forward(x)
pet.left(59) % is the remainder operator.
pet.pencolor('red')
x % 2 == 0 is an even-odd integer test.
pet.forward(270)
pet.exitonclick()

You might also like