te = () # empty tuple
t = (2, "mit", 3)
print(t[0]) # 2
print((2, "mit", 3) + (5, 6)) # (2, "mit", 3, 5, 6)
print(t[1:2]) # ("mit", ) -> extra comma means a tuple with one element
print(t[1:3]) # ("mit", 3)
len(t) # 3
# t[1] = 4 # gives an error, can modify object (immutablility)
temp = x
x = y
y = temp
# using tuple
(x, y) = (y, x)
def quotient_and_remainder (x, y) :
q = x // y # integer division
r = x % y
return (q, r)
(quot, rem) = quotient_and_remainder(4, 5)
print((quot, rem)) # (0, 4)
def get_data(aTuple) :
nums = ()
words = ()
for t in aTuple :
nums = nums + (t[0],)
if t[1] not in words :
words = words + (t[1],)
min_n = min(nums)
max_n = max(nums)
unique_words = len(words)
return (min_n, max_n, unique_words)
a_list = [] # empty list
L = [2, 'a', 4, [1, 2]]
print(len(L)) # 4
print(L[0]) # 2
print(L[2] + 1) # 5
print(L[3]) # [1, 2]
# print(L[4]) # this code gives an error
i = 2
print(L[i-1]) # a
L = [2, 1, 3]
L[1] = 5
print(L) # [2, 5, 3]
L = [2, 1, 3]
total = 0
for i in range(len(L)) :
total += L[i]
print(total) # 6
L = [2, 1, 3]
total = 0
for i in L :
total += i
print(total)
L = [2, 1, 3]
L.append(5)
print(L) # [2, 1, 3, 5]
L1 = [2, 1, 3]
L2 = [4, 5, 6]
L3 = L1 + L2
print(L3) # [2, 1, 3, 4, 5, 6]
print(L1) # [2, 1, 3]
print(L2) # [4, 5 ,6]
# Concatenation, L1 and L2 unchanged
L1.extend([0, 6])
print(L1) # [2, 1, 3, 0, 6]
# Mutated L1 to [2, 1, 3, 0, 6]
L = [2, 1, 3, 6, 3, 7, 0]
L.remove(2)
print(L) # [1, 3, 6, 3, 7, 0]
L.remove(3)
print(L) # [1, 6, 3, 7, 0]
del(L[1])
print(L) # [1, 3, 7, 0]
x = L.pop()
print(x) # 0
print(L) # [1, 3, 7]
list(s)
: convert string to list, returns a list with evrty character from s an element L
s.split()
: split a string on a character parameter
: splits on spaces if called without a parameter
''.join(L)
: turn a list of characters into a string
: can give a character in quotes to add char btw every element
s = "I<3 cs"
print(list(s)) # ['I', '<', '3', ' ', 'c', 's']
print(s.split('<')) # ['I', '3 cs']
L = ['a', 'b', 'c']
print(''.join(L)) # abc
print('_'.join(L)) # a_b_c
L = [9, 6, 0, 3]
print(sorted(L)) # returns sorted list, does not mutate # [0, 3, 6, 9]
L.sort() # mutates L, return None
print(L) # [0, 3, 6, 9]
L.reverse() # mutates L, return None
print(L) # [9, 6, 3, 0]
warm = ['red', 'yellow', 'oragnge']
hot = warm # aliasing
hot.append('pink')
print(hot) # ['red', 'yellow', 'oragnge', 'pink']
print(warm) # ['red', 'yellow', 'oragnge', 'pink']

cool = ['blue', 'green', 'grey']
chill = cool[:]
chill.append('black')
print(chill) # ['blue', 'green', 'grey', 'black']
print(cool) # ['blue', 'green', 'grey']
그림을 통해 살펴보자.

: cool 과 chill은 서로 다른 object를 가리키게 된다.
sorted(L) 은 정렬된 리스트를 반환하며, 반환받은 변수는 다른 object를 가리키게 된다. 코드와 그림으로 확인해보자.
cool = ['grey', 'green', 'blue']
sortedcool = sorted(cool)
print(cool) # ['grey', 'green', 'blue']
print(sortedcool) # ['blue', 'green', 'grey']

warm = ['yellow', 'orange']
hot = ['red']
brightcolors = [warm]
brightcolors.append(hot)
print(brightcolors) # [['yellow', 'orange'], ['red']]
hot.append('pink')
print(hot) # ['red', 'pink']
print(brightcolors) # [['yellow', 'orange'], ['red', 'pink']]
그림으로 확인해보자.

: brightcolors는 warm과 hot을 다시 참조하기 때문에, warm과 hot이 mutate되면 영향을 받게 된다.
Iterate할 때 Mutate한다면 또다른 문제를 불러일으킨다.
: Python uses an internal counter to keep track of index in the loop
: Mutating changes the list length but Python doesn't updatae the counter
: See this code
def remove_dups(L1, L2) :
for e in L1 :
if e in L2 :
L1.remove(e)
L1 = [1, 2, 3, 4]
L2 = [1, 2, 5, 6]
remove_dups(L1, L2)
print(L1) # [2, 3, 4]
def remove_dups(L1, L2) :
L1_copy = L1[:]
for e in L1_copy :
if e in L2 :
L1.remove(e)
L1 = [1, 2, 3, 4]
L2 = [1, 2, 5, 6]
remove_dups(L1, L2)
print(L1) # [3, 4]
: L1_copy를 loop element로 사용하여 문제를 해결하였다.