# ---------- Tuples ----------
student = ('Badger', 'Bucky', 'junior', 123, ('FIN 310', 'MATH 223', 'CS 410'))
student
('Badger', 'Bucky', 'junior', 123, ('FIN 310', 'MATH 223', 'CS 410'))
type(student)
tuple
type(('apple',)) # tuple of length 1 requires trailiing comma
tuple
type(('apple')) # string, not tuple
str
# student[2] = 'senior' # uncomment to see error: tuples are immutable
student = student[0:2] + ('senior',) + student[3:] # change variable, not tuple
student
('Badger', 'Bucky', 'senior', 123, ('FIN 310', 'MATH 223', 'CS 410'))
divmod(7, 3)
(2, 1)
(quotient, remainder) = divmod(7, 3) # (an unimportant illustrative function)
print(f'7 divided by 3 yields quotient {quotient} and remainder {remainder}.')
7 divided by 3 yields quotient 2 and remainder 1.
# ---------- Lists ----------
stocks = ['GME', 'AMZN']
list_of_lists = [[0, 0], [1, 1], [2, 5], [3, 9]]
list_of_lists # oops, [2, 5] should be [2, 4]: let's fix it ...
[[0, 0], [1, 1], [2, 5], [3, 9]]
list_of_lists[2][1] = 4
list_of_lists
[[0, 0], [1, 1], [2, 4], [3, 9]]
stocks.append('TWTR')
stocks
['GME', 'AMZN', 'TWTR']
stocks.append(['IBM', 'GOOG'])
stocks
['GME', 'AMZN', 'TWTR', ['IBM', 'GOOG']]
stocks = ['GME', 'AMZN']
stocks.extend(['IBM', 'GOOG'])
stocks
['GME', 'AMZN', 'IBM', 'GOOG']
stocks.remove('IBM')
stocks
['GME', 'AMZN', 'GOOG']
stocks.sort()
stocks
['AMZN', 'GME', 'GOOG']
stocks = ['GME', 'AMZN']
sorted_stocks = sorted(stocks)
print(f'sorted_stocks:{sorted_stocks}')
print(f'(original, unchanged) stocks:{stocks}')
sorted_stocks:['AMZN', 'GME'] (original, unchanged) stocks:['GME', 'AMZN']
squares = [1, 4, 9]
sum(squares)
14
stock_popped_from_index_1 = stocks.pop(1)
print(f'stock_popped_from_index_1={stock_popped_from_index_1}')
print(f'stocks={stocks}')
stock_popped_from_index_1=AMZN stocks=['GME']
sum_squares = 0 # here we use a loop to see what sum() does
for value in squares: # 1st way: set value to each item in sequence
sum_squares = sum_squares + value
print(f' value={value}, sum_squares={sum_squares}') # indent code 4 spaces
value=1, sum_squares=1 value=4, sum_squares=5 value=9, sum_squares=14
print(f'x=line before loop:')
for x in 'apple': # 1st way: set x to each value
print(f' x={x}') # indent code 4 spaces (indent output for debugging!)
print(f'done')
x=line before loop: x=a x=p x=p x=l x=e done
n = len(squares)
sum_squares = 0
for i in range(n): # 2nd way: set i to each index
sum_squares = sum_squares + squares[i]
print(f' i={i}, squares[{i}]={squares[i]}, sum_squares={sum_squares}')
i=0, squares[0]=1, sum_squares=1 i=1, squares[1]=4, sum_squares=5 i=2, squares[2]=9, sum_squares=14
product = 1 # sums start at 0, products at 1
n = len(squares)
for i in range(n): # 2nd way: set i to each index; range(n) is 0, ..., n-1
product = product * squares[i]
print(f' squares[{i}]={squares[i]}, product={product}')
squares[0]=1, product=1 squares[1]=4, product=4 squares[2]=9, product=36
stocks = ['GME', 'AMZN']
# lower-case stock names:
lower_stocks = []
for stock in stocks:
lower_stocks.append(stock.lower())
print(f'stock={stock}, lower_stocks={lower_stocks}')
print(lower_stocks)
stock=GME, lower_stocks=['gme'] stock=AMZN, lower_stocks=['gme', 'amzn'] ['gme', 'amzn']
# find portfolio = sum_i price_i * #shares_i:
price = (10, 15, 12)
n_shares = (1, 2, 5)
portfolio = 0
for i in range(len(price)):
portfolio += price[i] * n_shares[i] # x += y means "x = x + y"
print(f' i={i}, price[{i}]={price[i]}, n_shares[{i}]={n_shares[i]}, portfolio={portfolio}')
print(f'portfolio={portfolio}')
i=0, price[0]=10, n_shares[0]=1, portfolio=10 i=1, price[1]=15, n_shares[1]=2, portfolio=40 i=2, price[2]=12, n_shares[2]=5, portfolio=100 portfolio=100