COS10022 IDS Tutorial Week02 R
COS10022 IDS Tutorial Week02 R
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
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
In Python, you don’t need to declare the type of variable before using
it.
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.
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.
15
If-Else
◦ Expression:
◦ if criterion1 relation criterion2:
descriptions1
else:
descriptions2
◦ Example:
16
Brainstorm Time!
Assume you have 10 dollars on your
hand.
18
Brainstorm Time!
19
Brainstorm Time!
20
◦ Example:
If-Elif-Else
◦ Expression:
◦ if criterion1 relation criterion2:
descriptions1
elif criterion3 relation criterion4 :
descriptions2
else:
descriptions3
21
Functions
Using functions can also help you to make your code more tidy.
22
Function
◦ def function_name(input_vars):
descriptions_in_the_function
◦ 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.