In [1]:
# ---------- Tuples ----------
student = ('Badger', 'Bucky', 'junior', 123, ('FIN 310', 'MATH 223', 'CS 410'))
student
Out[1]:
('Badger', 'Bucky', 'junior', 123, ('FIN 310', 'MATH 223', 'CS 410'))
In [2]:
type(student)
Out[2]:
tuple
In [3]:
type(('apple',)) # tuple of length 1 requires trailiing comma
Out[3]:
tuple
In [4]:
type(('apple'))  # string, not tuple
Out[4]:
str
In [5]:
# student[2] = 'senior' # uncomment to see error: tuples are immutable
In [6]:
student = student[0:2] + ('senior',) + student[3:] # change variable, not tuple
student
Out[6]:
('Badger', 'Bucky', 'senior', 123, ('FIN 310', 'MATH 223', 'CS 410'))
In [7]:
divmod(7, 3)
Out[7]:
(2, 1)
In [8]:
(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.
In [9]:
# ---------- 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 ...
Out[9]:
[[0, 0], [1, 1], [2, 5], [3, 9]]
In [10]:
list_of_lists[2][1] = 4
list_of_lists
Out[10]:
[[0, 0], [1, 1], [2, 4], [3, 9]]
In [11]:
stocks.append('TWTR')
stocks
Out[11]:
['GME', 'AMZN', 'TWTR']
In [12]:
stocks.append(['IBM', 'GOOG'])
stocks
Out[12]:
['GME', 'AMZN', 'TWTR', ['IBM', 'GOOG']]
In [13]:
stocks = ['GME', 'AMZN']
stocks.extend(['IBM', 'GOOG'])
stocks
Out[13]:
['GME', 'AMZN', 'IBM', 'GOOG']
In [14]:
stocks.remove('IBM')
stocks
Out[14]:
['GME', 'AMZN', 'GOOG']
In [15]:
stocks.sort()
stocks
Out[15]:
['AMZN', 'GME', 'GOOG']
In [16]:
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']
In [17]:
squares = [1, 4, 9]
sum(squares)
Out[17]:
14
In [18]:
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']
In [19]:
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
In [20]:
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
In [21]:
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
In [22]:
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
In [23]:
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']
In [24]:
# 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