05. Tuples, Lists, Mutability, Aliasing, and Cloning#
Last time#
Organization and hiding details
Create your own functions
Understand the concept of scope
Today#
Tuples#
A non-scalar type object can consist of different types of object.
a = () # empty tuple b = (1, 'a', 2, 'b') c = (3, 'c', 4)
a = () # empty tuple
b = (1, 'a', 2, 'b')
c = (3, 'c', 4)
# An ordered sequence
print("b[2] =", b[2])
# Tuple is additive
print(('aa',) + ('bb',))
print(a + b + c)
# Tuple can be sliced
print(b[1:])
print(b[1:2])
# Tuple has length
print(len(b + c))
# Tuple is immutable
b[0] = 0
b[2] = 2
('aa', 'bb')
(1, 'a', 2, 'b', 3, 'c', 4)
('a', 2, 'b')
('a',)
7
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Cell In[1], line 16
14 print(len(b + c))
15 # Tuple is immutable
---> 16 b[0] = 0
TypeError: 'tuple' object does not support item assignment
Functionalities of tuple#
Can be used to switch the values of variables
x = "倫理" y = "學術" # Which one will return you "學術倫理"? # 1 x = y y = x print(x + y) # 2 tmp = x x = y y = tmp print(x + y) # 3 (x, y) = (y, x) print(x + y)
Exercise 5.1#
Please write a function called
findExtComDiv
that aims to find the extreme common divisors of two positive integers \((n_1, n_2)\).Test your function with different inputs \((n_1, n_2)\), for example,
findExtComDiv(94,87)
.Here is your sample code:
def findExtComDiv(n1, n2):
"""
Assume that n1 and n2 are positive integers
Return the smallest common divisor of (n1, n2) that larger than 1, and the largest common divisor of (n1, n2).
If there is no common diviosrs, returns (None, None).
Params:
n1, n2: int, (n1, n2) >= 1
Returns:
minVal: int or None
maxVal: int or None
"""
# 自己想
return (minVal, maxVal)
Lists#
A non-scalar type object can consist of different types of object
a = [] # empty list
b = ['賴功德', '习近平', 'Joe Biden']
c = ['shooting', 'store robbers!']
d = ['less than', '$950']
# An ordered sequence
print("b[2] =", b[2])
# List is additive
print(b + c + d)
# List can be sliced
print(b[:2])
# List has length
print(len(b + c + d))
# List is mutable
b.insert(2, c[0])
print(b)
b.insert(2, c[0])
print(b)
print(b+d)
a = []
# append
for i in range(10):
a.append(i)
print("i = {}, and a = {}".format(i, a))
print("="*60)
# pop
for i in range(3):
a.pop(i)
print("i = {}, and a = {}".format(i, a))
# methods
# a.clear()
Mutability and aliasing#
# Total student list
list1 = ["112514001", "112514002", "112514003", "112514004", "112514005", "112514006", "112514007", "112514008"]
# Student list of computer science
studentlist_CS = list1
# Student list of semi-conductor physics
studentlist_scp = list1
# By the time of midterm, someone withdraw from the semi-conductor physics
studentlist_scp.remove("112514005")
# Print the remaining students of semi-conductor physics
print("Student list of SCP:", studentlist_scp)
# Print the remaining students of computer science
print("Student list of CS:", studentlist_CS)
# Print the original student list
print("Total student list:", list1)
Cloning#
# Total student list
list1 = ["112514001", "112514002", "112514003", "112514004", "112514005", "112514006", "112514007", "112514008"]
# Student list of computer science
studentlist_CS = list1.copy()
# Student list of semi-conductor physics
studentlist_scp = list1.copy()
# By the time of midterm, someone withdraw from the semi-conductor physics
studentlist_scp.remove("112514005")
# Print the remaining students of semi-conductor physics
print("Student list of SCP:", studentlist_scp)
# Print the remaining students of computer science
print("Student list of CS:", studentlist_CS)
# Print the original student list
print("Total student list:", list1)
Exercise 5.2#
Please write a function called
findLCM
that aims to find the least common multiple of two positive integers \((n_1, n_2)\).Test your function with different inputs \((n_1, n_2)\), for example,
findLCM(94,87)
.Here is your sample code:
def findLCM(n1, n2):
"""
Assume that n1 and n2 are positive integers
Return the least common multiple of (n1, n2) that larger than 1
Params:
n1, n2: int, (n1, n2) >= 1
Returns:
output: int
"""
# 自己想
return output