Readings:
A set with "+" and "x" operations (that work properly).
Exercise: Create a set with a repeated value in and print it
S={1,2,2,3,4}
print(S)
{1, 2, 3, 4}
Exercise: Compute the cardinality of your set
print(len(S))
4
S={1,2,2,3,4}
T = S
T.add(6)
print(T)
print(S)
{1, 2, 3, 4, 6} {1, 2, 3, 4, 6}
S={1,2,2,3,4}
T = S.copy()
T.add(6)
print(T)
print(S)
{1, 2, 3, 4, 6} {1, 2, 3, 4}
dir(T)
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__', '__init_subclass__', '__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__', '__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
Q: Is a set a good data structure to make vectors and do linear algebra?
Convert your set to a list, print list, its length, it's first and last elements
Matlabesque way to select sub-sequences from list
Make slices for even and odd indexed members of your list.
Q: Is a list a good data structure to make vectors and do linear algebra?
Exercise:
(1,2,3)[0:2] # note can access directly
(1, 2)
a,b=1,2
print(a,b)
1 2
L = list(S)
S = set(L)
T = tuple(S)
mydict = {0:100,1:200,3:300}
mydict[0]
100
mydict = {'X':'hello','Y':'goodbye'}
mydict['X']
'hello'
print(mydict.keys())
print(mydict.values())
print(mydict.items())
dict_keys(['X', 'Y']) dict_values(['hello', 'goodbye']) dict_items([('X', 'hello'), ('Y', 'goodbye')])
Q: How would you use a dictionary to make a vector?
def f(x): # <--- def keyword and colon after name and arguments
print('hi') # <--- contents must have same indent
return x**2 # <--- return output
y = f(4)
print("y =",y)
hi y = 16
Exercise: make a function to count number of unique values in a list
if not (2+2 == 4):
print('tree')
else:
print('hi')
hi
Exercise: implement this with a python function
L=[2*i for i in {1,2,3}]
print(L)
[2, 4, 6]
Exercise: implement $\{x^2 : x \in L\}$ for your list $L$
(i.e., iterate over list and produce list squared)
L=[1,1,2,3,4,4,5]
Exercise: Iterate over dictionary and swap keys with values
for x in {1,2,3}:
print(x)
print(x*x)
1 1 2 4 3 9
for x in {1,2,3}:
print(x)
for y in {1,2,3}: # a nested loop
print('...',x,y)
1 ... 1 1 ... 1 2 ... 1 3 2 ... 2 1 ... 2 2 ... 2 3 3 ... 3 1 ... 3 2 ... 3 3
x=0
while x<5:
print(x)
x=x+1
0 1 2 3 4
A function, though we often just write the function contents "inline"
$$\sum_{i=1}^N x_i = x_1+x_2+...+x_N = "sum(x)"$$Exercise: implement the sum over your list with a loop
(you can compare to built-in functions "sum()", but use a loop for this exercise)
Used for making a list of incremental numbers to loop over
range(10,0,-1)
range(10, 0, -1)
list(range(10,0,-1)) # construct list using iterator
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
for x in range(0,10): # loop over iterator
print(x)
0 1 2 3 4 5 6 7 8 9
Clunky but handy to encapsulate related code. Feel free to avoid writing any.
Note odd way that constructor is defined
Also need to pass "self" argument to every function
class Complex:
def __init__(self, realpart, imagpart):
self.r = realpart
self.i = imagpart
x = Complex(3.0, -4.5)
x.r, x.i
(3.0, -4.5)