Professional Documents
Culture Documents
EEA216 資料結構
Data Structure
Lecture 1
Yu-Hui Huang, Data Structure
Yu-Hui Huang
Sep. 14, 2022
Yu-Hui Huang, Data Structure
Figure from https://medium.com/python4u/hello-python-509eabe5f5b1 2
Python – Setting up
• Windows : install Python IDE or Jupyter Notebook:
- install Anaconda/Miniconda
- use Google Colab from your browser:
https://colab.research.google.com/
$ python3 helloworld.py
hello world 11111
Yu-Hui Huang, Data Structure 4
Figure from
https://appdividend.com/2022/06/03/python-
5
comment-block/
Python – Comments
Ref: https://engineering.purdue.edu/~milind/datascience/2018spring/notes/lecture-2.pdf
Yu-Hui Huang, Data Structure 6
C vs. Python
if (condition) { if condition:
// block of code to be print(“…”)
executed if the condition is
true
}
Indentation : whitespaces at the
beginning of the line (print…)
temperature float
28.0
float other
25.0
#sequence types:
• The list Class: [‘red’, ‘blue’]
• The tuple Class: (3, 5,7)
• The str Class: “hello world”, ‘hi’
• The dict Class: {‘de’: ’German’, ‘en’: ‘English’}
a = 10
a >> 1 = ?
a << 2 = ?
>>> print(alpha)
• Ex:
>>> theList = [3, ‘guava’, 1]
>>> print(len(theList)) #number of items
3
https://medium.com/python4u/hello-python-509eabe5f5b1
My_List2[0][1] = 2
Figure from https://medium.com/python4u/hello-python-509eabe5f5b1
Yu-Hui Huang, Data Structure 24
Python – list Operations
• Ex:
>>> myList = [‘apple’, ‘guava’]
>>> myList + myList #Concatenation
[‘apple’,’guava’,’apple’,’guava’]
>>> myList * 2 #Repetition
[‘apple’,’guava’,’apple’,’guava’]
>>> 3 in myList #Membership
False
https://medium.com/python4u/hello-python-509eabe5f5b1
Reference: https://medium.com/python4u/hello-python-509eabe5f5b1
Yu-Hui Huang, Data Structure 26
Python – string vs. list
Common part:
• len
len(“123”) = > 3. len([1,2,3]) => 3
• Index
“123”[1] => ”2” [123][1] =>2
• In
“123” in “12345” => True
123 in [123,234,345] =>True
• Traverse
for t in “123”: for t in [1,2,3]:
print(t) print(t)
Reference: https://rilak.gitbooks.io/2018-python/content/string/basic.html
Yu-Hui Huang, Data Structure 27
Python – string vs. list
Different part:
• Immutable
String object does not support item assignment
a = “123” a = [1, 2, 3]
a[2] = “0” a[2] = 0
è TypeError! v
Reference: https://rilak.gitbooks.io/2018-python/content/string/basic.html
Yu-Hui Huang, Data Structure 28
Python – string methods
• split: string.split(str, n)
Reference: https://rilak.gitbooks.io/2018-python/content/string/basic.html
Yu-Hui Huang, Data Structure 29
Python – string methods
a = “apple”
print(a.replace(“p”,:””)). #ale
Reference: https://rilak.gitbooks.io/2018-python/content/string/basic.html
Yu-Hui Huang, Data Structure 30
Python – string methods
• split: string.split(str)
Reference: https://rilak.gitbooks.io/2018-python/content/string/basic.html
Yu-Hui Huang, Data Structure 31
Python – string methods
Reference: https://rilak.gitbooks.io/2018-python/content/string/basic.html
Yu-Hui Huang, Data Structure 32
Python – if/else Conditions
if first_condition: • Ex:
first_body if x > 1:
y = 5
elif second_condition:
elif: x <= 1:
second_body
y = 3
elif third_condition: else:
third_body y = 10
else:
forth_body
• Ex: • Output:
i = 1 1
while i < 5: 2
print(i) 3
4
i += 1 finish
print(‘finish’)
print(sum)
• Ex2:
for i in range(4): 0
1
print(i) 2
print(‘finish’) 3
finish
Exercise:
We have a list.
a = [1,6,2,4,7,10,3]
Write a program to print out all the
elements of the list that is smaller than 6.
print(count([0,2,4,6,3,1,1,0], 0))
Yu-Hui Huang, Data Structure 50
Python – functions
def a_list(items):
total = 0
for x in items:
total += x
return total
print(a_list[3,-5,6])
Syntax:
lambda [arg1 [,arg2, …, argn]]: expression
Ex:
sum = lambda arg1, arg2: arg1 + arg2;
print “value of total:” , sum(10, 20)
Yu-Hui Huang, Data Structure 52
Python – Variables Scope
• Global variables
• Local variables
#!/usr/bin/python
num = 0 #global variable
sum(10, 20)
print “Outside the function call: num = “, num
Yu-Hui Huang, Data Structure 53
Python – Variables Scope
#!/usr/bin/python
num = 0 #global variable
fp = open(‘xxx.txt’) #Mode
fo = open(‘yyy.txt’, ’w’) ‘w’ : write to the file
for lines in fp: ‘a’ : append to the end of the
line = fp.read() file
fo.write(line) ‘rb’: read binary files
fp.close() ‘wb’: write binary
fo.close()
https://runestone.academy/ns/books/
published/pythonds/index.html)