Lecture2 Basic of Python
Lecture2 Basic of Python
• In order to run the program, it must be converted into a suitable format for the computer,
which is referred to as machine code.
Second Law
A robot must obey the orders given it by human beings except where such
orders would conflict with the First Law.
Third Law
A robot must protect its own existence as long as such protection does not
conflict with the First or Second Law.
Driverless cars: Who should die in a crash?
Ethical
issue
Importance of programming
• There are many problems
that can arise as not only simple labor but also AI and robots replace many activities.
• The programming to be learned in this course aims to use computing resources efficiently
based on accuracy and speed,
but ethical issues will also have to be considered in the future.
So, I hope this programming course will impress you to live digital era.
Analog vs Digital
Representation of data
Digital devices like computers store
information as binary numbers, i.e.
a sequence of 0s and 1s. Binary is
equivalent to base 2.
• petabyte = PB
Binary vs ASCII
Computers work in binary code. InformaIon is coded using 0s and 1s. Each 0 or 1 is
called a bit. In the early years of computer development, different computer companies
applied the binary system in their own way. The code for the leNers in the word “cat”
was oQen different in different brands of computers.
Eventually, a set of standards was developed. Computer manufacturers agreed to use
one code called the ASCII (American Standard Code for InformaIon Interchange). ASCII
is an 8-bit code. That is, it uses eight bits to represent a leNer or a punctuaIon mark.
Eight bits are called a byte. A binary code with eight digits, such as 1101 10112, can be
stored in one byte of computer memory.
ASCII table
Python
https://youtu.be/Y8Tko2YC5hA
Installing Python on CSC computers
https://www.cityu.edu.hk/csc/deptweb/facilities/terminal-area/studentlan/StudAppWin8.asp
The CSC provides service for putting software on Work Desk menu for accessing in Teaching Studios. The software wil
l be installed on demand when selecting it from the menu. The software still need to go through the installation proc
ess and will be removed after rebooting the computer, but the installation step is simpler and easier for repeated inst
allations, and no need to download or bring the disc/media every time.
hJps://www.anaconda.com/download
P2_Yourname
P2_Yourname
Please change the file name (title) to P2_Yourname (you should submit tomorrow)
It should be 0.5, but the computer said 0.
(This is old version of Python. Python2)
>>> type(123.0)
<class 'float’>
>>> type('123')
<class 'str’>
>>> type(True)
<class 'bool’>
Basics
>>> a=1
>>> x=3.14
>>> circle=x*a**2 Area of circle = π ✕ r2
>>> circle
3.14
>>> a=2
>>> circle=x*a**2
>>> circle
12.56
function
A function is analogous to the formula function of a calculator or a mathematical function.
x → f(x)
x,y → g(x,y)
Repeating these instructions explicitly
makes the code longer than necessary.
The objective of the function (code reuse) is
to avoid repeating instructions again and
again.
function
>>> def circle(x):
... area=x*x*3.141592
... return area
...
>>> print(circle(2))
12.566368
>>> print(circle(5))
78.5398
>>> d,e,f='CityU',1,3.141592
>>> d
'CityU'
>>> e
1
>>> f
3.141592
Basics: mathematical operators
>>> 2**3
8
>>> 10%3
1
>>> 1.0e3
1000.0
>>> abs(-1)
1
Basics: string operators
>>> str1='SEE' >>> str3 = str1+str2
>>> str2='CityU' >>> str3
>>> str1+str2 'SEECityU’
'SEECityU’
>>> len(str1) >>> str1[0]
3 'S' Starting from 0
So, the latest should
>>> len(str2) >>> str3[7] be taking -1 from
the size
5 'U’
>>> len(str1+str2) >>> str3[1:4]
8 'EEC'
Basics: relational operator
>>> weekday=True
>>> weekend=False
>>> weekday==weekend
False
>>> weekday>weekend “Ture” value refers to 1
True “False” value refers to 0,
>>> weekday<weekend So, Ture>False (i.e., 1>0)
False
Basics: Logical operators
Basics: Logical operators
▶ and: True only if both elements are True (applies to a pair of elements)