1
2
3
4
name = ‘PamelamaDingDong’
type(name)
name[0]
name[8:]
name.upper()
name.lower()
5
6
append()
s = [2, 3] t = [5, 6] s.append(4) s.append(t) t=0
extend()
s = [2, 3] t = [5, 6] s.extend(4) s.extend(t) t=0
7
append()
s = [2, 3] t = [5, 6] s.append(4) s.append(t) t=0
extend()
s = [2, 3]
t = [5, 6]
s.extend(4) # Error: 4 is not an iterable! s.extend(t)
t=0
7
pop()
s = [2, 3]
t = [5, 6]
t = s.pop()
remove()
s = [6, 2, 4, 8, 4]
s.remove(4)
8
L = [1, 2, 3, 4, 5]
L[2] = 6
L[1:3] = [9, 8]
L[2:4] = [] # Deleting elements
L[1:1] = [2, 3, 4, 5] # Inserting elements
L[len(L):] = [10, 11] # Appending
L = L + [20, 30]
L[0:0] = range(-3, 0) # Prepending
9
10
users = {}
users[“profpamela”] =
users[“profpamela”] += “itsLongerSoItsMoreSecure!!”
>>> users[“profpamela”]
11
users = {}
users[“profpamela”] =
users[“profpamela”] += “itsLongerSoItsMoreSecure!!”
>>> users[“profpamela”]
11
12
empty = ()
# or
empty = tuple()
conditions = (‘rain’, ‘shine’)
# or
conditions = ‘rain’, ‘shine’
13
empty = ()
# or
empty = tuple()
conditions = (‘rain’, ‘shine’)
# or
conditions = ‘rain’, ‘shine’
oogly = (61,)
# or
oogly = 61,
13
(‘come’, ‘☂’) + (‘or’, ‘☼’)
‘wally’ in (‘wall-e’, ‘wallace’, ‘waldo’)
rainbow = (‘red’, ‘orange’, ‘yellow’, ‘green’, ‘blue’, ‘indigo’, ‘vio
roy = rainbow[:3]
14
le
(‘come’, ‘☂’) + (‘or’, ‘☼’) # (‘come’, ‘☂’, ‘or’, ‘☼’)
‘wally’ in (‘wall-e’, ‘wallace’, ‘waldo’)
rainbow = (‘red’, ‘orange’, ‘yellow’, ‘green’, ‘blue’, ‘indigo’, ‘vio
roy = rainbow[:3]
14
le
(‘come’, ‘☂’) + (‘or’, ‘☼’) # (‘come’, ‘☂’, ‘or’, ‘☼’)
‘wally’ in (‘wall-e’, ‘wallace’, ‘waldo’) # True
rainbow = (‘red’, ‘orange’, ‘yellow’, ‘green’, ‘blue’, ‘indigo’, ‘vio
roy = rainbow[:3]
14
le
(‘come’, ‘☂’) + (‘or’, ‘☼’) # (‘come’, ‘☂’, ‘or’, ‘☼’)
‘wally’ in (‘wall-e’, ‘wallace’, ‘waldo’) # True
rainbow = (‘red’, ‘orange’, ‘yellow’, ‘green’, ‘blue’, ‘indigo’, ‘vio
roy = rainbow[:3] # (‘red’, ‘orange’, ‘yellow’)
14
le
15
a_tuple = (1, 2)
a_tuple[0] = 3
a_string = “Hi y’all”
a_string[1] = “I”
a_string += “, how you doing?”
an_int = 20
an_int += 2
grades = [90, 70, 85]
grades_copy = grades
grades[1] = 100
words = {“agua”: “water”}
words[“pavo”] = “turkey”
16
a_tuple = (1, 2)
a_tuple[0] = 3
a_string = “Hi y’all”
a_string[1] = “I”
a_string += “, how you doing?”
an_int = 20
an_int += 2
# Error! Tuple items cannot be set.
# Error! String elements cannot be set.
grades = [90, 70, 85]
grades_copy = grades
grades[1] = 100
words = {“agua”: “water”}
words[“pavo”] = “turkey”
16
a_tuple = (1, 2)
a_tuple[0] = 3 # Error! Tuple items cannot be set.
a_string = “Hi y’all”
a_string[1] = “I” # Error! String elements cannot be set.
a_string += “, how you doing?” # How does this work?
an_int = 20
an_int += 2
# And this?
grades = [90, 70, 85]
grades_copy = grades
grades[1] = 100
words = {“agua”: “water”}
words[“pavo”] = “turkey”
16
x+x x+x
x+x x+x
17
x=2
x+x#4 x+x
x+x x+x
17
x=2
x+x#4 x=3
x+x#6
x+x x+x
17
x=2
x+x#4 x=3
x+x#6
x = [‘A’, ‘B’]
x + x # [‘A’, ‘B’, ‘A’, ‘B’]
x+x
17
x=2
x+x#4 x=3
x+x#6
x = [‘A’, ‘B’]
x + x # [‘A’, ‘B’, ‘A’, ‘B’]
x.append(‘C’)
x + x # [‘A’, ‘B’, ‘C’, ‘A’, ‘B’, ‘C’]
17
t = (1, [2, 3])
t[1][0] = 99
t[1][1] = “Problems”
18
list1 = [1,2,3]
list2 = [1,2,3]
exp0 == exp1
True exp0 exp1
list1 == list2
19
list1 = [1,2,3]
list2 = [1,2,3]
exp0 == exp1
True exp0 exp1
list1 == list2 # True
19
list1 = [1,2,3]
list2 = [1,2,3]
exp0 == exp1
True exp0 exp1
list1 == list2 # True
exp0 is exp1
True exp0 exp1
list1 is list2
19
list1 = [1,2,3]
list2 = [1,2,3]
exp0 == exp1
True exp0 exp1
list1 == list2 # True
exp0 is exp1
True exp0 exp1
list1 is list2 # False
19
20
four = [1, 2, 3, 4]
print(four[0])
do_stuff_to(four)
print(four[0])
four = [1, 2, 3, 4]
print(four[3])
do_other_stuff()
print(four[3])
21
turtle = (1, 2, 3)
ooze()
turtle # (1, 2, 3)
turtle = [1, 2, 3]
ooze()
turtle # [1, 2, ‘Mwahaha’]
22
def f(s=[]): s.append(3)
return len(s)
f() # 1
f() # 2
f() # 3
s
23
24
25
withdraw(25) # 75
25
withdraw(25) # 75
withdraw(25) # 50
25
withdraw(25)
withdraw(25)
withdraw(60)
# 75
# 50
# ‘Insufficient funds’
25
withdraw = make_withdraw_account(100) # Contains a list
withdraw(25)
withdraw(25)
withdraw(60)
# 75
# 50
# ‘Insufficient funds’
25
def make_withdraw_account(initial): balance = [initial]
def withdraw(amount):
if balance[0] – amount < 0:
return 'Insufficient funds' balance[0] -= amount
return balance[0]
return withdraw
26
27
28