x=[1,2,3,4] x[1] x[2] x[3] x[4] #Talk about indexing from 0, rather than 1. x[-1] x[-2] x[5] x[-5] #Note that some languages don't do bound checking. C is famous for not. Maybe then go to memory model images and show why this is a bad idea. x=[False,1,2.0,'three'] print x #note that the shell is reminding you that x[3] is a string. print x[3] x[3]='four' print x id(x) #emphasise that what we're interested now is just in whether this number changes. We don't really care what it is or how we get the number. #analagous to the 0x1's that we've been using. x[3]=3 id(x) y='four' id(y) y=3 id(y) #maybe do a function f(x) that just prints the id of x and compare to what we've been drawing. #don't print the id of constants and compare them to identical variables. #back to lecture notes and review this stuff. len(x) len([]) x=[1,2,3,4,5] min(x) max(x) x[3]=10 max(x) x[3]=-10 min(x) sum(x) x[2]=7.0 max(x) min(x) sum(x) x=['ada','bssa','afda'] min(x) max(x) sum(x) #Note that we do not concatenate the strings! x=[1,'1',2.0] min(x) max(x) #highlight that mixing types when using relational operators is bad. sum(x) #often build lists one element at a time and one doesn't know how many elements one needs. #remind of method notation. #basically can do while(not empty) list.append., move to next element. x.append(5) x x.append(3) x.sort() x.insert(2,4.0) x #Most of the time, I test all the shell before lecture, but I decided to #not test these so that we can be surprised together. x.insert(-10, 40) x.insert(20, 30) #Why? To emphasise that the point of this course is not to exhaustively train you and teach you everything. #It's more to get you familiar with the tools you need to understand programming jargon so that you're capable of finding things out for yourself. #There's just far too much to teach exhaustively, but since it's all written using the same terminology, if you're comfortable with the terminology, you should be comfortable finding things out on your own. x #insert something that's doubled. x.remove(doubled_thing) #insert several. x.count(doubled_thing) x.remove(doubled_thing) x.count(doubled_thing) #Go to notes. range(10) range(5,10) range(0,10) #same as range(10) range(0,10,2) range(0,10,3) range(0,10,12) range(0,10,-1) range(10,0,-1) #Why omit last element? So we can do for i in range(len(list)) #Go to notes. Consider break here. x=[1,2,3,4,5,6,24] x[1:3] x[0] x[1] x[3:] x[:3]#slicing has the same options as the range function x[::2] x[10:2:-1] x[10:2:1] #now slicing and aliasing. y=x z=x[:] x[0]=55 x y z #z is the same as the original. Go to board and explain how this #could work, and why it makes sense, because you need to reindex things. #back to slides. x=() # so have an empty tuple. type(x) x=(8) type(x) x=(8,) x(0) x[0] y=x x=(8,7,5) x y#back to slides. x='string' min(x), max(x), sum(x) x(3:) x(:3) x(2:4) x(::2) x.find('ring') x.find('ring',2,6) #fiddle to get it to work. x='tattle' #which way does rfind work? x.find('tt') x.rfind('tt')#back to slides. x=[[1,2,3],[4,5,6],[7,8,9]] x[0][0] x[0][1] x[2][0] x[2][:2] x[:2][:2] #slicing doesn't work the way you'd expect. Better to use loops.