Everyone, welcome. I have two handouts for you today. You all know that Assignment 7 is due this evening, 11:59. It is the last assignment that wefll issue a letter grade on. Like Assignment 1, Assignment 8 is intended to be this kind of like transition type of thing. Wefre trying this out of the course. Ifm not planning on putting Python or anything related to Python on the final exam. Ifm just trying to fill out the course with a new interesting language thatfs topical. It certainly represents a paradigm, but Ifm just trying to get in the habit or not forcing lesson material on the last few weeks or the last few lectures to come up with a clean endpoint, which is probably Ifll call last Friday as far as testable material, but still fill out the quarter with an interesting assignment. The assignment thatfs due a week from today ? Ifm sorry, a week from tomorrow. Ifm giving you until Thursday evening of next week. Itfs this very short Python assignment thatfs just intended to get you used to the framework. Itfs not gonna teach you everything about the language. Itfs really just taking a program that works and making it work a little bit differently just so youfre forced to deal with Python and its integers and its Booleans and its tests and to fully define functions and classes and things like that, okay. So as far as how the rest of the quarter will work out, I will definitely lecture today and Friday. I probably will lecture Monday because I donft think Ifm gonna get through all the Python material. As to whether or not I lecture next Wednesday, I think will just depend on how quickly I get through the Python material that I want to introduce to you, but Ifm not trying to race as much material as possible into the course because I know you all have a lot of other things going on. Fortunately, 107 does not have a lot of stuff going on in the next week. After you get the scheme assignment done, then youfre really just in a situation where you can start studying for the final, okay. Ifll have a practice mid-term ? Ifm sorry, a practice final probably out next Monday, a practice solution as well. The mid-term is scheduled for June 9. You can take it either at 8:30 or 3:30 on June 9. I have rooms set up. I forget where they are. Ifll put that on the practice exams. Okay, so this thing called Python. Oops, letfs spell it right. There are two overarching features that I want to emphasize. It is as opposed to anything youfve seen before, Scheme you could argue is a little bit like this, but C and C++ and Java are certainly not. Itfs an example of whatfs called a scripting language. Okay, and then as far as that, we can call that a paradigm. It is also imperative. It is also object oriented. It is also functional or it has functional components in the language just like Scheme does, and Ifll show you an example hopefully today, either today or the beginning of Friday where you actually see the equivalent of map, okay, in Python, and youfre all intimately familiar with the notion of map right now. Youfve seen vector map and youfve seen iterators and things like that in C++ and you certainly know map from Scheme. There really are lambdas. In Python, therefs also the map function. There is the notion of an evaluate statement just like there is in Scheme. So I put these up here kind of to remind you that imperative is the C language, itfs the assignment oriented, function oriented type of language, Ifm sorry, procedure-oriented type of language. [Inaudible], youfre very familiar with that. Everythingfs centered around the data and we call objects for classes and functional is an example of a paradigm which Scheme adheres to. I would say that Python is object oriented like C++ is. A lot of people program in C++, not so much as C++ programmers, but as C programmers, but like to use objects. Does that make sense? And they still had this very sequential way of thinking and they organize data in terms of classes as opposed to structs. I actually ? I get the impression based on what little Python programming Ifve done and what good amount of Python code Ifve seen is that it really is the case that people program imperatively, okay. They rarely subscribe to the functional or object of paradigms. They usually go with this imperative approach where they have a series of tasks that need to be done one after the other, and they just get them done in bullet point format, okay, and subscribe more to the C like or Pascal or just the imperative approach of actually accumulating data piecemeal until you actually have a result at the end. They may incidentally use objects, certainly those that are part of library set that come with Python. They may like to use lambdas and they see an opportunity to use some functional paradigm approach to solve some subset of the problem. They might do that. I really think of it as this where it just happens to have these features inside of it. Thatfs consistent with the fact that itfs a scripting language. The best analogy that I can think of outside of programming for scripting analogy is if you actually ever go see a play and for whatever reason therefs like a casting emergency and somebody who normally plays the part and has the lines memorized isnft available, so somebody has to actually fill in the part and they have to hold the script in their hand, okay. I have seen that a couple of times. They have to go through the motions. They have to do everything perfectly, but they actually are reading their instruction sets while they do it, okay. Do you understand what I mean when I say that? Okay. Just think about a set and like a dress rehearsal for a play where you still have a script in hand. I think of a Python script or any kind of shell script for that matter as being very much like that. Itfs this very what you see is what you get. It digests function definitions and it digests individual statements as they read them and as they read them the side effects of their execution are actually realized while the script is running, okay. So itfs like Scheme in that regard. We normally prepare all our functions and put them in a dot SCM file ahead of time and then load them and then run the result. That still has a scripting feel to it, but the emphasis is on the functional paradigm. With Python, and a lot of things, Pearl, a lot of things that are designed to be run a script, to batch process these together to get some large set of tasks done. I actually see that ? I see Python doing that more than anything else. I donft see many games written in Python. I donft see many compilers written in Python, although I do see some servers written in Python. A couple of ? I donft want to say the names because Ifm not actually sure of them, but there is this set of companies right now that are setting up services, not these companies specifically, but companies like Twitter and Friend Feed that are all trying to basically broadcast information. Some of them, Ifm not sure which ones. I think of one, but I donft want to say it and be wrong, are actually writing all of their server and services in Python, okay. Normally those things have been written in Java or in C++. Python has a very rich set of networking libraries and natural support for SMTP and ACTP and ACTPS and all of these networking protocols that are very common today with all of these distributed systems that are set up to provide these types of services, and I know Python is contributing to that. So there really is some large-scale software thatfs being written in Python beyond the scripting set that Ifm gonna focus on today, okay. As far as other buzzwords that describe Python, it is dynamic. Scheme is also dynamic. C and C++ are not at all, okay. They have a huge compile time element and therefs very little type information at run time. Scheme and Python both maintain type information about every single piece of data that exists in the run time, and you can actually query the data type if you want to, okay. Does that make sense? Okay. It is really interesting to code in Python because it feels very much like C and C++ and Java in the way that you just do block structures and if statement and Y loops and things like that. But therefs a down play on the emphasis of curly braces and parenthesis, okay. Therefs a ? the block structure is actually decided, not by curly braces or by parenthesis, but by white space and tabs. And itfs the most frustrating thing to get used to the first day you program in Python because you just naturally put in curly braces, and then you realize you do that, and youfre like, gItfs not gonna work.h So when I show you our first ? show you the first Python function I want to write, youfll see just that itfs just about taxed with very little delimiters. Therefs double quotes and things like that, but therefs no curly braces. Therefs very few parenthesis. Itfs really just very strange to look at the very first time you see it, but then you just get used to it after you program in it for a while. So what I thought Ifd do is Ifd take the Scheme approach to introducing Python and just show you the Python environment, how you can actually talk to Python and get it to announce ? like tell you what the sum of the first four integers are and what happens when you type in a string constant and how Booleans work and things like that, just to you get used to the syntax, and so you have the understanding as to what things look like. And I will do that first. Going to the screen, I will try and get as much of this to fit on the screen as possible. Okay, thatfs not bad at all. Okay, so I launched Python much like we would launch Kawaa, okay. I just type in the word Python. As opposed to Kawaa, Python basically comes on any system that has any Unix backing whatsoever. So the mess, the pods, all of them have Python installed already, so does Mac OS10. So you just go into a terminal and you type Python and itfs probably gonna do what it just did right here. This part should feel very familiar to you. I type in a four and it comes back with a four. I type in hello there, and it comes back with that. It happens to delimit it in single quotes. You can actually use them interchangeable. It allows you to use both double quotes and single quotes because you might want to delimit the entire thing in single quotes if there are a lot of double quotes in your string constant and vice versa. That doesnft mean that a lot of modern languages are paying a lot of effort to because therefs so many quotes that appear in HTML documents and things like that they donft want you to have to backslash and escape everything because that just makes it that much error point of an investment to try and code something like that up. It actually has [inaudible] numbers not surprisingly, and that it is also nice that wefre back to normal addition, okay. I donft want to say that this is conceptually confusing, but whatever. Wefre used to end fixed notations, so Python went with the in face approach. As far as the Boolean constants are concerned, true is true, false is false. True and true is true. You actually spell out the keywords, okay, kind of like you do in Scheme, but theyfre in face again. If you want to ? Ifm trying to think of what else. The one thing about strings that are interesting is that there really is no direct support for the notion of a character. Itfs pretty clear that this is a string the way I type this in, whoops, quote A, B, C, D, E, like that, okay. This happens to be the best you can do for isolating an individual character. You just think of characters not so much as characters as you think of them as strings of length 1, okay. Does that make sense? Okay. Numbers themselves are just plain old numbers, but strings, let me do this. Hello starts with HB. Whoops, didnft like that. Oh, thatfs my fault. Hold on a second. Oh, Ifm sorry. Ifm just doing something. Ifm sorry, starts, doesnft like that, sorry. Ifm just messing up here. There it is, okay. Capitalized in Scheme, okay. The one thing thatfs unique, and this isnft unique to Python. Ifve seen this in C sharp and managed C++ as well is that you donft necessarily have to create an object to surround an object constant in order to send messages to it. Does that make sense? I certainly could have done this. Greeting is equal to hello, and then done something like this, greeting dot starts with worth, that right there. And thatfs the way you would have taken ? would have done it in C++ or Scheme or something like that, but Python is just smart enough to know that if youfre sending a message to something and itfs actually a constant, it can tell by looking at the constant what data type it is, so it can just build the synonymous object around it so that it really can receive these messages. Does that make sense to people? Okay, so as far as all the primitive types, Ifve more or less touched on everything I wanted to. With regard to Booleans, you have and, or, and not spelled out. That shouldnft surprise you. True and false are the actual Boolean constants. I will do this. You may look at that and you may say, gOkay, that doesnft make sense.h But do you understand that as opposed to pure Scheme where everything evaluates to something, that thing right there didnft evaluate to anything. It had the side effect of actually associating the number five with X, but X equals five didnft print anything out. Does that make sense? Thatfs because it evaluates to this constant called none, which is basically the equivalent of void from C and C++, which means therefs really no side effect or nothing of interest in the evaluation of it, okay. Does that make sense? So you can still do this, okay, but then it adopts a whole different set of functionality in order to propagate the assignment. But the overall assignment and actually evaluates to this constant called none. And none really doesnft mean very much except that it doesnft have anything printable about it, okay. So therefs that the ? as far as strings are concerned, there are a whole set of methods on the string class. Itfs certainly a superset of the set of string methods you see in traditional languages like C++ and Java. Python recognizing that when it was written, it was intended to be something of a web savvy language. There are some very like very strange methods that exist for the string class. Let me see if I can do some of them. Hello, there. Capitalize right there. Thatfs a strange method, but it just realizes that a lot of things get printed sentence wise as part of HTML documents, so rather than actually having you go through and uppercase everything that comes at the front of a string, it just has built in code that does this, okay. It has some really weird stuff. Let me see if I can do this. Is title, it comes back with false. So a really weird predicate method to be built into the language, okay. It really just analyzes a string and it goes through the entire thing and sees whether or not every single word thatfs delimited by white space or by the boundaries of the entire string happens to begin with a capital letter. So something like this as if itfs a movie title ? like clearly Iron Man is a title. And Ifll go back, donft worry. Just like that comes back with a true because itfs clearly the name of the movie, like it just knows, okay. Does that make sense to people? Now Ifm not clear what the motivation is to why these things were specifically included in there except for the fact that they just wanted to have ? maybe it must have been the case that they just saw these types of things being written several times in other languages and said, gYou know what? Letfs just take care of it. Why make them rewrite it again? Wefll just put it into the core language, okay.h Does that make sense? Now as far as writing functions is concerned, itfs a little difficult to do at the command prompt for the same reasons itfs difficult to write a Scheme function at the command prompt. I do want to introduce one other data structure before I write a function, okay. Let me show you what a list looks like. This is a list constant just like that, okay. The square brackets actually mean something. It means not only is it a list, but itfs gonna be a mutable list. So I can do something like this, small nums is equal to one, two, three, four, five, six, just like that. And then I can ask what small nums of zero is, okay. And that doesnft surprise you. I can also do that this is the fun part. What do you think that means? Youfre right because it actually understands that it is just ? therefs no reason to buy a search from the front in terms of the syntax thatfs available to you, so it recognizes that you might be interested in the last character, but you donft want to do it like this where you do lin of small nums minus one. Ifm not even sure this works. Yeah, it does, okay. Lin Ifve used on strings. I havenft used on list before. You could do it that way, but then it actually has to manually count the entire thing, whereas internally they could just optimize operator square bracket, the C++ terminology to just have access to both sides, and when therefs a negative number to just start searching from the back instead. Okay, this is ? I mean the neat part. What I can do is I can do that right there and I can get sublists. Does that make sense to people? Okay, so that wants everything from index zero up through, but not including index three. If I do this, thatfs basically the equivalent of that right there. If for whatever reason this programmatically comes up, I can get the empty sequence, everything from index zero up through but not including index zero is just basically the empty list. If Ifm interested in the last ? the end of the list, I can just punt on the thing that follows the colon. It basically says negative two up through the end of the string. Does that make sense to people? If I want everything but the last two characters, I can do something like this, okay. Does that make sense? So itfs the syntactic sugar that was introduced to the language because this language is much younger than any of the other languages wefve studied. Scheme is really the oldest of all the languages wefve looked at, at least in this class. Scheme, wefre talking like late 1940s, Ifm sorry, late 1950s, early 1960s when there was a lot of research circulated around it. It was really introduced to kind of emulate the lambda calculus and come up with an implementation of that, and it was used for symbolic programming and larger programming and things like that. C was invented for the purpose of implementing Unix. Itfs like they wanted access to the hardware. They wanted some form of abstraction. They thought it was brilliant level of abstractions at the time, Ifm sure. We know better now, but it actually provided a much cleaner syntax than what was otherwise available to those implementing operating systems, which was assembly code, okay. You had to get down there in the nitty gritty of the hardware. Itfs hard to do that in Fortran or Scheme, so they actually ? they. Those who worked on Unix invented the C language with the intent of actually making it easier to build the OS. C++ was invented late e70s, early e80s. It became fashionable around 1990. It was initially used as C with objects, and then a lot of effort was put into the implementation to introduce templates and a string class and all of those things that youfre now familiar with. But wefre talking with a language that at this point is 27, 28 years old in terms of specification, okay. Python is probably 13, 15 years old, okay. It didnft come into any level of popularity until year 2000, 2001 is when I started to hear about it. That doesnft mean that itfs all of the sudden popular only because Ifve heard about it, but when I was doing a lot more consulting work at the turn of the century during dot com era, I heard of a lot of big name companies starting to use Python. Google is the one specifically that I remember hearing about so much that people that went there to work said, gYou know what? You should really teach Python in 107 instead of Scheme.h Ifm like, gNo, I like Scheme better. I think itfs cooler as far as illustrating a paradigm.h But Ifm getting to the point where Python is something we have to pay attention to because it has that much more of a presence in industry, okay. If you want this, just another neat little feature of list, you can do this with strings as well. Hello, there. See if this works. Oops, sorry about that, totally can. Okay, it emphasizes the fact that it really is a sequence of characters, something of a list. It turns out as an immutable list of characters. Strings are immutable like they are in Java. The one thing thatfs really fun, let me just do this. List ? I shouldnft do that. Letfs do SCQ is equal to ? letfs do zero, one, two, three, oops, canft count. Do that right there, okay, and I want to go back and correct the problem, okay. Do you understand that the part where there should be a four is actually the sublist of SCQ and I want to say itfs this right here. I may be messing this up, but I donft think I am. Ifve just identified the splice within the list thatfs missing the four. Does that make sense? Okay. So what this is gonna do is it basically comes up with an L value and identifies the sequence in the list that is synonymous with four, up to, but not including four, which is this empty region between the three and the five, and you can actually assign to it. Does that make sense? I hope Ifm not messing this up, but wefll see in a second. There you have that, okay. Now it turns out this is just cool. Itfs not anything you canft do in Java or C++ or Scheme already. Itfs just that itfs more gracefully handled by the syntax that youfre seeing right here, okay. Thatfs just the product of it being a more modern language. It can learn from all the other languagefs mistakes and know where a program was cumbersome before and just try to come up with some better solution now, okay. There is a variation on the list. When I do this, SCQ is equal to five gate ? whoops, commas are important now. When I do that, I actually have the option of taking sequence and saying, gYou know what? I have fives. Ifm gonna replace it with a 14.h And so wefre dealing with a clear script of whatfs called a mutable list. I just call it that. There is a variation of the list which was immutable. I do this. Itfs also considered to be a list, but the parenthesis, for whatever reason, I donft know why theyfve dumped the parenthesis, but that marks ? I mean is considered to be an immutable list, a collection of data that is read-only. So if I do this, thatfs all fine and dandy, but if I try to do this, it freaks, okay. Itfs actually not called a list. Itfs called a tuple. Ifm reading that right now. And but itfs clearly marked behind the scenes as read-only. It just wanted to be able to make the distinction between something that can be updated while the program is running and something that is supposed to be just this bundling of data in some predefined sequence that canft be updated at run time. So itfs just basically this one little gesture toward const or final in the language, okay. Does that make sense? Okay. So as far as lists are concerned, they are in fact objects. You havenft seen any object oriented flavor to this yet, but if I do SCQ is equal to ? letfs just do some strings, which is what these really are. SCQ append D works, okay. Does that make sense to people? Okay. So therefs that ? therefs a whole list of list methods. Therefs actually lists, therefs sets, therefs dictionaries. Theyfre all these things theyfll talk about in a few minutes. But let me just go ahead and just write some code that does something algorithmic. Itfs not gonna be very sophisticated, but I just want to show you what the things look like. I do have to kill the computer for a second. I will come back and show you how it works afterwards. As far as languages go, this language is more like the make file than anything Ifve ever seen before, and itfll be clear in a second when I actually write some code. I want to write a ? I think a fairly juvenile function in terms of algorithm, but I want to illustrate the syntax. I just want to write a function thatfs in the handouts called gathered divisors. So I want it to be able to work like this. I want to be able to pass, letfs say, gather divisors, and I want to be able to pass in something like 24. And I want it to be able to spit back 1, 2, 3, 4, 6, 8, 12, and 24 is fine with me, okay. I want it to accumulate all the things that evenly divide into the number thatfs specified, Ifm sorry, all the positive integers that divide evenly into it. Keyword is def, okay. Just know INE was too much work to include that, so we just go with the EF, gathered divisors. Ifll just call it number, and therefs a colon right now, which means itfs very clear about that. Everything that follows, any line that follows this one that is indented is under the jurisdiction of the entire definition, okay. Does that make sense to people? So what I want to do is I want you to just assume that the number is a positive number. Therefs some error checking in the handout. Ifm not gonna worry about that right now. What Ifm gonna do is Ifm going to set divisors equal to this list constant. There are no semi-colons. I almost put a semi-colon there. Itfs very hard not to put a semi-colon, okay, because you put a semi-colon in every other imperative language other than Scheme. It just knows that the lines end and now backslash N means semi-colon, okay. More so than any other language this space right there, this tab right there, okay, is absolutely required because if it started right there, it would have not only marked a statement that said divisors is equal to the empty list. It would have implicitly marked the end of anything associated with this definition right here, okay. So all the white space that you see in the handout in the definition of this function, every single one of those, itfs not four spaces, itfs actually a tab. It may be stores as four spaces by the editor, but itfs rehydrated into a tab or the equivalent of a tab when you actually load the file again, okay. Not surprisingly, what I want to do is I want to look over every single thing between 1 and 24, okay, and inclusive 24. And I want to decide whether or not the number defies it evenly. Ifm speaking like youfre all 106A programmers. I know you know how to do this part. You just may not know the syntax within Python to do it. This is the way you do it. Ifll say dif in range. I will say zero. I will say number plus one plus one. It looks very for loopish. It certainly is. It has a technically different approach to the way that it generates for loops. Youfre used to four I is equal zero, I less than ten, I++, and therefs a test that is checked with every single iteration to see whether or not you should continue. For loops are almost always expressed ? Ifm sorry, they are always expressed in terms of what are called iterables. That means that the object of the N key right here has to be something that evaluates to a list of things, okay. Now this range function is a built-in. Ifll explain ? Ifll type it on the console in a second. But this actually evaluates to the list. I donft mean zero here. I mean one. It evaluates by default to every single number between and including this one up to, but not including that one right there, which is why I have the plus one there. So this actually evaluates to the list, one, two, three, four, all the way up through number, which in the context of this thing would be 24, and we want 24 in there, okay, because I want it to appear in the result. Therefs an optional third argument right here that can be the step amount. By default, itfs one, which means just include every number in counting up sequence. If I put a ten there, it just would have like done 1 and 11 and 21, etcetera. You can even make it negative if you want to, if you want to generate a list where itfs counting down from some high number to a low number, okay. This idiom right here, itfs technically different than the for loop youfre used to. It just iterates and it associates div with every single number in this iterable, okay. So in the first iteration itfs associated with a one and a two and a three, but for different reasons, okay. If therefs some implicit ? therefs no implicit plus equals one or plus plus coding behind the scenes. Itfs incidental that the numbers are sequential because thatfs the way we constructed the iterable. Does that make sense? Okay, so therefs that. So with each iteration, this is the easy part. If itfs the case, I need a tab there. I donft need parenthesis. It doesnft cause problems, but you donft need them. If itfs the case that number mod div equals equals zero, then I want to take divisors and I want to append whatever div is on this particular iteration, okay. And I have no semi-colons. When I come right here Ifm gonna just return whateverfs accumulated over the course of that iteration divisors. There is no ambiguity as to where the if test ends. Ifm sorry. Therefs no ambiguity as to whatfs under the jurisdiction of that if test, and therefs no ambiguity as to whatfs under the jurisdiction of that for loop, okay. The fact that this came all the way back over here means that itfs a peer in the sequence of statements right here and this marks the end, not only of this if jurisdiction, but also of the for jurisdiction, okay. If I had moved this over one tab, it would not have been under the jurisdiction of the F, but it would have been under the jurisdiction of the for, which means that it would have returned after the first iteration was over, okay. Make sense? Okay, so therefs that. Ifm going to store this in a file called divisors dot PY. The dot PY is more required than youfd think. Itfs actually not required, but I think every single Python file Ifve ever seen ends in dot PY, okay. So what I will do, so Ifll bring the computer back. Ifm doing okay on time. This is good. And I will control the exit because I want to see where I am. Thatfs not where I am right now, CD devel, CS107, Python examples, okay. Let me just ? I have a better idea. This more divisors right there, you see the gathered divisors right there, so therefs clearly some codes in this file that looks very much like the code I just put on the board. Let me open it in a slightly prettier environment. Text Mate is the coolest little program ever. Does anybody use Text Mate? Itfs great. Okay, so herefs this. Letfs just focus on what is visible, nice soft autumn colors, okay. I actually did not include the 24 in my implementation here, but I included it on the one on the board, but conceptually itfs exactly the same. The weirdest thing about this is you see this triple double quote at the front. If the very first statement in a function definition like I have here is a string, itfs understood to be a documentation string. Ifll show you how you can actually find that in the run time interpreter that I was showing before. The triple quote means that the string Ifm about to present is actually expected to appear over multiple lines, so donft give me a problem because I donft put a double quote at the end, okay. Does that make sense? Okay, so therefs that. As far as this is concerned, thatfs the entire function. I have some other code in here. I didnft put this in the handout, but there is prime. Therefs scattered primes, whatever. Itfs the same exact idea. So thatfs the entire thing. Therefs no real analog to this in C. Therefs a little bit of an analog to it in C++ where we have name spaces. Youfre much more familiar with this from Java where you import packages, okay. Do you understand what Ifm talking about? Okay, therefs a little bit of that in Python as well. The fact that I called this gathered divisors, of course thatfs relevant if I want to actually call this function. The fact that itfs inside this thing called divisors dot PY means that itfs inside a module called divisors, okay. So that when I come back and I quit this and I go to a directory like that and I relaunch Python. Actually, Ifm sorry. I didnft mean to do that. CD devel, it says 107 Python examples. Okay, Ifm inside this directory that has all these modules of code. Ifm gonna go ahead and invoke Python. And if I try to call gathered divisors 24, itfs like, gWhat are you asking me? Ifve never heard of gathered divisors. Itfs not in the language. Yeah, it may be in any one of the 15 trillion files in the world, but you have to tell me which one itfs in.h Therefs a couple of ways to do it. Import divisors does that. That is relative to the current execution path of the current path where you ? Ifm sorry, the current directory backing the Python run time. It basically goes in an digests everything inside the Python file, so itfs the equivalent of the load statement from Scheme, okay. And you say, gOkay, thatfs great. I can just do gathered divisors, and now it will work.h And the answer is no, it will not. The reason is just because youfve digested all the material that was inside the divisors module, yeah, the divisors module, doesnft mean that all of the sudden all of the things that were defined in there are the master copies of code attached to all those symbols. So if you really want to invoke the gather divisors, therefs two ways to do it. The more clumsy way is this. Divisors dot gather divisors, oops. Did I actually get that right? Yes, 24. Pray with me, yes. Okay, so basically the module is the name space, okay, and you have to frame by default all functions you invoke in terms of that name face so it knows specifically which gathered divisors youfre talking about. You may think thatfs ludicrous, but in our real system, youfre gonna have presumable, letfs say, between 1 and letfs say 4,000 Python files that are all contributing to a system. You have to make sure that no two functions are named the same thing because if they are you want ? but if there are, you have to qualify which one youfre referring to by actually including the packet name inside. Now if therefs no danger of ambiguity, you can do this. Let me control D and start over. You can say from divisors, import, gather divisors, and then all of a sudden gather divisors is a top-level function name. So itfs just syntax. Itfs all in the handout. Itfs not really the emphasis, but Ifm just leading you through the full example and hitting on everything that I think is important, okay. Does that make sense to people? Okay, now you know how in Scheme that you modeled everything in terms of lists, and if you had a struct, youfd say, gOh, wefll just use lists.h And the CAR of the list is the name and the CAR of the CDUR is the GPA. You just had to remember the actual structure that was imposed in your lists. Python you don't need to do that. You can elect to do that if you want to, but most people use classes, which Ifll talk about a little bit more on Friday how they work. Theyfre not complicated. Itfs just that we really do have better modeling schemes available to us in Python because itfs just a more modern language and they included those things inside of it. What I thought I would do here is I would pretend that we havenft done that stuff, and I will introduce you to, I think an even more central data structure to Python than anything else involving lists or sequence or tuples for that matter, okay. There is a ? the notion of what is called a dictionary. Ifm very careful to use the word dictionary in Python because that is the replacement word for map. You can use map and if wefre talking about Python, people would know what youfre talking about, but dictionary is the operative word for the primary data structure in Python. If I do this, letfs say Ifm student, and right now itfs not a very interesting student. I use square brackets and parenthesis for lists. I use curly braces to delineate all of the content of a dictionary constant. And when I do that, all it means is that the student is an idea at the moment, okay. I can do this though. Student name is equal to letfs say Linda, okay, and therefs that. And now all of the sudden student has grown quite a bit, okay. Make sense? I can do this. GPA is equal to 3.98. Go, Linda. And then we have that, okay. Does that make sense? If I want to say I forgot her GPA I can do this and Ifm reminded, okay. Interesting to know is that the dictionary is completely backed by a hash table, okay. And itfs a very small hash table, and when I say small, I meant the number of buckets is actually very, very small initially because most of these dictionaries imitate structs and classes and objects in Python. In fact, objects are really dictionaries with just a little bit more embedded inside of them. If I want to update for GPA because itfs just not high enough, thatfs Harvard. That right there obviously updates the dictionary in place, okay. The reason Ifm talking like this is because the dictionary is easily the most malleable, easily manipulated data structure in Python as opposed to lists in Scheme where you pass around lists and you functionally manipulate them to create new lists. Python, at least to the extent that Ifm gonna have you exercise it in Assignment 8, really just deals with strings, which you already have enough information about, and dictionaries, okay. Therefs one thing I forgot to mention last autumn when I taught Python for the very first time, and it impacted people on Assignment 8, so I just want to say it right now before yfall disappear for he day is that all of these objects, and I say objects loosely. Anything, any aggregate data structure like a list or a tuple or a dictionary, theyfre all passed around by reference. So if you write a function that takes a dictionary, it just makes a shallow copy of it. Any changes that you make within the function are reflected in the original. Does that make sense to people? Thatfs a huge point for the purposes of getting through Assignment 8 because youfre gonna use one of these dictionaries to keep track of a master set of information that accumulates through some intense recursive algorithm, okay. And it just involves some caching of previously computer information so you donft make the same recursive calls several times, okay. So thatfs why I wanted to say that right there. I think itfs pretty clear that the dictionary right here is heterogeneous in the fact that it really doesnft impose any requirements at all on what the values need to be, okay. So I have a string attached to the field called name. I have a double or floating point attached to the thing called GPA. If I wanted to do this, share, and therefs that. Whoops, what did I do wrong? Oh, Ifm sorry. I meant an array. Therefs clearly order, student, and then all of a sudden you have this third thing inside here. So you can attach anything you want to these things. The fact that it was printed first just means it happened to ? that friends happen to hash to a lower value. Thatfs really what it is, okay. If I want to bundle all of my ideas, but I donft have any at the moment, I can do that. So therefs this one thing where one of the keys happens to be associated with an interdictionary, okay. Does that make sense to people? Okay, so it is really free form. In fact, let me just do something like this, playground is equal to initially itfs empty. I donft recommend this. Ifll show you the problem with this in a second. It actually doesnft have a problem, okay. So you can have things that are non-strings. It just ? anything that is hashable can actually appear as the key inside a dictionary. I donft recommend actually mixing them. I would stick with strings, okay, for the keys, but itfs a freefall as to what value you want to attach to those things, okay. Does that make sense? Now come Friday I want to do a lot of things. Ifm going to jump ahead to the lecture next week. I forgot that itfs ? I thought it was gonna cover like this entire handout today and Ifm just not ? I just didnft at all. I want to talk about a lot of things about Python specifically that are interesting from a language standpoint. The libraries are certainly interesting, more so than even Java, which you have some familiarity with. It has incredible support for XML processing, for HTTP processing, for building a web server, things like that, just amazing that you can really ? of course itfs rudimentary, but you can build a web server in about 15 lines of code, and Ifm not overstating it. And itfs not very sophisticated. It just does the special files and it doesnft do anything clever about like compiling pages or building pages dynamically, but if you just want to fetch pages behind the scene, you can use Python and about 10 lines of code, 15 lines of code, do exactly that, okay. Thatfs usually a statement that the libraries are really taking care of something and theyfre taking care of it very, very well, okay. On Friday I want to show you a little bit more about dictionaries. I have this very dense example, but nonetheless, I think representative of how Python would solve the random sentence generator problem that you all solved for Assignment 1, okay. The difference is that you actually prepare the grammar and you frame it in terms of a dictionary, okay. You may think that itfs cheating that youfre getting the data structure in memory, so you donft actually have to worry about reading it in from a file and parsing it and looking for the angle brackets and things like that. We didnft have the option in C to do anything else, okay. We donft have data structure constants that you can actually very easily assign to variables in C. You have to build them up out of raw deserialized data, okay. In Scheme and also in Python, functions and data ? Ifm sorry. Functions like the ability to express a constant carried beyond just the set of integers and the set of strings. You can define list constants. You canft do that in C or C++, okay. You can define tuple constants and you can define dictionary constants. So you can just elect to format the RSG grammars in something that kind of looks like a Python dictionary so that when you set it equal to a variable called grammar, itfs loaded, okay. Does that make sense? And then you can just write the recursive code and frame it in terms of this variable that you know happens to be a Python dictionary. So you can use operative square bracket and you can assign things to it and you can make random selections from the list of options that are available and associated with each non-terminal, okay. And Ifll go over that. Itfs in the handout, but I think itfs very nice to look at. It also introduces map, this Scheme functional language thing as to how that can work. Okay, so with just a little bit of work you are more than outfitted to tackle this Assignment 8. Even if you struggle on the first like 30 minutes to an hour just to get the syntax down, I think you will recognize that itfs not intended to be a lot of work. Itfs just about getting used to the Python idea, living without a compiler or an interpreter that does very much checking for you. Okay, I will see you all on Friday.