Professional Documents
Culture Documents
Email: jkwok@ust.hk
Course Plan – Part 5b
Python – Python - Python – Python –
Introduction Python - App
Environment Basics Data Control Flow
Data Types,
Data
Python Colab Comment Type Looping
Analytics
Conversion
print() String
input() Objects
Operators
2
Program1_pt5b.ipynb
Enter km/h: 70
Speed: 70 KM/H = 43.498 MPH
3
Variable and
input()
print()
Type Conversion
Programming
and Other Built-
Objects
in Functions
4
Variables and
print()
5
Variables and Values (1)
• A name that is used to denote something or a value is called a
variable.
• In Python, variables can be declared and values can be assigned to
them as follows,
Question:
[1] # Add two numbers
Is num1 the same as Num1?
num1 = 15
num2 = 12
Task:
Use print() to show the
num1 and num2 are variables values of num1 and num2
And their values are 15 and 12 respectively.
6
Variables and Values (2)
Question:
How many variables are there?
What are they?
[2] x = 2
y = 5
xy = 'Hey'
print(x+y, xy)
# the first part is a formula,
# while the second part is just the value of the variable
7
Variables and Values (3)
[3] x = y = 1 # x and y have the same value
print(x,y) # comma is to separate the two values
1 1
[4] a=10
b=23.5
a,b=b,a # this can swap values
print(a, b)
23.5 10
A space is in between.
8
print()
• The print() function is a Python function, which can print the specified
message to the screen. For example:
print("Hello World")
print("Hello", "how are you?")
9
print() – " " and ' '
[5] print("abc'123'def")
abc'123'def
[6] print('abc"123"def')
abc"123"def
10
input()
11
input()
• input(prompt), prompts for and returns input as a string.
12
Data Types and
type()
13
Common Data Types
• The basic types build into Python include
• float (floating point numbers),
• int (integers),
• str (Unicode character strings) and
• bool (boolean).
• Some examples are given below:
15
type()
type() function can be used for determining the type of an
object/variable
Question:
[8] var_float = 1.34 Why only ONE bool?
var_int = 345
var_str = "testing"
var_bool = True
bool
16
Operators
17
Arithmetic Operators
and
Assignment Operators
18
Operators – Arithmetic Operators (1)
Symbol Task Performed
+ Addition
- Subtraction
/ division
% mod
* multiplication
// floor division
** to the power of
19
Operators – Arithmetic Operators (2)
[1] 1+2
[2] 2-1
[3] 1*2
0.75
20
Operators – Arithmetic Operators (3)
[5] 3//4 # round down
0.0
[6] 15%10
[7] 11**300
261701099618839990701703252897203834249164941695300026024080595582797205668538243449709
034149678703258573888478674528670047399984728066419173100887481175131088859178611199467
820892017514391176118142449566087795065414506696903625266973548309893688401647132648740
3792787648506879212630637101259246005701084327338001
21
Assignment Operators (1)
22
Type Conversion
and
Other Built-in Functions
23
Type Conversion and Other Built-in Functions (1)
Implicit Data Type
Meaning
Conversion
int() converts a number to an integer
float() converts a number to a floating point value
str() converts a value to a string data
Other Arithmetic
Meaning
Operations
rounds the input value to a specified number of
round()
places or to the nearest integer
abs() outputs the absolute value of an input value
24
Type Conversion and Other Built-in Functions (2)
[1] print(int(7.7), int('7'))
7 7
[2] print(round(float('34.1251'),2))
34.13
[3] print(str(True),str(1.2345678),str(-2))
True 1.2345678 -2
25
26
Exercise
Exercise 1
• Write a Python program to convert HKD to CNY.
27
Exercise 2 - Corporate Social Responsibility (CSR)
• Write a Python program to implement a Carbon Calculator.
• Only implement “By annual electricity consumption”
28
https://www.hkelectric.com/en/customer-services/carbon-calculator
Course Plan – Part 6
Python – Python - Python – Python –
Introduction Python - App
Environment Basics Data Control Flow
Data Types,
Data
Python Colab Comment Type Looping
Analytics
Conversion
print() String
input() Objects
Operators
29
Program1_pt6.ipynb
Program 1
1 kilometer = 0.621371192 miles
• Write a Python program to convert
KM/H to MPH, which may be handy
for calculating speed limits when [1] # Program 1: Converting KM/H to MPH
driving aboard, especially for HK and
kmh = int(input("Enter km/h: "))
US drivers. mph = 0.6214 * kmh
print("Speed:", kmh, "KM/H = ", mph, "MPH")
• Read input data from users
• Show the output Enter km/h: 70
Speed: 70 KM/H = 43.498 MPH
30
Program 1 – Expected formatted Output
Expected
output
31
String
Operator
Objectives
Print()
f-string
32
String Operator
33
String Operator (1)
String concatenation is the "addition" of two strings. Observe that
while concatenating there will be no space between the strings.
HelloWorld!
34
String Operator (2)
Strings can be concatenated (glued together) with the + operator, and
repeated with * operator
unununion
35
Practice
• Debug the following program so that it can produce the given output.
[1] price_water = 5
price_burger = 22.5
36
print() – f-string
37
print() – f-string (1)
There are lots of methods for formatting and manipulating strings built into
python. Some of these are illustrated here.
String concatenation is the "addition" of two strings. Observe that while
concatenating there will be no space between the strings.
[1] # version 2a
# objective: f-string
Enter km/h: 70
Result: 70 kilometer/hour = 43.498 mile/hour
38
print() – f-string (2)
[1] # version 2b
# objective: f-string
Enter km/h: 70
Result: 70 kilometer/hour = 43.498 mile/hour
39
Programming
Objects
40
Objects David is the name of
an object in the same
Programming Objects
James is
program. Do you think Examples Examples an object
David can swim() too? (Concept) (Actual Code)
Walk
Sleep Movements/ Obj.walk() James.walk()
Functions Obj.sleep() James.sleep(8, "hr")
Swim Actions
Obj.swim() James.swim("freeStyle")
Examples
(Actual Code)
Movements/ Spaceship_1.moveup(10)
Move up Functions
Actions Finished = Spaceship_1.movedown(25)
Move down
Object.function()
43
Key takeaways
• Object
• Method
• Property
• Object.Method()
• Object.Property
• Usage
object.method()
object.property = "ISOM"
# "ISOM" is a value, aka String data
45
Exercise
Exercise 3
• Write a Python program to convert HKD to CNY.
46
End