1
EEA216 資料結構
Data Structure
Lecture 1
Yu-Hui Huang, Data Structure
Yu-Hui Huang
Sep. 11, 2025
Announcements
• 9/17(三)13:10-14:00 Python 介紹課程@70103
Yu-Hui Huang, Data Structure 2
Python - Basics
• An interpreted language
• An object-oriented language (classes form the basis
for all data types)
Yu-Hui Huang, Data Structure 3
Python - Basics
• assignment statement:
temperature = 25.0
temperature float (class)
(identifier) 25.0
• alias:
other = temperature
temperature float other
25.0
Yu-Hui Huang, Data Structure 4
Python – Built-in Classes
• The int Class: 0, -3, 0b1010, 0o51, 0x6f
• The float Class: 3.14, 6e-23
• The bool Class: True, False
#sequence types:
• The list Class: [‘red’, ‘blue’, 3]
• The tuple Class: (3, 5, 7, ‘y’)
• The str Class: “hello world”, ‘hi’
• The dict Class: {‘de’: ’German’, ‘en’: ‘English’}
Yu-Hui Huang, Data Structure 5
Python - Basics
Start Stop Step
Control statements:
1. for loop for i in range(5, 0, -2):
2. while loop print(i)
3. If / elif / else while condition:
print(‘xd’)
if condition:
print(’xd’)
Yu-Hui Huang, Data Structure 6
Python - Basics
#Mutable: Changeable or has the ability to change.
- List myList = [‘abc’, ‘3’, 50]
- Sets mySet = {‘apple’, ‘banana’}
- Dictionaries myDict = {‘1’: ‘Tom’, ‘de’:’German’}
#Immutable: not changeable
- numbers (integers, floats, Boolean,…)
- strings myStr = ’this is a string.’
- tuples myTup = (‘abc’, 3, ‘d’)
Yu-Hui Huang, Data Structure 7
Python - Basics
temperature = 25.0
other = temperature
temperature = temperature + 3.0
temperature float
28.0
float other
25.0
The temperature identifier is assigned to a new value
Yu-Hui Huang, Data Structure 8
Python – id function
• The id() returns a unique id for the specified object.
The id is the object’s memory address.
>>> a=3 >>> b+=3 >>> b=b+3
>>> hex(id(a)) >>> b >>> b
'0x10b92ff50' 6 9
>>> a >>> a
>>> b=a 3 3
>>> hex(id(b)) >>> hex(id(b)) >>> hex(id(b))
'0x10b92ff50' '0x10b92ffb0' '0x10b930010'
>>> hex(id(a))
'0x10b92ff50'
Reference: https://www.w3schools.com/python/ref_func_id.asp
Yu-Hui Huang, Data Structure 9
Python - Basics
alpha = [1,2] alpha
beta = alpha list
beta += [3,4] [1,2] beta
beta = beta + [5,6]
+= calls the __iadd__method
+ calls the add method
Yu-Hui Huang, Data Structure 10
Python – id function
• The id() returns a unique id for the specified object.
The id is the object’s memory address.
>>> a=[1,2] >>> b+=[3,4] >>> b=b+[5,6]
>>> hex(id(a)) >>> hex(id(b)) >>> hex(id(b))
'0x10bba5280' '0x10bba5280' '0x10bba55a0'
>>> b=a >>> hex(id(a))
>>> hex(id(a))
>>> hex(id(b)) '0x10bba5280'
'0x10bba5280'
'0x10bba5280'
Reference: https://www.w3schools.com/python/ref_func_id.asp
Yu-Hui Huang, Data Structure 11
Python – functions
def count(data, target): -->function signature
”count how many elements
equal to the target from the data” -->function docstring
total = 0
for item in data:
if item == target:
total += 1
return total -->return expression
print(count([0,2,4,6,3,1,1,0], 0))
Yu-Hui Huang, Data Structure 12
Python – functions
def a_list(items):
total = 0
for x in items:
total += x
return total
print(a_list[3,-5,6])
Yu-Hui Huang, Data Structure 13
Python – Anonymous Functions (*)
• Functions not called through def keyword are called
anonymous functions.
• Use lambda keyword to create anonymous functions.
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 14
Python – Variables Scope
• Global variables
• Local variables
#!/usr/bin/python
num = 0 #global variable
def sum (arg1, arg2):
num = arg1 + arg2
print “Inside the function call: num = “, num
sum(10, 20)
print “Outside the function call: num = “, num
Yu-Hui Huang, Data Structure 15
Python – Variables Scope
#!/usr/bin/python
num = 0 #global variable
def sum (arg1, arg2):
num = arg1 + arg2
print “Inside the function call: num = “, num
return num
num = sum(10, 20)
print “Outside the function call: num = “, num
Yu-Hui Huang, Data Structure 16
Python – Variables Scope
#!/usr/bin/python
greeting = “HELLO” #global variable
def change_greeting (new_greeting):
greeting = new_greeting
def greeting_world ():
world = “WORLD”
print(greeting, world)
change_greeting(“HI”)
Reference: https://www.datacamp.com/tutorial/scope-of-
greeting_world() variables-python
Yu-Hui Huang, Data Structure 17
Python – Variables Scope
#!/usr/bin/python
greeting = “HELLO” #global variable
def change_greeting (new_greeting):
global greeting = new_greeting
def greeting_world ():
world = “WORLD”
print(greeting, world)
change_greeting(“HI”)
greeting_world()
Yu-Hui Huang, Data Structure 18
Python – OOP
• Everything in Python is object
• Object can have attributes and methods
• Attribute: A variable stored in an instance or class
• Method: A function stored in an instance or class
• A class is a blueprint of how something should be
defined, but it doesn’t activate the content itself
Yu-Hui Huang, Data Structure 19
Python – OOP
Fig. from https://commons.wikimedia.org/wiki/File:CPT-OOP-objects_and_classes_-_attmeth.svg
Yu-Hui Huang, Data Structure 20
Python – OOP
• Everything in Python is object
• Object can have attributes and methods
• Attribute: A variable stored in an instance or class
• Method: A function stored in an instance or class
class Car:
def __init__(self, fuel, maxspd): constructor
self.fuel = fuel
self.maxspeed = maxsp
mini = Car(3, 180) Create an instance
Yu-Hui Huang, Data Structure 21
Python – OOP
class Car:
def __init__(self, fuel, maxspd):
self.fuel = fuel
self.maxspeed = maxsp
self.speed = 0
def setSpeed(spd):
self.speed = spd
def getSpeed(spd):
return self.speed
def drive():
print(‘is driving..’)
mini = Car(3, 180)
mini.setSpeed(50)
currentSpeed = mini.getSpeed()
Yu-Hui Huang, Data Structure 22
Python – functions vs. method
function:
describe a traditional, stateless function that is invoked
without the context of a particular class or an instance
of that class.
Ex: sorted(data)
method:
describe a member function that is invoked upon a
specific object using an object-oriented message
passing syntax
Ex: data.sort()
Yu-Hui Huang, Data Structure 23
Python – OOP
• Object-oriented design principles
• Inheritance
• Abstraction
• Encapsulation
• Polymorphism
Yu-Hui Huang, Data Structure 24
Python – Class Inheritance
class Parent: c = Child()
parentAttr = 10 c.childMethod()
def __init__(self): c.parentMethod()
print(“calling parent constructor”)
c.setAttr(20)
def parentMethod(self):
print(‘calling parent method”) c.getAttr()
def setAttr(self, attr):
Parent.parentAttr = attr
def getAttr(self):
print(”parent attr:”, Parent.parentAttr)
class Child(Parent):
def __init__(self):
print(“calling child constructor”)
def childMethod(self):
print(“calling child method”)
Yu-Hui Huang, Data Structure 25
Python – Overriding
class Parent:
def myMethod(self):
print(‘calling parent method”)
class Child(Parent):
def myMethod(self):
print(“calling child method”)
c = Child()
c.myMethod()
Yu-Hui Huang, Data Structure 26
Python – Encapsulation
• Encapsulation describes the idea of wrapping data
and the methods that work on data within one
unit.
• Can prevent from accidental change of the data
• A class is an example of encapsulation as it
encapsulates all the data that is member
functions, variables, etc.
Yu-Hui Huang, Data Structure 27
Python – Encapsulation
class hid:
__hiddenVar = 0 # declaring private member of class
def sum(self, counter):
self.__hiddenVar += counter
print(self.__hiddenVar)
hiddenobj = hid()
hiddenobj.sum(5)
hiddenobj.sum(7)
print(hiddenobj.__hiddenVar)
AttributeError: 'hidden' object has no attribute '__hiddenVar'
Yu-Hui Huang, Data Structure 28
Python – Encapsulation
class hid:
__hiddenVar = 0 # declaring private member of class
def sum(self, counter):
self.__hiddenVar += counter
print(self.__hiddenVar)
hiddenobj = hid()
hiddenobj.sum(5)
hiddenobj.sum(7)
#print(hiddenobj.__hiddenVar)
print(hiddenobj._hid__hiddenVar)
Yu-Hui Huang, Data Structure 29
Python – Polymorphism
• Polymorphism = having many forms.
= the same function name (with different
signatures) being used for different types.
Example:
>>>len(“apple”)
5
>>>len([3,5,7])
3
Reference: https://www.geeksforgeeks.org/polymorphism-in-python/
Yu-Hui Huang, Data Structure 30
Python – Polymorphism
• Polymorphism with inheritance
class Bird:
def flight(self):
print(“most of the birds can fly”)
class sparrow(Bird):
def flight(self):
print(“sparrows can fly”)
bd = Bird()
spr = sparrow()
bd.flight()
spr.flight()
Yu-Hui Huang, Data Structure 31
OOP Exercise
Create a Python program to create a
Vehicle class with max_speed and mileage
instance attributes.
Yu-Hui Huang, Data Structure 32
OOP Exercise
Create a child class Bus which inherits
all the variables and methods from
Vehicle class.
class Vehicle:
def __init__(self, name, max_speed, mileage):
self.name = name
self.max_speed = max_speed
self.mileage = mileage
Yu-Hui Huang, Data Structure 33
Python – References:
• 資訊之芽Python班課程講義
https://rilak.gitbooks.io/2018-python/content/
• 黃建庭的教學網站
https://sites.google.com/view/zsgititit/
• Hello Python!|Python入門詳細介紹
https://medium.com/python4u/hello-python-
509eabe5f5b1
• W3 Schools
https://www.w3schools.com/python/default.asp
Yu-Hui Huang, Data Structure 34
Python Exercises (W3Schools)
• Python lists
https://www.w3schools.com/python/exercise.asp
?filename=exercise_lists1
• Python If … Else
https://www.w3schools.com/python/exercise.asp
?filename=exercise_ifelse1
• Python While Loops
https://www.w3schools.com/python/exercise.asp
?filename=exercise_while_loops1
• Python For Loops
https://www.w3schools.com/python/exercise.asp
?filename=exercise_for_loops1
Yu-Hui Huang, Data Structure 35
Python – Exercise(W3Schools)
• Python Classes/Objects
https://www.w3schools.com/python/python_classes.asp
• Python Inheritance
https://www.w3schools.com/python/python_inheritance.a
sp
• Python Lists
https://www.w3schools.com/python/exercise.asp?filenam
e=exercise_lists1
• Python Tuples
https://www.w3schools.com/python/exercise.asp?filenam
e=exercise_tuples1
Yu-Hui Huang, Data Structure 36