1
2
3
members = []
members = [“Pamela”, “Tinu”, “Brenda”, “Kaya”]
ages_of_kids = [1, 2, 7]
prices = [79.99, 49.99, 89.99]
digits = [2//2, 2+2+2+2, 2, 2*2*2]
remixed = [“Pamela”, 7, 79.99, 2*2*2]
4
len()
attendees = [“Tammy”, “Shonda”, “Tina”]
print(len(attendees))
num_of_attendees = len(attendees)
print(num_of_attendees)
5
len()
attendees = [“Tammy”, “Shonda”, “Tina”]
print(len(attendees)) # 3
num_of_attendees = len(attendees)
print(num_of_attendees)
5
letters = [‘A’, ‘B’, ‘C’] #Index: 0 1 2
letters[0]
letters[1]
letters[2]
letters[3]
curr_ind = 1
letters[curr_ind]
6
letters = [‘A’, ‘B’, ‘C’] #Index: 0 1 2
letters[0] # ‘A’
letters[1] # ‘B’
letters[2] # ‘C’
letters[3]
curr_ind = 1
letters[curr_ind] # ‘B’
6
letters = [‘A’, ‘B’, ‘C’] #Index: 0 1 2
letters[0] # ‘A’
letters[1] # ‘B’
letters[2] # ‘C’
letters[3] # Error!
curr_ind = 1
letters[curr_ind] # ‘B’
6
letters = [‘A’, ‘B’, ‘C’] #Index: 0 1 2
letters[0] # ‘A’
letters[1] # ‘B’
letters[2] # ‘C’
letters[3] # Error!
curr_ind = 1
letters[curr_ind] # ‘B’
6
letters = [‘A’, ‘B’, ‘C’] #Index: 0 1 2
letters[0] # ‘A’
letters[1] # ‘B’
letters[2] # ‘C’
letters[3] # Error!
curr_ind = 1
letters[curr_ind] # ‘B’
letters[-1] # ‘C’
letters[-2] # ‘B’
letters[-4] # Error!
6
from operator import getitem getitem(letters, 0)
7
+
boba_prices = [5.50, 6.50, 7.50]
smoothie_prices = [7.00, 7.50]
all_prices = boba_prices + smoothie_prices
add
from operator import add
boba_prices = [5.50, 6.50, 7.50]
smoothie_prices = [7.00, 7.50]
all_prices = add(boba_prices, smoothie_prices)
8
*
boba_prices = [5.50, 6.50, 7.50]
more_boba = boba_prices * 3
mul
from operator import mul boba_prices = [5.50, 6.50, 7.50]
more_boba = mul(boba_prices, 3)
digits = [1, 9, 8, 4]
together = [6, 2, 4] + digits * 2
together = add([2, 7], mul(digits, 2))
9
*
boba_prices = [5.50, 6.50, 7.50]
more_boba = boba_prices * 3
mul
from operator import mul boba_prices = [5.50, 6.50, 7.50]
more_boba = mul(boba_prices, 3)
digits = [1, 9, 8, 4]
together = [6, 2, 4] + digits * 2 # [6, 2, 4, 1, 9, 8, 4, 1, 9, 8, 4]
together = add([2, 7], mul(digits, 2))
9
gymnasts = [ [“Brittany”, 9.15, 9.4, 9.3, 9.2],
[“Lea”, 9, 8.8, 9.1, 9.5],
[“Maya”, 9.2, 8.7, 9.2, 8.8] ]
gymnasts
gymnasts[0]
10
gymnasts = [ [“Brittany”, 9.15, 9.4, 9.3, 9.2],
[“Lea”, 9, 8.8, 9.1, 9.5],
[“Maya”, 9.2, 8.7, 9.2, 8.8] ]
gymnasts
gymnasts[0]
10
gymnasts = [ [“Brittany”, 9.15, 9.4, 9.3, 9.2],
[“Lea”, 9, 8.8, 9.1, 9.5],
[“Maya”, 9.2, 8.7, 9.2, 8.8] ]
gymnasts
gymnasts[0]
10
gymnasts = [
]
[“Brittany”, 9.15, 9.4, 9.3, 9.2],
[“Lea”, 9, 8.8, 9.1, 9.5],
[“Maya”, 9.2, 8.7, 9.2, 8.8]
gymnasts[0]
gymnasts[0][0]
gymnasts[1][0]
gymnasts[1][4]
gymnasts[1][5]
gymnasts[3][0]
11
gymnasts = [
]
[“Brittany”, 9.15, 9.4, 9.3, 9.2],
[“Lea”, 9, 8.8, 9.1, 9.5],
[“Maya”, 9.2, 8.7, 9.2, 8.8]
gymnasts[0]
gymnasts[0][0]
gymnasts[1][0]
gymnasts[1][4]
gymnasts[1][5]
gymnasts[3][0]
# [“Brittany”, 9.15, 9.4, 9.3, 9.2]
11
gymnasts = [
]
[“Brittany”, 9.15, 9.4, 9.3, 9.2],
[“Lea”, 9, 8.8, 9.1, 9.5],
[“Maya”, 9.2, 8.7, 9.2, 8.8]
gymnasts[0]
gymnasts[0][0] # “Brittany”
gymnasts[1][0]
gymnasts[1][4]
gymnasts[1][5]
gymnasts[3][0]
# [“Brittany”, 9.15, 9.4, 9.3, 9.2]
11
gymnasts = [
]
[“Brittany”, 9.15, 9.4, 9.3, 9.2],
[“Lea”, 9, 8.8, 9.1, 9.5],
[“Maya”, 9.2, 8.7, 9.2, 8.8]
gymnasts[0]
gymnasts[0][0] # “Brittany”
gymnasts[1][0] # “Lea”
gymnasts[1][4]
gymnasts[1][5]
gymnasts[3][0]
# [“Brittany”, 9.15, 9.4, 9.3, 9.2]
11
gymnasts = [
]
[“Brittany”, 9.15, 9.4, 9.3, 9.2],
[“Lea”, 9, 8.8, 9.1, 9.5],
[“Maya”, 9.2, 8.7, 9.2, 8.8]
gymnasts[0]
gymnasts[0][0] # “Brittany”
gymnasts[1][0] # “Lea”
gymnasts[1][4] # 9.5
gymnasts[1][5]
gymnasts[3][0]
# [“Brittany”, 9.15, 9.4, 9.3, 9.2]
11
gymnasts = [
]
[“Brittany”, 9.15, 9.4, 9.3, 9.2],
[“Lea”, 9, 8.8, 9.1, 9.5],
[“Maya”, 9.2, 8.7, 9.2, 8.8]
gymnasts[0]
gymnasts[0][0] # “Brittany”
gymnasts[1][0] # “Lea”
gymnasts[1][4] # 9.5
gymnasts[1][5] # IndexError!
gymnasts[3][0]
# [“Brittany”, 9.15, 9.4, 9.3, 9.2]
11
gymnasts = [
]
[“Brittany”, 9.15, 9.4, 9.3, 9.2],
[“Lea”, 9, 8.8, 9.1, 9.5],
[“Maya”, 9.2, 8.7, 9.2, 8.8]
gymnasts[0]
gymnasts[0][0] # “Brittany”
gymnasts[1][0] # “Lea”
gymnasts[1][4] # 9.5
gymnasts[1][5] # IndexError!
gymnasts[3][0] # IndexError!
# [“Brittany”, 9.15, 9.4, 9.3, 9.2]
11
12
in
digits = [2, 8, 3, 1, 8, 5, 3, 0, 7, 1] 1 in digits
3 in digits
4 in digits
not (4 in digits)
13
in
digits = [2, 8, 3, 1, 8, 5, 3, 0, 7, 1] 1 in digits # True
3 in digits # True
4 in digits # False
not (4 in digits) # True
13
14
for
while
def count(s, value): total = 0
for element in s:
if element == value:
total = total + 1 return total
15
for
16
gymnasts = [
]
for-in
for gymnast in gymnasts: for data in gymnast:
print(data, end=”|”)
[“Brittany”, 9.15, 9.4, 9.3, 9.2],
[“Lea”, 9, 8.8, 9.1, 9.5],
[“Maya”, 9.2, 8.7, 9.2, 8.8]
17
pairs = [[1, 2], [2, 2], [3, 2], [4, 4]]
same_count = 0
for x, y in pairs: if x == y:
same_count = same_count + 1
18
19
… -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5…
range(-2, 3)
for num in range(6):
print(num) # 0, 1, 2, 3, 4, 5
for num in range(1, 6):
print(num) # 1, 2, 3, 4, 5
20
21
[