0% found this document useful (0 votes)
23 views

Lecture2 Basic of Python

Uploaded by

louis425llw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Lecture2 Basic of Python

Uploaded by

louis425llw
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

SEE1002

Introduction to Computing for Energy and Environment

Lecture 2: Basic of Python


Languages for humans and computers
• Programming languages are for humans not computers!

• In order to run the program, it must be converted into a suitable format for the computer,
which is referred to as machine code.

Programming Language conversion Machine code


(for humans) (for computers)
I, Robot (2004) https://youtu.be/5sz1e2qh_ng
I, Robot (2004) https://youtu.be/Np1A4AGpqSo
Three Laws of Robo@cs
First Law
A robot may not injure a human being or, through inaction, allow a human
being to come to harm.

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.

• Unless you live alone on a deserted island,


many parts of your life will be made up of these programs.
(Conversion from analog to digital)

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.

• A bit refers to a single binary digit,


e.g. 0 or 1.
• Arbitrarily large numbers can be
formed by combining bits.
2 is a 2-bit number (i.e. 10).
Units for digital numbers
• byte = B = 8 bits (23 bits)

• kilobyte = KB = “1 thousand = 103 bytes” or 1024 bytes (210 bytes)

• megabyte =MB = “1 million = 106 bytes” or 1048576 bytes (220 bytes)

• gigabyte = GB = “1 billion = 109 bytes” or 230 bytes

• terabyte = TB = “1 trillion = 1012 bytes” or 240 bytes

• 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

Desktop on CSC computer → “Work Desk” icon

Python 3.7.4 (SEE) or Anaconda3 2020.07 (SEE)


The computers in CSC Teaching Studios have auto restore function, i.e. after booting the computers, the system will r
eturn to the original state, all installed software will be removed.

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)

Do you know why?


Common data types
• The following data types are standard:

‣ integer: -1, 0, 1, etc.


‣ float [real number]: 3.141519…, 2.7141…..
‣ string: “dog,” “cat”, “SEE”, ‘CityU!’
Strings are defined by enclosing their values within single or double quotes.
‣ boolean: True or False
Common data types
>>> type(123)
<class 'int’>

>>> 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

We’ll practice making functions in tomorrow’s practical


lab (Practical Lab 2)
Basics
>>> a=see1002
>>> b=3 Traceback (most recent call last):
>>> a/b File "<stdin>", line 1, in <module>
NameError: name 'see1002' is not defined
0.6666666666666666
>>> type(a)
<class 'int'> >>> int(3.5)
>>> type(b) 3
>>> float(3)
<class 'int'> 3.0
>>> round(3.5)
>>> type(a/b) 4
<class 'float'> >>> round(3.4)
3
Common data types
variable value
Naming variables

Python variables obey the following rules:

• No spaces or special characters (e.g. +,-,/,*, etc.)


• Case sensi]ve
• Numbers are fine (e.g. var1, var2)
• Can’t use reserved keywords (e.g. print)
• Variables can be very long, but long variable names are rarely
used in prac]ce.
Naming variables

>>> x1,x2=1,2 >>> 1x,2x=1,2


>>> x1 File "<stdin>", line 1
1 1x,2x=1,2
>>> x2 ^
2 SyntaxError: invalid
syntax
Naming variables
Mixed variables

>>> 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

>>> a=1 >>> a=1.0


>>> b=2 >>> b=1.001
>>> a<b >>> a==b
True False
>>> a>b >>> a!=b
False True
Basics: relaAonal 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

The 3 most important logical operators are and, or & not:

▶ and: True only if both elements are True (applies to a pair of elements)

▶ or: True if at least one element is True (applies to a pair of elements)

▶ not: Changes state of the boolean variable (applies to a single element)


Basics: Logical operators
>>> weekday=True
>>> weekend=False
>>> not weekday
False
>>> not weekend
True
>>> weekday and weekend
False
>>> weekday or weekend
True
Basics: print
>>> length=1.2
>>> width=2.0
>>> area=length*width
>>> print('The area is',area)
The area is 2.4
>>> print('length=',length,'width=',width)
length= 1.2 width= 2.0
>>> print('doubling the area=',2*area)
doubling the area= 4.8
Basics: comments
• print allows us to show a message on the screen for the user.
• Sometimes, however, we only want to leave a message for
ourselves, i.e., the programmer.
• To do this we can leave a comment. In Python, comments
start with #.

>>> # This is a comment


>>> x = 1 # define x
>>> y = 2*x # define y
>>> print(x,y) # print x and y
1 2
Basics: input
>>> number=input('Please enter a number: ')
Please enter a number:

Please enter a number: 5


>>> print(number)
5
Good job!
Now, you have learned (very) basics of python programming!

You can be a programmer!

Let’s have fun programming in prac^cal labs!

You might also like