COS10022 – INTRODUCTION TO DATA SCIENCE
Week 1
Lab 01
Topic:
• Basic Python
Programming
Tool:
• Python 3 with
Jupyter
Notebook
3
BASIC OF PYTHON PROGRAMMING
4
Useful Online Resource
◦ CS Dojo
◦ https://www.youtube.com/watch?v=Z1Yd7upQsXY&list=PLBZBJbE_rG
RWeh5mIBhD-hhDwSEDxogDg
◦ CS Dojo YouTube channel has a series of Python programming course
for absolute beginners.
◦ The key points of Python is introduced in the series in detail.
5
Launching the Jupyter Notebook IDE
Jupyter Notebook Server
+R Jupyter Notebook Host (Browser)
6
Start a New Jupyter Notebook File
◦ Click on “New” → “Python 3” ◦ Click on “Untitled” to rename the file.
7
Write Comments in Your Code
◦ Try the comment symbol to leave comments in your code.
◦ The comment is commonly used in the programs for helping programmers to remember what is
going on in the code.
◦ A more import fact is that the comments help other programmers in the team to understand your
code to boost up the efficiency of cooperation and team works.
◦ Tip: Try to write a sentence with the symbol # as the start and press “Shift” + “Enter” to
execute the codes in the cell.
8
Displaying the Result from the Code
◦ The basic function to use is print()
◦ It prints whatever in a given contain between the brackets.
◦ Practice
◦ Try to print a number 5.
◦ Practice
◦ Try to print the sentence: I’m learning Python programming.
9
Variable
A variable is a container, which can be used to temporary store data.
In Python, you don’t need to declare the type of variable before using
it.
A variable can be used to represent a single value, a vector, or a
dictionary (array).
The value inside a variable can be changed anytime.
10
Set Variables and Print the Values
◦ Let’s create 2 variables called a ◦ We can reassign value to any of the
and b and assign values 1 and 6 variable.
to them, accordingly. ◦ Let’s try to assign the value stored in b to a
and change b to store a string “It’s new!”
◦ Try to print a and b independently
◦ Print the final results of a and b.
and then try to print them in the
same line.
11
Brainstorm Time!
Assume we have two variables, a
and b, holding two strings
“Alpha” and “Beta,” respectively.
What are you going to do if we
want to make a swap to let
variable a hold “Beta” and
variable b hold “Alpha?”
12
Brainstorm Time!
13
Mathematic Operators
◦ Relationships:
◦ > Greater
◦ < Smaller
◦ == Equal to
◦ >= Greater and equal to
◦ <= Smaller and equal to
◦ != Not equal to
14
Flow Control: If-Else
In many situations, you’ll need to control your program to do one
thing if a certain criterion appears, otherwise, your program needs
to achieve another operation when that criterion doesn’t exist.
If-Else is a flow control description to help you change to execution
structure of your code.
You need to “indent” the description under the criterion description
to let Python know that this part of the code belongs to the criterion
corresponding situation.
15
If-Else
◦ Expression:
◦ if criterion1 relation criterion2:
descriptions1
else:
descriptions2
◦ Example:
16
Brainstorm Time!
Assume you have 10 dollars on your
hand.
A set of hamburger causes 12 dollars.
Write a program using If-Else to check
whether you can afford to have a
hamburger set.
17
Brainstorm Time!
18
Brainstorm Time!
Assume you have 10 dollars on your
hand.
A set of hamburger causes 12 dollars
while a set of hotdog causes 8 dollars.
Write a program using If-Else to check
what meal you can get.
19
Brainstorm Time!
20
◦ Example:
If-Elif-Else
◦ Expression:
◦ if criterion1 relation criterion2:
descriptions1
elif criterion3 relation criterion4 :
descriptions2
else:
descriptions3
21
Functions
Function can be treated as a collection of instructions.
Function can also be treated as a collection of codes.
The purpose of using function is to put the repeatable part of codes in an
independent place and reuse in the future without rewrite them again.
Using functions can also help you to make your code more tidy.
22
Function
◦ def function_name(input_vars):
descriptions_in_the_function
◦ To use the function, simply call the function in the code.
◦ Example:
23
Function
◦ Passing a variable into the function and use it.
◦ Example:
24
Return Value from a Function
◦ def function_name(input_vars): ◦ To use the function, simply call the
descriptions_in_the_function function in the code and assign the
return a_variable function to a variable.
25
Passing and Returning Multiple Values
from a Function
26
Exercise 1 –
27
Choosing Available
Rooms
◦ Assume we have three class rooms
called RoomA, Room B, and RoomC
with capacities of 20, 50, and 120,
respectively.
◦ Only 1 room can be used to contain all
students.
◦ The student enrolment number is 48.
◦ Use If-Else and/or If-Elif-Else
structure to recommend the available
classroom.
◦ Use a function to handle the output of
available recommendations.