te = ()
te
()
t = (2, 'one', 3)
t
(2, 'one', 3)
t[0]
2
t + (5, 6)
(2, 'one', 3, 5, 6)
t[1] = 4
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-5-87b0f225887f> in <module>() ----> 1 t[1] = 4 TypeError: 'tuple' object does not support item assignment
t[1:2]
('one',)
t[1:3]
('one', 3)
x = 5
y = 2
(x, y) = (y, x) # swap variable values
print('x =', x)
print('y =', y)
x = 2 y = 5
def quotient_and_remainder(x, y):
q = x // y
r = x % y
return (q, r)
(quot, rem) = quotient_and_remainder(4, 5)
print('quotient =', quot)
print('remainder =', rem)
quotient = 0 remainder = 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_nums = min(nums)
max_nums = max(nums)
unique_words = len(words)
return (min_nums, max_nums, unique_words)
(small, large, words) = get_data(((1, 'mine'),
(3, 'yours'),
(5, 'ours'),
(7, 'mine')))
print('Small =', small)
print('Large =', large)
print('Unique words = ', words)
Small = 1 Large = 7 Unique words = 3
a_list = []
a_list
[]
b_list = [2, 'a', 4, True]
b_list
[2, 'a', 4, True]
L = [2, 1, 3]
print(len(L))
print(L[0])
print(L[2] + 1)
print(L[3])
3 2 4
--------------------------------------------------------------------------- IndexError Traceback (most recent call last) <ipython-input-13-27a29de3b80b> in <module>() 3 print(L[0]) 4 print(L[2] + 1) ----> 5 print(L[3]) IndexError: list index out of range
L[1] = 5
print(L)
[2, 5, 3]
total = 0
for i in range(len(L)):
total += L[i]
print(total)
10
total = 0
for i in L:
total += i
print(total)
10
print(L)
[2, 5, 3]
L.append(5)
L
[2, 5, 3, 5]
L1 = [2, 1, 3]
L2 = [4, 5, 6]
L3 = L1 + L2
L3
[2, 1, 3, 4, 5, 6]
L1
[2, 1, 3]
L2
[4, 5, 6]
L1.extend([0, 6])
L1
[2, 1, 3, 0, 6]
del(L1[3])
L1
[2, 1, 3, 6]
a = L1.pop()
L1
[2, 1, 3]
a
6
L = [2, 1, 3, 6, 3, 7, 0]
L.remove(2)
L
[1, 3, 6, 3, 7, 0]
L.remove(3)
L
[1, 6, 3, 7, 0]
del(L[1])
L
[1, 3, 7, 0]
L.pop()
L
[1, 3, 7]
s = 'I <3 cs'
list(s)
['I', ' ', '<', '3', ' ', 'c', 's']
s.split('<')
['I ', '3 cs']
s.split()
['I', '<3', 'cs']
L = ['a', 'b', 'c']
''.join(L)
'abc'
'_'.join(L)
'a_b_c'
L = [9, 6 , 0, 3]
L
[9, 6, 0, 3]
sorted(L)
[0, 3, 6, 9]
L
[9, 6, 0, 3]
L.sort()
L
[0, 3, 6, 9]
L.reverse()
L
[9, 6, 3, 0]
warm = ['red', 'yellow', ' orange']
hot = warm # aliasing
hot
['red', 'yellow', ' orange']
warm
['red', 'yellow', ' orange']
hot.append('pink')
hot
['red', 'yellow', ' orange', 'pink']
warm
['red', 'yellow', ' orange', 'pink']
cool = ['blue', 'green', 'grey']
chill = ['blue', 'green', 'grey']
cool
['blue', 'green', 'grey']
chill
['blue', 'green', 'grey']
chill[2] = 'blue' # mutating
chill
['blue', 'green', 'blue']
cool
['blue', 'green', 'grey']
chill = cool[:] # cloning
chill
['blue', 'green', 'grey']
chill.append('black')
chill
['blue', 'green', 'grey', 'black']
warm = ['red', 'yellow', 'orange']
sortedwarm = warm.sort()
print(warm)
print(sortedwarm)
['orange', 'red', 'yellow'] None
cool = ['grey', 'green', 'blue']
sortedcool = sorted(cool)
print(cool)
print(sortedcool)
['grey', 'green', 'blue'] ['blue', 'green', 'grey']
warm = ['yello', 'orange']
hot = ['red']
brightcolors = [warm]
brightcolors
[['yello', 'orange']]
brightcolors.append(hot)
brightcolors
[['yello', 'orange'], ['red']]
hot.append('pink')
print(hot)
print(brightcolors)
['red', 'pink'] [['yello', 'orange'], ['red', 'pink']]
print(hot + warm)
print(hot)
['red', 'pink', 'yello', 'orange'] ['red', 'pink']
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)
L1
[2, 3, 4]
def remove_dups_new(L1, L2):
L1_copy = L1[:] # clone
for e in L1_copy:
if e in L2:
L1.remove(e)
L1 = [1, 2, 3, 4]
L2 = [1, 2, 5, 6]
remove_dups_new(L1, L2)
L1
[3, 4]
def applyToEach(L, f):
'''
assumes L is a list, f a function
mutates L by replacing each element, e, of L by f(e)
'''
for i in range(len(L)):
L[i] = f(L[i])
L = [1, -2, 3.4]
applyToEach(L, abs)
L
[1, 2, 3.4]
applyToEach(L, int)
L
[1, 2, 3]
def fact(n):
if n == 1:
return 1
else:
return n*fact(n-1)
applyToEach(L, fact)
L
[1, 2, 6]
def fib(x):
if x == 0 or x == 1:
return 1
else:
return fib(x - 1) + fib(x - 2)
applyToEach(L, fib)
L
[1, 2, 13]
def applyFuns(L, x):
for f in L:
print(f(x))
applyFuns([abs, int, fact, fib], 4)
4 4 24 5
map(abs, [1, -2, 3, -4])
<map at 0x10f3ecb70>
for elt in map(abs, [1, -2, 3, -4]):
print(elt)
1 2 3 4
L1 = [1, 28, 36]
L2 = [2, 57, 9]
for elt in map(min, L1, L2):
print(elt)
1 28 9
a = []
for elt in map(abs, [1, -2, 3, -4]):
a += [elt]
a
[1, 2, 3, 4]