You are on page 1of 47

SBMT5730

MBA Accelerator – Python

Prof. James Kwok


Associate Professor of Business Education
Certified Information Systems Auditor (CISA)

Email: jkwok@ust.hk
Course Plan – Part 5b
Python – Python - Python – Python –
Introduction Python - App
Environment Basics Data Control Flow

Anaconda, Basic Syntax Packages and


Programming Variables if-then-else
VS Code help() Libraries

Data Types,
Data
Python Colab Comment Type Looping
Analytics
Conversion

print() String

input() Objects

Operators

2
Program1_pt5b.ipynb

Program 1: Speed Convertor

[1] # Program 1: Converting KM/H to MPH

kmh = int(input("Enter km/h: "))


mph = 0.6214 * kmh
print("Speed:", kmh, "KM/H = ", mph, "MPH")

Enter km/h: 70
Speed: 70 KM/H = 43.498 MPH

3
Variable and
input()
print()

Objectives Data Types and


type()
Operators

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

The comma is to separate the


two values

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.

[7] name = input("What is your name: ")


print(name)

What is your name: James

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:

float 2.3 1e100


int -1234 1234
str 'This is a string' "It's another string"
bool True False
14
Data Types
• Python has many native datatypes. Here are some important ones.
• Numbers can be integers (1 and 2), floats (1.1 and 1.2), fractions (1/2 and 2/3), or
even complex numbers.
• Strings are sequences of Unicode characters, e.g. an html document.
• Booleans are either True or False.
• Lists are ordered sequences of values.
• Tuples are ordered, immutable sequences of values. To be covered in
• Sets are unordered bags of values. another course
• Dictionaries are unordered bags of key-value pairs.
• There are other types in Python but will likely not cover them in this
course.
(Note: Everything, including function is an object in Python.)

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

type(var_float) # show float Task:


type(var_int) # show int Revise the program so that it
type(var_str) # show str
type(var_bool) # show bool can show all FOUR values.

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

[4] 3/4 # the result could be different in Python 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)

Symbol Example Same As


= x=5 x=5
+= x += 3 x=x+3
-= x -= 3 x=x-3
*= x *= 3 x=x*3
/= x /= 3 x=x/3
%= x %= 3 x=x%3
//= x //= 3 x = x // 3

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

Anaconda, Basic Syntax Packages and


Programming Variables if-then-else
VS Code help() Libraries

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.

[1] # + is a String operator


string2='World'
string3='!'
print('Hello' + string2 + string3)

HelloWorld!

34
String Operator (2)
Strings can be concatenated (glued together) with the + operator, and
repeated with * operator

[1] # 3 times 'un', followed by 'ion'


str = 3 * 'un' + 'ion'
print(str)

unununion

35
Practice
• Debug the following program so that it can produce the given output.
[1] price_water = 5
price_burger = 22.5

total = price_water + price_burger

print("The total is: " + "$" + total)

The expected output:

The total is: $27.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

kmh = int(input("Enter km/h: "))


mph = 0.6214 * kmh
print(f"Result: {kmh} kilometer/hour = {mph} mile/hour")

Enter km/h: 70
Result: 70 kilometer/hour = 43.498 mile/hour

38
print() – f-string (2)

[1] # version 2b
# objective: f-string

kmh = int(input("Enter km/h: "))


mph = 0.6214 * kmh
print(f"Result: {kmh} kilometer/hour = {mph:.3f} mile/hour")

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)

Name Obj.Name James.Name = "James"


Age Properties/ Obj.Age James.Age = 20
DOB Properties DOB = James.DOB
Attributes Obj.DOB
Email Obj.Email Address = James.Email

Walk
Sleep Movements/ Obj.walk() James.walk()
Functions Obj.sleep() James.sleep(8, "hr")
Swim Actions
Obj.swim() James.swim("freeStyle")

"freeStyle" is an argument value


41
Objects Programming Objects

Examples
(Actual Code)

Name Properties/ Spaceship_1.Name = "Mars 1234"


Color Attributes/ Properties Spaceship_1.Color = "red"
Type Characteristics ss_type = Spaceship_1.Type

Movements/ Spaceship_1.moveup(10)
Move up Functions
Actions Finished = Spaceship_1.movedown(25)
Move down

Can Spaceship_1 Assume ismoving does exist. Is


swim()? ismoving a property or function?
42
Python – Programming Example
Object
function() to create an object

Object.function()

Object or Function or Property ?

Object or Function or Property ?

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

You might also like