0% found this document useful (0 votes)
16 views27 pages

ENGG1003_Lab08_PythonBasics_2425T1

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)
16 views27 pages

ENGG1003_Lab08_PythonBasics_2425T1

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
You are on page 1/ 27

ENGG1003

Digital Literacy and


Computational Thinking - P

Lab 08 Python Basics


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 08 Activities

 Practice using variables and expressions interactively

 Use of basic python commands


 input(), data types such as int() and float()
commands

 Solving problems using if: elif: else: with Bonus

 Upload and submit your saved files to Blackboard


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)
Execute a batch of codes
 Feel troublesome?
 If you have a batch of codes to be executed (like the
codes in the previous slide, it is good to create a
python (program) file by Python IDLE.
 Start up the Python IDLE, you will see the Python
Shell environment.
This is Python Shell
environment. We can
enter and execute
code one-by-one only.
Execute a batch of codes
 Click on “File” from menu ribbon, then click “New
File”, a new window with the name “untitled” will
be prompted.
 This is the Python editor. We can type in a batch
of codes here and execute them together. It will
help to organize the execution of codes.

This is Python editor.


We can enter and
execute a series of
codes together.
Execute a batch
of codes
 Try to type in the example codes to the
editor.
 Click Run  Run Module, or click [F5] key.
 Python editor request to save the file (.py)
before execution. Let’s save the file as
“Lab08Example.py” [F5] : Execute the
python
 The Python IDLE Shell will show the (Lab08Example.py) file
output(s) of the corresponding python
program file “Lab08Example.py” in IDLE
Shell.
 This is convenience to make corrections
that if the codes contain any mistakes.
 Enjoy your programming~!
TASK 1
Task 1 - Setup

 Download the Python code template,


Lab08_2425T1_template.py from Blackboard

 Save the file as Lab08.py in your folder or


workspace

 To open the Lab08.py:


 Launch IDLE Python, File  Open  Browse to your
workspace, and pick Lab08.py  Open.

 Read the comments in the file, and modify the code


to calculate the angles of a triangle
Lab08_template.py
All the lines starting with # are
comments

In between the comments, you can


remove the … and add your Python
codes

To run the codes, click Run  Run


Module [F5]
Task 1 - Procedures

 Ask the user to input two integers as two of the three


angles of a triangle
 Using int() and input() functions, e.g.
 x = int(input(“x? ”))

 Calculate the third angle


 Using mathematical expressions, e.g.
 x = y + z

 Output all the angles properly as shown in the sample


outputs on the next page
 Using print() function, e.g.
 print(“Text outputs.”)
Task 1 - Sample Outputs

Sample Output 1

Task 1 - Input and output of the angles of a triangle


What is the angle a? 30
What is the angle b? 60
a is 30
b is 60
c is 90
Task 2
Task 2 - Classification of
Triangle by Angle
 By the sizes of the angles of
a triangle, three types of
triangles can be determined
as shown in the figure
 Modify the codes for Task 2
using if…elif…else
statements with the
inputted angles to
determine whether the
triangle is an Acute, Right,
or Obtuse triangle
 Output the classification as
shown in the sample
outputs on the next page

 Image source:
https://www.cuemath.com/geometry/types-of-triangle/
Task 2 – Example Codes

 Example of if…elif…else statement

if x == 0 and y > 100:


print(“Case 1”)
elif x != 100 and y <= 100:
print(“Case 2”)
else:
print(“Others”)
Task 2 – Sample Outputs

Sample Output 2.1 Sample Output 2.2

Task 1 - Input and output of the Task 1 - Input and output of the
angles of a triangle angles of a triangle

What is the angle a? 50 What is the angle a? 120

What is the angle b? 55 What is the angle b? 20

a is 50 a is 120

b is 55 b is 20

c is 75 c is 40

Task 2 - Acute, Right, or Obtuse Task 2 - Acute, Right, or Obtuse


triangle triangle

It is an Acute triangle. It is an Obtuse triangle.


Task 3
Task 3 - Classification of
Triangle by Side
 By the length of the sides of
a triangle, three types of
triangles can be determined
as shown in the figure
 Modify the codes for Task 3
using if…elif…else
statements with the
inputted angles to
determine whether the
triangle is an Equilateral,
Isoceles, or Scalene triangle
 Output the classification as
shown in the sample
outputs on the next page

 Image source:
https://www.cuemath.com/geometry/types-of-triangle/
Task 3 – Sample Outputs

Sample Output 3.1 Sample Output 3.2

Task 1 - Input and output of the angles Task 1 - Input and output of the angles
of a triangle of a triangle

What is the angle a? 60 What is the angle a? 30

What is the angle b? 60 What is the angle b? 30

a is 60 a is 30

b is 60 b is 30

c is 60 c is 120

Task 2 - Acute, Right, or Obtuse Task 2 - Acute, Right, or Obtuse


triangle triangle

It is an Acute triangle. It is an Obtuse triangle.

Task 3 - Equilateral, Isoceles, or Task 3 - Equilateral, Isoceles, or


Scalene triangle Scalene triangle

It is an Equilateral triangle. It is an Isoceles triangle.


Task 4
Bonus Task 4 – Pythagoras
Theorem
 Based on the result of
Tasks 1 to 3, we can
determine whether the z
triangle is a Right x
Triangle

y
 In Task 4, check if it is
a Right Triangle.

 If it is not a Right
Triangle, alert the user
and stop the
execution.
Bonus Task 4 – Pythagoras
Theorem
 If it is a Right triangle,
 a. input the length of the
shorter arm of the right
angle (e.g. x),
z
x
 b. determine the length of
another arm of the right
angle (e.g. y) by
trigonometric function with y
math module, and
 c. determine the length of
the hypotenuse of a right
triangle (e.g. z) by the
Pythagoras Theorem
 Output all sides line-by-line
as shown in the sample
outputs
Task 4 - Notes

 Module math has already imported at the


beginning of the program. You can use its
functions to accomplish the task, such as:
 math.tan()
 math.radians()

 No extra library or module (e.g. numpy) should be


imported to facilitate the calculations

 x, y, and z are floating point numbers. So, float()


should be used instead of int()
Task 4 – Sample Outputs

Sample Output 4.1 Sample Output 4.2

Task 1 - Input and output of the Task 1 - Input and output of the
angles of a triangle angles of a triangle
What is the angle a? 45 What is the angle a? 30
What is the angle b? 45 What is the angle b? 60
a is 45 a is 30
b is 45 b is 60
c is 90 c is 90

Task 2 - Acute, Right, or Obtuse Task 2 - Acute, Right, or Obtuse


triangle triangle
It is a Right triangle. It is a Right triangle.

Task 3 - Equilateral, Isoceles, or Task 3 - Equilateral, Isoceles, or


Scalene triangle Scalene triangle
It is an Isoceles triangle. It is a Scalene triangle.

Task 4 - Use of Pythagoras Theorem Task 4 - Use of Pythagoras Theorem


What is the length of the shortest What is the length of the shortest
side? 1 side? 5
x is 1.0 x is 5.0
y is 1.0000000000000002 y is 8.660254037844387
z is 1.4142135623730951 z is 10.0
Task 4 – Sample Outputs

Sample Output 4.3 Sample Output 4.4

Task 1 - Input and output of the Task 1 - Input and output of the
angles of a triangle angles of a triangle
What is the angle a? 90 What is the angle a? 20
What is the angle b? 60 What is the angle b? 20
a is 90 a is 20
b is 60 b is 20
c is 30 c is 140

Task 2 - Acute, Right, or Obtuse Task 2 - Acute, Right, or Obtuse


triangle triangle
It is a Right triangle. It is an Obtuse triangle.

Task 3 - Equilateral, Isoceles, or Task 3 - Equilateral, Isoceles, or


Scalene triangle Scalene triangle
It is a Scalene triangle. It is an Isoceles triangle.

Task 4 - Use of Pythagoras Theorem Task 4 - Use of Pythagoras Theorem


What is the length of the shortest It is not a Right triangle.
side? 5
x is 5.0
y is 8.660254037844387
z is 10.0
Submission

 Once you complete the tasks, upload Lab08.py file


to Blackboard

 Make sure the submitted file Lab08.py contains


your complete work, NOT the template file
Lab08_2425T1_template.py.

You might also like