CS50 2020 - Lecture 6 - Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] all right this is cs50 and this is week six and this is again one of those rare days where in just a bit of time you'll be able to say uh that you learned a new language and that language today is going to be this language called python and we thought we'd begin by introducing python by way of some more familiar friends so this of course is where we began the course back in week 0 when we introduced scratch a simple program that quite simply says hello world and then very quickly things escalated and became a lot more cryptic a lot more arcane and we introduced c and syntax like this which of course do the exact same thing just printing out hello world on the screen but with the requirement that you understand and you include all of this various syntax so today all of this complexity all of the the syntax from c suddenly begins to uh melt away such that we're left with this new language called python that's going to achieve the exact same goal simply with this line of code here which is to say that python tends to be more accessible it tends to be a little easier but that's because it's built on this tradition of having started as humans years ago building these low-level languages like c realizing what features are missing what some of the pain points are and then layering on top of those older languages new ideas new features and in turn new languages so there are dozens hundreds really of programming languages out there but there's always a subset of them that tend to be very popular very in vogue at any given time python is among those very popular languages and it's the third of our languages that we'll look at indeed at this point in the term so let's go ahead and introduce some of the syntax of python really by way of comparison with what we've seen in the past because no matter how new some of today's topics are they should all be familiar in the sense that we're going to see loops again conditions variables functions return values there's pretty much just going to be a translation of features past to now features present so this of course in the world of scratch was just one puzzle piece or a function whose purpose in life is to say hello world on the screen in week one we translated this to the more cryptic syntax here key details being that it's printf uh that you have to quote the string hello world you have this backslash n to record represent a new line character and then of course this kind of statement has to end with a semicolon the equivalent line of code today on out in this language called python is going to be quite simply this so it looks similar certainly but it's now print instead of printf we still have the double quotes but gone are the backslash n as well as the semicolon so if you've been kicking yourself all too frequently for forgetting stupid things like the semicolons uh python is will now be your friend well let's take a look at another example here how we might go about getting user input as well well here notice that we have a puzzle piece called ask and it says ask what's your name and wait and the next puzzle piece said whatever the human had typed in preceded with the word hello in c we saw code like this string answer equals get string what's your name and then printing out with printf hello percent s plugging in one value for the other in python some of this complexity is about to melt away too and in python we're going to see a little something like this so no longer present is the mention of the type of variable no longer present is the semicolon at the end and no longer present is the percent s and that additional argument to print so in fact let's go ahead and see these things in action i'm going to go ahead and go over to cs50 ide here for just a moment and within cs50 ide i'm going to go ahead and write my very first python program and to do that i'm going to go ahead and create a file that will initially called hello.pi much like in the world of c python programs have a standard file extension being dot pi instead of dot c and i'm just going to do what i proposed was the simplest translation i'm just going to go ahead and say and print hello comma world i'm going to save my file and then i'm going to go down to my terminal window and in the past of course we would have used make and then we would have done dot slash hello or the like but today i'm quite simply going to run a command that itself is called python i'm going to pass in the name of the file i just created as its command line argument and voila hitting enter there is my very first program in python so that's pretty powerful let's go ahead and create this second program that i proposed a moment ago instead of just printing out hello world the whole time i'm also going to go ahead this time and give myself a variable that i'll call answer i'm going to go ahead now and get input from the user and i'm going to go ahead and use the familiar get string that we did cnc i'm going to go ahead and ask what's your name question mark i'm not going to bother with a semicolon but down here i'm going to go ahead and say print hello comma and then a space inside of the quotes and instead of doing something like percent s i'm actually going to go ahead and just do a plus operator and then literally the word answer but the catch is that this isn't going to work just yet this isn't going to work just yet because get string it turns out just like it doesn't come with c it also doesn't come with python so i need to do one thing that's going to be a little bit different from the past instead of hash including something i'm going to literally say from cs50 import getstring so in the world of c recall that we included cs50.h which had declarations for functions like getstring and getint and so forth in the world of python we're going to do something similar in spirit but the syntax is just a little different we're going to say from cs50 which is our python library that we the staff wrote import that is include a function specifically called getstring and now any errors that i might have seen a moment ago on the screen have disappeared if i go ahead and save this file and now do python space hello dot pi and hit enter now i can go ahead and type in my actual name and voila i see hello comma david so let's tease apart what's different about this code and consider what more we can do after this so again notice on line three there's no mention of string anymore if i want a variable i just go ahead and give myself a variable called answer the function is still called getstring and it still takes an argument just like the c version but the line no longer ends with a semicolon on my final line of code here print it's now indeed print instead of printf and then this is new syntax but in some sense it's going to be a lot more straightforward instead of having to think in advance where i want the percent s and my placeholder this plus operator seems to be doing something for me and let me go ahead and ask a question of the group here what does that plus operator seem to be doing because it's not addition in the arithmetic sense if we're not like adding numbers together but the plus is clearly doing something that gives us a visual result any thoughts from uh peter what's this plus doing it's concatenating strings yeah it's concatenating strings which is the term of art to describe the joining of one string and the other so it's quite like therefore scratch's own join block we now have a literal translation of that join block which we didn't have in c in c we had to use printf we had to use percent s python is going to be a little more user friendly such that if you want to join two strings like hello comma space and the contents of that variable we can just use this plus operator instead and the last thing that we had to do was of course import this library so that we have access to the getstring function itself well let's go ahead and take a tour of just some other features of python and then dive in primarily to a lot of hands-on examples today so recall that in uh the example we just saw we had this first line of code which gets a string from the user stores it in a variable called answer we had the second line of code which is peter notes concatenates two values together but it turns out even though this is definitely more convenient than in c in that you can just take an existing string and another and join them together without having to use format strings or the like well it turns out there's another way there's frankly many ways in languages like python to achieve the same result and i'm going to go ahead and propose that we now change this line here to this funky syntax so definitely ugly at first glance and that's partly because this is a relatively new feature of python but notice that in python can we use these curly braces so curly braces that we have used in c to plug in an actual value of a variable here so instead of percent s python's print function uses these curly braces that essentially say plug in a value here but there's one oddity here you can't just start putting curly braces and variable names into strings that is quoted strings in python you also have to tell the language that what follows is a formatted string so this is perhaps the weirdest thing we've seen yet but when you do have a pair of double quotes like i have here prefixing it with an f will actually tell the computer to format the contents of that string plugging in values between those curly braces as opposed to literally printing those curly braces themselves so let me go ahead and transition to my actual code here and try this out instead of using the concatenation operator as peter described it this plus operator let me literally go ahead and say hello comma answer initially so this is probably not going to be the right approach because if i rerun this program python of hello.pi it's going to ask me what's my name i'm going to type in david and it's going to ignore me altogether because i've literally hard coded hello comma answer but it's also not going to be quite right to just start putting that in curly braces because if i again run this program python of hello.pi and type in my name now it's going to say hello squigglybrace answer so here is just a subtle change where i have to tell python that this type of string between the double quotes is in fact a formatted string and now if i rerun python if hello.pi and type in david i now get hello david so it's marginally more convenient than c because again you don't have to have a placeholder here a placeholder here and then a comma separated list of additional arguments so it's just a more succinct way if you will to actually introduce more values into a string that you want to create these are called format strings or for short f strings and it's a new feature that we now have in our toolkit when programming with this new language called python well let's take a look at a few other translation of puzzle pieces to see and then turn to python and then start building some programs of our own so here in scratch this was an example early on of a variable called counter initializing it to zero in c in week one we started translating that to code like this int counter equals zero semicolon and that gave us a variable of type int whose initial value was zero in python the code's gonna be similar similar but it's gonna be a little simpler still notice that i don't have to in python mention the type of variable i want it will infer from context what it is and i also don't have to have the semicolon there so counter equal 0 in python is going to give you a variable called counter and because you're assigning it the value zero python itself the language will infer that oh you must mean this to be an int or an integer what else did we see in scratch change counter by one so this was a way of increasing the value of a variable by one in c we had a few different ways to implement this we could say counter equals counter plus one it's kind of pedantic it's kind of long and tedious to type so instead we had some shorthand notation that allowed us to do it uh this way instead in pi in c we were able to do counter plus equals one and that was going to achieve the same result well in python we actually have a couple of approaches as well we can much like in c say it explicitly like this but just omit the semicolon so counter equals counter plus one the logic in python is exactly the same as in c and as for this shorthand notation this also exists in python again without the semicolon the one thing that does not exist in python in at this point in the story is that fancy counter plus plus syntax or i plus plus that syntactic sugar that made it even more succinct to just increment a variable unfortunately does not exist in python but you can do counter plus equals one or whatever your variable happens to be well what else did we see in scratch and then c recall this we introduced of course conditions pretty early on and those conditions use boolean expressions to decide whether to do this or this other thing or something else altogether in c you we converted this to what looked kind of similar and indeed the curly braces kind of hug the printf line just like the yellow condition here hugs the purple say block and we had parentheses around the boolean expression like x less than y we again used printf inside of the curly braces which had double quotes a backslash n for a new line and a semicolon python nicely enough is going to be sort of identical in spirit but simpler syntactically what python is going to look like henceforth is just this so the parentheses around the x less than y go away the curly braces go away the new line goes away and the semicolon goes away and here you see just a tiny example of evolution of humans programming languages right if you and i have been frustrated for some time about all the stupid semicolons and curly braces all over the place it makes it harder in some sense for your code to read let alone being correct humans decided when inventing new languages that you know what why don't we just say what we mean and not worry as much about all of this syntactic complexity let's keep things simpler and indeed that's what we see here is one example in python but there's a key detail if any of you have been in the habit when writing code in c of being a little sloppy when it comes to your indentation and maybe style 50 is constantly yelling at you to add spaces add spaces or remove spaces or lines well in python it is now necessary to indent your code correctly in c of course we cs50 and a lot of the world in general recommend that you indent your code by four spaces typically or one tab in the context of python you must do so if you accidentally omit these spaces just to the left of the print statement here your python code is not going to run at all the python program just won't work so no more sloppiness python is going to impose this on you but the upside is you don't have to bother including the curly braces what about a more complicated condition where there's two uh paths you can follow if or else well in this case in c we translated it pretty straightforwardly like this again parentheses up here curly braces here and here backslash n backslash n and semicolon you can perhaps guess in python that this is going to get a little more compact because boom now we don't need the parentheses anymore we do need to indent but we don't need the curly braces we don't need the new line and we don't need the semicolon so we're sort of shedding features that can be taken now for granted but what about this example in scratch when we had a three-way fork in the road if else if else well in python or rather in c we would have translated this like this and there's not much going on there but it's a pretty substantive number of lines of code some 12 lines just to achieve this simple idea in python notice what's going to go away here is again those parentheses again those curly braces again the backslash n and the semicolon there's only one oddity here there's only one oddity what looks wrong or weird to you maybe what looks like a typo to you and i promise i haven't screwed up here maybe elsewhere but not here uh andrew um i would say the lift instead of elsif is different syntactically exactly so whereas in c we would literally say else if in python humans years ago decided heck why say else if and waste all that time typing that out if you can more succinctly say l-if as one word e-l-i-f so indeed this is correct syntax here and you can have more of those you can have four uh bran four uh forks in the road five six any number thereafter but the syntax is indeed a little different but it's a little tighter right there's less syntactic distraction when you glance at this code you don't have to ignore as many semicolons and curly braces in the life like python tends to just be a little cleaner syntactically and indeed that's characteristic of a lot of more recent more modern languages like it all right let's take a look at a few other blocks in scratching and turn c in scratch when we wanted to do something again and again as a loop perhaps forever we would literally use the forever block in c we could implement this in a few different ways and we proposed quite simply this one while true print out hello world again and again and again and because the boolean expression never changes it's going to indeed execute forever so python's actually pretty similar but there are a couple of subtle differences so in grain in your mind what this looks like here we have true in parentheses the curly braces the new line the semicolon a lot of that's about to go away but there's still going to be a slight difference notice that we're indenting as i keep emphasizing we no longer have the new line or the semicolon or the curly braces but true and it turns out false now must be capitalized so whereas in c it was lowercase false lowercase true in python it's going to be capitalized false capitalize true why just because but there is one other detail that's important to note both with our loops here as well as with our conditions just as before if i rewind to our most recent condition notice that even though we've gotten rid of the curly braces and we've gotten rid of the parentheses we now have introduced these colons which are necessary after this expression this expression and this one to make clear to python that the lines of code that follow indented underneath are indeed relevant to that if elif or else and we see that same feature again here in the context of a loop well we saw other loops of course in scratch when we wanted to do something a finite number of times like three we would repeat the following three times in c we had a few different approaches to this and all of them i dare say were very mechanical like if you want to do something three times the onus in c is on you to like declare a variable keep track of how many times you've counted already increment the thing like there's a lot of moving parts and so in c one approach looked like this we declare a variable called i equals zero but we could call it anything we want we have a while block here that's asking a boolean expression again and again is i less than zero is i less than three and then inside of the loop we printed out hello world and using c's syntactic sugar the plus plus notation we kept adding one to i add one to i at one to i until we implicitly break out of the loop because it's of course no longer less than 3. so in python similar in spirit but again some of that clutter goes away i equals 0 is all we need say to give ourselves a variable while i less than 3 is all we need to say there but with the colon then inside of that indented properly we print out hello world and we can't do the plus plus so we minor disappointment but i plus equals 1 increments i so this would be one way of implementing in python the exact same thing a loop that executes three times but we saw other approaches of course in c and there's other approaches possible in python as well you might recall in c that we saw this approach the for loop and odds are you've been reaching for the for loop pretty frequently because even though it looks a little more cryptic you can pack more features into that one line of code in between those those semicolons if you will so same exact logic it just prints out this hello world three times using a for loop instead in python things start to get a little elegant here now it's a little weird at first glance but it's definitely more succinct if you want to do something three times it turns out in python you can use a more succinct syntax for the for loop for i in and then in square brackets a list of values so just as we used in the past square brackets and a few different places to connote arrays and indexing into arrays in the world of python whenever you surround a bunch of values that themselves have commas in between them and you encapsulate them all using square brackets that's what we're going to call in python a list and it's very similar in spirit to an array but we'll call it in the context of python a list and so what this line of code says is for i in012 what does that mean this is a for loop in python that says give me a variable called i and on the first iteration of this loop set i equal to zero on the second iteration of this loop set i equal to one and on the last iteration of this loop set i equal to two for me it just does all of that for you now at the end of the day it actually doesn't matter what i is per se because i'm not printing the value of i and that's totally fine odds are you've used for loops or you did something again and again like printing hello world even though you didn't print out the value of i so technically i could have put any three things in these square brackets if i want but the convention would be just enumerate just like in c zero one two just like a computer scientist counting from zero but this could break down pretty easily this could become very ugly very quickly does anyone see a problem with for loops in python if you have to put in between those square brackets the list of values that you want to iterate over uh noah um if you want to do for example like a thing 50 times you'd have to write out zero one two three four five six yeah my god it would start to look hideous quickly and it's funny you mentioned 50 because in preparing this this demonstration for lecture today i went back to week zero when actually the analog in week zero was to indeed print out hello world 50 times and i thought to myself damn it like this is going to look atrocious now because i literally have to put inside of square brackets 0 1 2 3 4 5 6 7 8 9 all the way to 49 as noah says which would just look atrocious like surely there's got to be a better way and there is while this might be compelling for very short values there's a simpler way in python when you want to do something some number of times we can replace this list of three values with this a function called range that takes an input which is the number of things that you want to return and essentially what range will do for you past an input like three it will automatically generate for you a list of three values zero one and two and then python will iterate over those three values for you so to know as a concern a moment ago if i now want to iterate 50 times i just change the 3 to a 50 and i don't have to create this crazy mess of a manually typed out list of 0 through 49 which of course would not be a very well designed a program it would seem just because of the the length of it and the opportunity to mess up and the like so in python this is perhaps now if you will the most pythonic way to do something some number of times and indeed this is a term of art in the python community long story short technical people programmers they tend to be pretty religious in some sense when it comes to the right way of doing things and indeed within the world of python programming a lot of python programmers do have both opinions but also standardized recommendations that dictate how you should write python code and tricks like this are what are considered pythonic you are doing something pythonically if you're doing it the quote-unquote right way which doesn't mean right in the absolute it means right in the sense that most other people rather agree with you in this sense all right let's see a few final features of python before we now start to build some of our own features in c recall we had this whole list of data types and there are more and you can create your own of course but the primitives that we looked at initially were these bull char double float int long string and so forth in python even though i haven't needed them because i can give myself a variable like a string or an int just by giving it a name like counter or i or answer and then assigning it a value and python infers from what you're assigning it what data type it should be python does have data types it's just what's known in in the programming world as a loosely typed language in the world of c c is a strongly typed language where not only do types exist you must use them explicitly in the world of python you have what's called a loosely typed language which in which types exist but you can often infer them implicitly the burden is not on you the programmer to specify those data types incessantly let the computer figure it out for you so this is our list from c this now is going to be our analogous list in the world of python we're going to have bull still true is and false but capital t capital f we're going to have floats which are real numbers with decimal points we're going to have ins which of course are numbers like negative 1 0 and 1 and so forth and then not strings per se but stirs str and whereas in the world of c there was technically no string type that was a feature offered by the cs50 library which just made more accessible the idea of a char star recall that c has strings and they're called strings but there's no data type called string the way you give yourself a string of course in c is to declare something as a char star and in cs50's library we just gave that char star a synonym a nickname an alias called string in python there are actual there is an actual data type for strings and for short it's called str all right so with that said what other features do we have from python that we can use here well there's other data types as well in python that are actually going to prove super useful as we begin to develop more sophisticated programs and do even cooler things with the language we've seen range already strictly speaking this is a data type of sorts within python that gives you back a range of values by default zero on up based on the input you provide list i keep mentioning verbally a list is a proper data type in python that's similar in spirit to arrays but whereas in arrays recall we've spent great emphasis over the past few weeks noting that arrays are a fixed size you have to decide in advance how big that array is going to be and like last week if you decide oops i need more memory you have to dynamically allocate more space for it copy values over and then free up the old memory like there's so much jumping through hoops so to speak when you want to use arrays and c if you want to grow them or even shrink them python and other higher level languages like it do all of that for you so a list is like an array that automatically resizes itself bigger and smaller that feature now you get for free in the language so to speak you don't have to implement it yourself python has what are called tuples in the context of like math you might have or gps you might have x and y coordinates or latitude and longitude coordinates so like comma separated values tuples are one way of implementing those in python dict or dictionaries so python has dictionaries that allow you to store keys and values or literally in our human world if you have a human dictionary here for instance for english much like a dictionary in physical form lets you store words and their definitions a dictionary in python more generally lets you store any keys and any values you can associate one thing with another and we'll see that this is a wonderfully useful and versatile data structure and then lastly for today's purposes there's these things called sets which if you recall from math a set is a collection of values like abc or 123 without duplicates but python manages that for you you can add items to a set you can remove items from a set python will make sure that there are no duplicates for you and it will manage all of the memory for you as well so what we have uh in the way of functions meanwhile is a few familiar friends recall that in c we use the cs50 library to get chars doubles floats ins longs and strings in python thankfully we don't have to worry about doubles or longs anymore more on that in a bit but the cs50 library for python which you saw me import a few minutes ago does give you a function called get float it does give you a function called getint it does give you a function called getstring that at least for this week's purposes are just going to make your life easier these two are training wheels that we will very quickly take off so that you're only using native python code ultimately and not cs50s own library but for the sake of transitioning this week from c to python you'll find that these will just make your life easier before we relax and take those away too so in c to use the library you had to include cs50.h in python again you're going to go ahead and import cs50 or more explicitly the specific function that you might want to import so it turns out there's different ways to import things they ultimately achieve essentially the same goal you can with lines like this explicitly import one function at a time like i did earlier using get string or you can import the whole library all at once by just saying more succinctly import cs50 it's going to affect the syntax we have to use hereafter but there we'll see multiple ways of doing this in our examples here on out you can also simplify this a bit and you can import a comma separated list of functions from a library like ours and this is a convention we'll see quite frequently as well because as we start using uh popular third-party libraries written by other programmers on the internet they will very commonly give us lots of functions that we ourselves can use and we will be able to import those one after the other by just specifying them here in this way all right let me pause here just to see if there's any questions on python syntax like that's essentially it for our crash course in python syntax we're now going to start building things and explore what the features of python are and what some of the nuances are and really the power of python but first any questions on on syntax we've seen loops conditions variables olivia question or comment um if in a for loop if you want to increment by something besides one but you don't want to explicitly type out the list how would you do that really good question so if you wanted to use a for loop and iterate over a range of values but you wanted that range to be 0 2 4 6 8 instead of 0 two three let me go ahead and go back to that slide from a moment ago and i can actually change this on the fly let me go into that slide which was right here and what i can do actually is specify another value which might be this if i change if i change the input to range to be not one value but two values that's going to be a clue to the computer that it should count a total of three values but it should increment two at a time instead of the default which is one and there's even other capabilities there too you don't have to start counting at zero you can adjust that as well which is to say that with python you're going to find a lot more features come with the language and even more powerfully the functions that you can write and the functions that you can use in python also can take different numbers of arguments sometimes it's zero sometimes it's one sometimes it's two but it's ultimately often up to you good catch other questions will we see sequences like primarily in the for loops or are there other applications where they're very useful sequences in what sense in the sense of ranges or lists or something else um yeah in terms of ranges good question will we use them in other contexts generally speaking it's pretty rare i mean i'm i'm racking my brain now as to other use cases that i have used range for and i'm sure i could come up with something but i think hands down the most common case is in the context of iteration as in a for loop and i'll think on that to see other applications but anytime you want to generate a long list of values that follow some pattern whether it's 0 1 2 or as olivia points out a range of values with gaps range will allow you to avoid having to hard code it entirely and you can actually write your own generator function so to speak a function that returns whatever pattern of values that you want other questions or confusion anything on your end brian from the chat or beyond uh looks like all the questions are answered here all right well let's go ahead now and do something more interesting than hello world because after all this is where programming really gets fun really gets powerful when you and i no longer have to implement those low level implementation details when you had to implement memory management for your hash table or memory management for a linked list or copying values in array we've been spent the past several weeks focusing really on some low-level primitives that are useful to understand but they're not fun to write and i i can see that they might not be fun to write in problem set form and they're certainly not going to be fun to write for the rest of your life every time you want to just write code to solve some problem but again that's where libraries come in and now this is where other languages come in it turns out that python is a much better a much easier language to use for solving certain types of problems among them some of the problems you we have been solving in past problem sets so in fact let me go ahead and do this i'm going to go ahead and grab a file here give me one moment called bridge.bitmap which you might recall from a past problem set this is a beautiful week's bridge down by the charles river in cambridge mass by harvard and this is a very clear photograph taken by one of cs50's team members and in recent weeks of course you wrote code to do all sorts of mutations of this image among them blurring the image and blur i dare say was not the easiest problem to solve you had to sort of look up down left and right sort of average all of those pixels you had to understand how an image is represented one pixel at a time so there's a lot of low level minutiae there when at the end of the day all you want to do is just blur an image so whereas in past weeks we sort of had to think at and write at this lower level now with python it turns out we're going to have the ability to think at a higher level of abstraction and write far less code for ourselves so let me go ahead and do this i'm going to use my mac for this instead of cs50 ide just so i can open the images more quickly this is to say that even though we'll continue using cs50 ide for python and for other languages over the remainder of the course you can also install the requisite software on a mac on a pc sometimes even kind of sort of a phone today to use python and sort of c in other languages on your own devices but again we tend to use cs50 ide during the class so as to have a standard environment that just works so i'm going to go ahead and write though on my computer a program called blur.pi pi of course being the file extension for python programs that my program looks a little different now i've got this black and blue and white window but this is just a text editor on my own personal mac here i'm going to go ahead and do this i need to have some functionality related to images in order to blur an image so i'm going to go ahead and import from a pill library a pillow library so to speak a special feature called image and a special feature called image filter that is to say these are essentially two functions that someone else smarter than me when it comes to image manipulation wrote they made their code freely available on the internet free and open source which means anyone can use the code and i am allowed now to import it into my program because i before class downloaded and installed it beforehand now i'm going to go ahead and do this i'm going to give myself a variable called before and i'm going to call image.open on bridge.bmp so again even though we've never seen this before never used this before you can kind of glean syntactically what's going on i've got a variable on the left called before i've got a function on the right called image.open and i'm passing in the name bridge.bitmap so it sounds like this is kind of like f open in the world of c now notice this dot is kind of serving a new role here in the past we've used the dot operator only for structs in c when we want to go into a person object or into a a node object and we want to go inside of it and access some variable therein well it turns out in python you have things similar in spirit to structs in c but instead of containing only variables or data like name and number like we did for the person struct a few weeks back in python you can have inside of a structure not only data that is variables you can also have functions inside of structures and that starts to open up all sorts of possibilities in terms of features available to you so it seems that i've got this image object this image struck that i've again imported from someone else inside of it is an open function that expresses input the name of a file to open so we'll see this syntax increasingly over the course of today's examples let me give myself a second variable after let me go ahead now and assign to this variable called after the results of calling that before images filter function passing in image filter dot box blur of one now this is a little cryptic and we're not going to spend time on this particular syntax because odds are in life you're not going to have that many opportunities to want to blur an image for which you're going to run or write code but for today's purposes notice that inside of my before variable because i assigned it the return value of this new feature it has inside of it not just data but also functions one of them now called filter and this filter function takes as input the return value of some other function call that long story short will blur my image using a box of uh with a one pixel radius so just like your own code if you implemented blur and c this code is going to tell my code to look up down left and right and blur the pixels by taking the average around them and that's kind of it after that i'm going to do after dot save and i'm going to save this as like out.bmp i just want to create a new file called out.bmp and if i've made no mistakes let me go ahead now and run python of blur.pi and hit enter no error messages so that's usually a good thing if i type ls now notice that i've got bridge.bmp which i already opened blur.pi which i just wrote and out.bmp and if i go ahead and open out.bmp let's go ahead and take a look here's before here's after huh before after now over the internet it probably doesn't look that blurred though on my mac right here a few inches away it definitely looks blurred but let's do it a little more compellingly how about instead of looking one pixel up down left and right why don't we look 10 pixels at a time so we really blur it by looking at more values and averaging more let me go ahead now and run python of blur.pi now let me go ahead and reopen and now you see before and after before and after so what is this to say well here is what problem set four in four lines of code blurring an image so pretty cool pretty powerful by standing on the shoulders of others and using their libraries can we do other things quite quickly notice what i can also do here too is solve a more recent problem let me go over to a different directory where i have an advanced and you can download these files off of the course's website a few files that we wrote before class one is called speller.pie so long story short in speller.pie is a translation from c into python the code for speller.c recall that that was part of the distribution code for problem set 5 and in speller.c we translated it now to speller.pie and in dictionaries and in texts we see the same files as in problem set five two different size dictionaries and a whole bunch of short and long texts what hasn't been created yet is the equivalent of dictionary.c aka now dictionary.pi so let me go ahead and implement my spell checker in python let me go ahead and create a file called dictionary.pi as is again the convention and let's go ahead we have to implement four functions right we have to implement check load size and unload but i probably need like a global variable here to store my dictionary and this is where you all implemented your hash table with a pointer and then linked lists and arrays and all of that a lot of complexity you know what i'm just going to go ahead and give myself a variable called words and declare it as a set so recall that a set is just a collection of values that handles duplicates for you and frankly that's all i really need i need to be able to store all of the words in a dictionary and just throw them into a set so that there's no duplicate values and i can just check is one word in the set or is it not well let's go ahead now and load words into that set i'm going to go ahead and define a function called load that takes the name of a file to open and here is submittedly some new syntax so thus far we've only typed code into the file itself in fact the most striking difference thus far dare say about python versus c is that i have never once even written a main function and that too is a feature of python if you want to write a program you don't have to bother writing your default code in a function called main just start writing your code and that's how we were able to get hello world down from this many lines of code in c to one line in python we didn't even need to have main but if i want to define my own functions it turns out in python you use the keyword def for define then you put the name of the function and then in parentheses like in c you put the names of the variables or parameters that you want the function to take you don't have to specify data types though and again we don't use curly braces we're instead using a colon so this says hey python give me a function called load that takes an argument called dictionary and what should this function do well the purpose of the load function in speller was to load each word from the dictionary and somehow put it into your hash table i'm going to go ahead and do the same read each word from the dictionary and put it into this so-called set my variable called words so i'm going to go ahead and open the file which i can do with this function here in python you don't use f open you just use a function called open and i'm going to assign the return value of open to a variable called file but i could call that anything i want this is where python gets really cool recall that reading the lines from python from the file in c was kind of arduous right you had to use like uh f read or some other function in order to read character after character after character one line at a time well here in python you know what if i want to iterate over all of the lines in a file we'll just say for line in file this is going to automatically give me a for loop that initial that assigns the variable line to each successive line in the file for me it will figure out where all of those lines are what do i want to do with each line well i want to go ahead and add to my set of words that line insofar as each word rep each line represents a word i just want to add to my global variable words that line and that's not quite right because what's at the end of every line in my file every line in my file by definition has a backslash n right that is why all of the words in the big dictionary we gave you are one per line so how do you get rid of the new line at the end of a a string well in c my god we would have to like use malloc to make a copy and then move all of the characters over and then shorten it a little bit by getting rid of the backslash n uh-uh in python if you want to strip off the new line at the end of a string just do r strip to strip characters means by default to strip off white space white space includes the space bar the tab character and backslash n so if you want to take each line and throw away the trailing new line at the end of it you can simply say line dot r strip and this is where strings again in python are powerful because they are their own data type they have inside of them not only all of the characters composing the string but also functions like r strip which strips from the end of the line any white space that might be there uh you know what after this i think i'm done i'm just going to go ahead and close the file and i'm going to go ahead and return true so that's it that's the load function in python open the dictionary for each line in the file add it to your global variable close the file return true i mean i'm pretty sure that my code is probably several lines and certainly many hours shorter than your code might have been for implementing that as well well what about checking right maybe the complexity is just elsewhere well let me go ahead and define a function called check that takes a specific word as input as its argument and then i'm just going to check if that given word is in my set of words well it turns out in c you would probably have to use a for loop or a while loop and you'd have to iterate over the whole list of words that you've loaded using binary search or linear search or like like i'm so past that at this point so many weeks in i'm just going to say if word in words go ahead and return true else return false and that now is my implementation of check now it's a little buggy and i will fix this does anyone spot the bug even if you've never seen python before but having spent hours implementing your own version of check is there some step i'm missing logically there is a bug here does anyone spot what i'm not doing that you probably did do when checking if a given word is in fact in the dictionary a couple people are commenting on case sensitivity yeah case sensitivity so odds are in your implementation in c you probably forced the word to all uppercase or you forced it to all lowercase totally doable but you probably had to do it like character for character you might have had to copy the input using malloc or putting it into an array character for character then using uh a two upper or two lower to capitalize or lowercase each individual letter like oh like that would take forever as indeed it might have so you know what if you want to take a given word and lowercase it just say word.lower and python will take care of all of those steps of iterating over every character changing each one to lowercase and returning to you the new result and indeed this now i would think is consistent with what you did in your example as well well how about size well in size recall that you had to define a function that doesn't take any inputs but returns the number of words in the set of words and i'm going to go ahead here and actually i got my indentation slightly off here let me fix this real fast if you want to return the size of your dictionary or really the number of words in your set you can just return the length of that global variable words done and lastly if you want to unload the dictionary let me go ahead and unload things doesn't take input as well honestly because i've not done any equivalent of malloc i've not done any memory management why you don't have to in python i can literally just return true in all cases because my code is is undoubtedly correct because i didn't have to bother with pointers and addresses in memory management so all of the stress that might have been induced over the past few weeks as you understood the lower level details of memory management now go away not because not because uh it's not happening underneath the hood but because python is doing it for you and i did spot one bug here actually notice i kind of relapsed into c code here i what i should have said here is it's actually file.close so here when i close the file and load i actually have to call file.close because now that function close is associated with that variable for me so again there is memory management happening malloc and free or reallock are all happening sort of for you underneath the hood but what python the language is doing for you now is managing all of that for you that's what you get by using a so-called higher level language instead of a lower level language you get more features and in turn in this case you get all of those problems taken care of for you so that you and i can focus on building our spell checker so you and i can focus on building our instagram filters not on allocating memory copying strings uppercasing things which honestly while it might have been fun and very gratifying the first time you got those things working programming would very quickly become the most tedious thing in the world if anytime you want to write a program you have to think and write code at that low level all right let me go ahead and really cross my fingers that i didn't screw up here and go ahead and run this code so i'm going to go ahead and run python of speller.pie which admittedly i wrote in advance because just like the distribution code in speller we wrote speller.c for you we wrote speller.pie in advance but we won't look at the internals of that i'm going to go ahead and test this on how about something big like shakespeare and i'm going to cross my fingers here and so far so good the words are kind of flying by i'm going to assume they're correct hopefully we'll get to the output and it looks like yeah i think i see some familiar numbers here i've got 143 000 91 words and then down here the total time involved was just under one second so that's pretty darn fast and to be clear i'm using my mac instead of the ide so my numbers might be a little different than in the cloud but 0.90 seconds but you know what out of curiosity let me open up a different tab real quick and let me go ahead and make speller from problem set 5. so i brought in advance our own implementation of speller the staff solution written in c in dictionary.c and speller.c and i've just compiled it with make and let me go ahead and run dot slash speller using the same text on shakespeare so again i just ran the python version now i want to run the c version using the staff's implementation all right wow all right it flew by way faster kind of twice as fast and notice even though the numbers are the same up above the times are not my c version took 0.52 seconds so half a second my python version took point nine or roughly one second so it would seem that my c version is faster my python version is slower why might that be why might that be because i kind of disappointed if we just spent all this time preaching the virtues of python and yet here we are writing worse code in some sense santiago um could it be because um c even though it's low level it it explicitly says tells the computer what to do and so um that makes it a little faster whilst in python it all happens like underneath the hood as you were saying so that could make it a little slower yeah in python you have a general purpose solution to the problem of memory management and capitalization and all of these other features that we ourselves had to implement ourselves in c python has general purpose implementations of all of those but there's a price you pay by using someone else's code to implement all of those things for you and you pay an even greater price by using the type of language that python is in a sense so there's been this other salient difference between using c and using python when i wrote c code i would compile my code from source code into machine code and recall that machine code are zeros and ones understood by the computer's brain the so-called cpu or central processing unit we always had to compile our code every time we changed the source code and then we did like dot slash hello to run the program but every demo thus far in python i haven't used make or clang i have used not dot slash hello but rather python space the name of the program and why is that well it turns out that python is often implemented as what we describe uh with an interpreter so python is not only a language like we've been writing it's also a program unto itself the python program i keep running is an identically named program that understands the python language and what's happening though is that by using an interpreter so to speak to run my programs you're incurring some amount of overhead you're paying a performance price why well computers recall from week zero at the end of the day only understand zeros and ones that's what makes them them tick but i have not outputted any zeros and ones i the human have only been writing python so there needs to be some kind of translation between my python code in this english-like syntax into what the computer itself understands and if you're not going to go through the effort of compiling your code every time you make a change but instead you're just going to run your code through an interpreter as is the norm in the python world you're going to pay a price because someone had to implement a translator for you and in fact there's formal terminology for this in the world of python we have for instance a picture that looks more like this whereas in the world of c we would actually take our source code as input and output first machine code as output and then run the machine code in the world of python thus far i'm writing source code and then i'm immediately running it i'm not compiling it into zeros and ones in advance i'm trusting that there's a program coincidentally called python whose purpose in life is to translate that code for me into something the computer does understand and what does that actually mean in real terms well it means that if i were to think back to an algorithm like this which probably cryptic to many of you though not all might be a spanish algorithm for searching a phone book for someone and suppose that i don't speak spanish at all i might ideally compile this program this algorithm into something i do understand by using a compiler that translates spanish to english like voila this english version much better reading and understanding this i can execute this algorithm pretty fast because i'm pretty good at english but if you only give me the spanish version the source code and you require that i translate it or interpret it line by line honestly that's really going to slow me down because it's like me having to go take like a spanish dictionary and look up every word recognia telefonica all right well what's up i have to look that up what's ghia what's telephonica oh okay pick up phone book got that step one what's step two abra de la mitad telephonica so open to the middle well wait i don't know that spoiler what does that mean abre all right let me look that up and it means open a la mitad uh that means to the middle uh de gea telefonica of the phone book oh that means open to the middle of the phone book so i'm struggling to go back and forth here clearly but it's clearly a slower process and if i keep going de la pahina look at the page looking up translating every line it's undoubtedly going to slow down the process and so that's effectively what's happening for us when we run these python programs there is a translator like a man in the middle so to speak that's looking at your source code and reading it top to bottom left to right and essentially translating each line respectively into the corresponding code that the computer understands so the upside of this is that thankfully we don't have to run make or claim we don't have to compile our code anymore right like how many people here have made a change to an earlier pset in c forgotten to save the file but you rerun the pro sorry you forgot to recompile the file and you rerun it and the program obviously has not changed because you haven't actually not only saved but recompiled it so that stupid annoying human step is gone in the world of python you change your file go ahead and just rerun it reinterpret it you can save that step but the price you're going to pay is a little bit of overhead and indeed we see that here in terms of my python version taking roughly one second to spell check shakespeare and my c version taking only one half of a second so here too i promised in past weeks this theme of trade-offs this is so prevalent in the world of computer science and programming and frankly in the real world anytime you make some improvement or gain some benefit odds are you're paying some price maybe it's time maybe it's space maybe it's money maybe it's complexity maybe it's anything else there's this perpetual trade-off of resources and being a good programmer ultimately is about finding those inflection points and knowing ultimately what tools to use for the trade all right let's go ahead here take a five minute break and when we come back we'll look at other features of python and we'll end ultimately today some with some really uh powerful capabilities back in five all right we are back and uh first a retraction if i may brian kindly pointed out that my answer to olivia and noah's follow-up question unfortunately missed the mark because i was doing things on the fly instead of reading the documentation so let me recall for us this example here where we had the range function returning three values so that code correct that's what gives us the value zero one and two but what i think olivia asked was if you wanted to skip values and for instance do every two digits how do we do that and i unfortunately screwed up the syntax for that providing only two inputs to range instead of three as would be needed here so for instance suppose that we wanted to print out all of the numbers between 0 and 100 inclusive but skipping every other so zero two four six eight so all the even numbers on up through one hundred um we would actually want to do something like this instead we would say for i in range of zero comma 101 comma two why is that well we'll pull up the documentation in just a moment but 0 is where you want to start counting the second value 101 is where you want to stop counting but it is by definition exclusive so we have to go one past the value we care about and then the two the third argument is how many numbers do you want to increment at a time from zero to two to four to six to eight on up through 100 so how could i have figured this out in advance rather than embarrassing myself now well it turns out there is official documentation for python and we'll always link this to you and here there's this search box at the very top and you can see that during the break i was searching for the documentation for range and sure enough if i search for the range documentation at first glance it might seem kind of overwhelming because there's a lot of mentions of something like range in the documentation fortunately the first result here is the one we want and if i click on that you'll see some documentation that's a little cryptic at first glance but what's interesting about this is that range comes in two different flavors and even though i keep calling it a function technically it's what's called the class but more on that another time it behaves for our purposes as a function notice that there's two lines here and they're similar but different the first one specifies that this rain function range function can take one input the stop value so at what value do you want to stop counting so before when we did range of three it stands to reason that by default if you start counting at 0 and you stop at 3 that will get you to use i equals 0 1 and 2. but there's another flavor of the range function which is not the one that i proposed exists there's another that takes in potentially three arguments here or technically two but it works in the following way when you see syntax like this in python's documentation this means that the alternate form of range takes an argument called start followed by an argument called stop followed by optionally a third argument called step and i know as the reader it's optional because it's in square brackets here so nothing to do with lists or arrays or anything like this this is just human documentation anytime you see things in square brackets that tends to imply to the human reader that this is optional so what does that mean well notice that there is no flavor of range that lets me specify a stop and a step which i thought there was a moment ago when answering olivia noah but rather there is this three input version so if i specify i want to start at zero i want to stop at 101 which is just past what the 100 i care about and then provide an optional step of two this will give me a program ultimately that will print out all of those even numbers so let me do this first let me go into a program here i'll call it count dot pi and i'm going to go ahead and start at 0 go up 2 but not through 101 stepping 2 at a time and this time i'm going to print out i and here too another handy feature of python no more percent s and also no more percent i if you want to print out the value of a variable called i just say print open paren i close paren you don't need another format string as in c let me go ahead now and run python of count dot pi enter and it scrolled by really fast but notice that it stopped at 100 and if i scroll to the beginning it started at zero so my apologies maya culpa for messing that up earlier but what a wonderful opportunity to introduce the official documentation for python which will soon become your friend cryptic though it might feel at first glance all right let's go ahead then and revisit one other program that we started with earlier and that program was again this relatively simple hello program that we left off in this state we were using the getstring function from the cs50 library in python we had a variable called answer that was getting the return value of that version of getstring and we were printing out hello comma so and so and we were using that new cryptic feature but handy known as a format string or an f string which just means replace whatever's in curly braces with the actual value so let's start to now take off the training wheels that we just put on only an hour ago let's get rid of the cs50 library how can we actually get input in python without using a library from someone like cs50 well getstring no longer exists but thankfully there is another function we can use called quite simply input input is a function that quite similar to getstring in both c and python prompts the user with a phrase like this one here what's your name waits for them to type in a value and as soon as they hit enter it returns whatever the human has typed in for you so if i go ahead now and rerun this program python of hello.pi after getting rid of the cs50 library and using input instead of getstring what's my name david hello comma david so already there now this is raw native python code completely unrelated to anything cs50 specific but now let's go ahead and let's keep using the cs50 library initially because we'll see that very quickly are there advantages of using it because we do a lot of error checking for you but we'll eventually take those training wheels off entirely as well but notice indeed how relatively simple it is to do so let me go ahead and open up a program that we wrote in advance and i'm going to go ahead and grab this this is available as always on the course's website and i'm going to go ahead and open a file called edition0.c which we've actually seen before and i'm going to go ahead and do this fancy thing here where in just a moment i'm going to split my window so that i can see two files at a time and over here i'm going to create a new file and i'll call this addition.pi so that is to say i'm just going to rearrange my ide temporarily today so that we can see one language on the left c and a corresponding language on the right in python and again you can download all these examples online if you'd like to follow along on your own so if i'm translating this program on the left to this program on the right let's first recall what the program on the left actually did this was a program that prompts the user for x prompts the user for y and quite simply performs addition on the two so this was week one stuff way back when now well let's go ahead and translate this i will use the get in function from the cs50 library because it's going to make my life a little easier for now i'm going to say from cs50 import getint i'm going to then go ahead and get an int from the user using getint and prompting them for x i'm going to then go ahead and get an in from the user prompting them for y i'm going to then finally go ahead and let's say print out x plus y and let me go ahead down here and run python of addition dot pi i'm now even prompted for x let's type in one y let's type in two and voila three is my program here so pretty straightforward fewer lines of code because one i don't have these unnecessary includes like standardio.h i don't have any of the curly braces to be fair i don't have any of the comments so let me write comments in python it's going to be a different symbol prompt user for x should be prefixed with a hash symbol now instead of a slash slash but i'll go ahead and prompt user for y and then how about here perform addition but even still it's pretty tight it's only 10 lines of code with some of those comments there all right well what might i do that's a little bit different well let's take off the training wheels let's take off the training wheels and get rid of the cs50 library again and get input here well if i go ahead and get input here get input here assigning the values to x and y respectively i'm going to go ahead now and run python of addition.pi x will be 1 again y will be two again and the answer of course is twelve well that's wrong what's going on how did i screw up such a simple program already albeit in a new language for me python what did i do here yeah ben um because um it's probably taking it in as two strings so it's just putting them next to each other as opposed to doing the actual math on it it's not reading it in as an end exactly so input this function that comes with python really is analogous to cs50s get string no matter what the human types it's going to come back as keyboard input characters or ascii characters or unicode characters from weeks past even if they look like numbers they're not going to be treated as numbers aka integers unless we coerce them so now remember in c we had this ability to cast values from one to another casting meant to convert one data type to another and we were allowed to do that for chars to ins or instachars but you could not do it for strings to ins or from inst to strings for that we needed special functions and some of you might have used a to i ascii to int which was a function that actually looks at all of the characters in an ascii string and converts it to the corresponding integer in python frankly it's a little simpler we can just cast it from one thing to another so i'm going to go ahead and cast the return value of input as using this int and i'm going to do the same for y passing the return value of input to it there to convert what looks like a string to what's actu what looks like an int to what's actually an int and now let me go ahead and perform the addition again python of addition.pi and notice this time hopefully to ben's point it's not going to concatenate two strings as we saw is the default behavior of plus when you have two strings left and right hopefully now it will do a do addition on what x equals one y equals two and voila now we're back in business however what if i'm not the most cooperative or uh sharp user and i type in cat for x now some crazy stuff starts to happen so notice we've triggered our very first error when it comes to running a program whereby my program won't even run in the first place and notice i'm getting some somewhat cryptic syntax here traceback most recent call last file dot pi line two all right that's at least familiar i screwed up somewhere on line two it's showing me the line of code here and it's saying value error invalid literal for int with base ten cat that's a very cryptic way of saying i just have tried to cast something that's not an integer to an integer and so this is why we use things like the cs50 library it's actually kind of annoying to write all of the code that checks and makes sure did the user type in a number and only a number and not cat or dog or some other cryptic string we ourselves now would have to implement that kind of error checking if we don't want to use the cs50 library so they're trade-off maybe you feel more comfortable writing all of the code yourself you don't want to use some random person on the internet's library whether it's cs50s or someone else's even if it's free and open source you want to write it yourself okay fine if you want to write it yourself now i've got to add a bunch more lines of code to check did the human type in a decimal digit one after the other or did they type in other ascii characters so again trade-off between using libraries or not generally the answer is going to be use a common library to do to solve these kinds of problems well let's go ahead and change the program a little bit let me go ahead and open a new file called division dot pi just to do a bit of division here and let me go ahead on the right hand side and copy paste what we did before but just change to division here let me go ahead and divide x by y and i keep typing in 1 for x 2 for y in a moment i'm going to run python of division.pi and type in 1 for x and 2 for y but before i hit enter if this were a program in c what would the answer be feel free to just respond in the chat if you'd like if this were a program in c and i'm dividing x by y what would i have gotten in week one and every week since brian the consensus looks like zero yeah because of truncation if 1 divided by 2 of course is 1 half or 0.5 0.5 is a float but if i'm dealing with integers even though it's implicitly integers thus far and now explicitly now that i've casted them i would seem to throw away the 0.5 and just get back zero but let me go ahead and run python of division.pi and putting x equals one y equals two and voila wow one of the most annoying features or lack of features in c seems to have been insult seems to have been solved in python by division doing what you want and if you divide one integer by another in python it turns out one of the other features of today's language is that it does what you the programmer would expect without having to get into the weeds of the nuances of floats and ants it just does the quote-unquote right thing instead well let me go ahead and open up another program here also from week one this one was called conditions.c and this one give me one moment to open this up on the left this one here was a program whose purpose in life was to get an in from the user called x get another called y and then it just did this if x less than y print out as much else if x greater than y print out as much and so forth let's go ahead and translate this program into the corresponding python code using some of the syntax we've seen already i'm going to go ahead and save this as conditions.pi and i think i'm going to go ahead and keep using the library the cs50 library so i don't have to worry about those kinds of errors when casting bad input to another so from cs50 import getint and let me go ahead and now get an int from the user calling it x let's go ahead and get an in from the user calling it y and i won't bother typing comments this time just for time sake and now let me ask the question in c i would have done if x less than y python is a little more terse if x less than y suffices but with a colon under that i'm going to go ahead and say print x is less than y l if this is the weird one x is greater than y go ahead and print out x is greater than y and then else also with a colon print out x is equal to y and i think that's just about it i'm going to go ahead down here and run python of conditions dot pi i'll type in 1 i'll type in 2 and indeed x is less than y i'll run it again this time with 2 and 1 x is greater than y and let me run it again with 1 and 1 x is equal to y so that seems to have worked and let me point out one other thing i mentioned earlier that you have this other shorthand syntax where you can just say import the cs50 library if you don't want to bother typing out individual function names that's totally fine but notice that the ide is yelling at me at lines three and four that getint is no longer recognized that's because python supports this feature when using other people's libraries that it can name space them for you that is to say you can't refer to getint anymore directly you have to more explicitly say call the get in function that's inside of the cs50 library and so again using our familiar dot operator means go inside of the cs50 library just like a c struct and call the function called get int therein so i can now go ahead and rerun this python of conditions.pi typing in one and one and voila the code is now working again so which is better it depends i mean if it's sort of more readable to just write getint all over the place that's going to save you a lot of keystrokes because you don't have to keep typing dot cs50 dot if though you're writing a pretty big program and maybe you're using two different libraries that both implement a function called get int you want to be able to distinguish one from the other so you might want to just import the libraries by their name and then prefix the function calls as i've done here which is known as name spacing name spacing means that you can have two identically named variables or functions existing in two different name spaces they don't collide so long as they are inside of the cs50 library or some other library's name instead let me do one other thing with conditions here let me go ahead and open up another file uh from week one this one was agree agree.c and this program prompted the user to input whether or not they agree and we checked a little curiously that first week using equals equals quote unquote y or lowercase y or quote-unquote capital n or lowercase n well how do we go about converting this one let me go ahead and give myself a new file over here i'll call it agree.pi in this case and it turns out we can solve this one in a few different ways let me go ahead and start off by importing from cs50 getint just because it's oh no getstring rather because it's convenient let me go ahead and get the user's input via getstring and ask them the same question do you agree question mark with a space then let me check if s equals equals quote unquote y or s equals equals lowercase y then i'm going to go ahead and print out agreed else oh no l if s equals equals uh capital n or s equals equals lowercase n let me go ahead and print out here not quote unquote not agreed and i think that should do it but something's weird here there's a few differences what strikes you as different from c what muscle memory might you have to break now when using conditions with multiple boolean expressions combined in this way and there's another subtlety there's at least two salient differences between c and python with just this example alone any thoughts in the chat or an area ryan i was going to say for this one instead of using the symbols for the logical operators you can just type the text directly yeah we can literally just type the english word or if we want to express a logical or so in c recall on the left we would have done this vertical bar thing which is fine you get used to it but it's not very readable at least in any english sense python took the approach of using more frequently actual english or english-like words that actually do read left to right and indeed a theme is emerging here when you read python code it is closer to english than c is because you don't trip over as much punctuation each line of python code tends to read a little more like an english phrase or an english sentence and there's one other subtlety here on the left back in week one i took care to use single quotes around the y's and the ends this week i'm using double quotes but to be honest it actually doesn't matter i can alternatively use single quotes anywhere everywhere so long as i'm consistent but in python there is no fundamental difference between double quotes and single quotes so long as you are consistent the reason being when we looked at the data types that existed between c and now python absent from the list of python data types was char in python there is no such thing as an individual char everything that's character based is a string even if it's just one character long everything is a string downside is we don't have it quite as fine grain control upside is we get a lot more features with those string structures as we've already seen with for instance doing something like uppercase with those as well well let me go ahead and i think i can simplify this for instance suppose i wanted to tolerate something like not just y or y uh in uppercase or lowercase suppose i wanted to also tolerate yes uh in uppercase or lowercase as well well you could imagine just starting to add to the code or s equals equals yes or s equals equals yes but wait a minute what if the user is being a little sloppy and what if i want to actually say like well what if they're yelling or s equals equals yes in all caps and there's a few other permutations as well like this is quickly devolving into quite the mess but if at the end of the day you really just want to detect why or the word yes irrespective of capitalization i bet we can be pretty clever in python here what if i go ahead and say if s is in quote unquote y or yes in fact i can borrow an idea from earlier whereby i can use the square bracket notation to give me a list which again is like an array but it automatically grow or shrink as you need it you don't have to decide in advance how big it is this preposition here in is a new keyword in python that will literally answer that question for me and we've used it before earlier when i implemented speller i said if the word is in my set of words return true so if s in this list i'll get back true or false based on the answer to that question but again it's not tolerating case but no big deal dot lower now i can say is the lower case version of s no matter what the human typed in in this list of two values that means now the user can type in all caps in alternating caps in one's capitalized letter or any other permutation whatsoever all right so that then is our conditions let me pause here to see if there's any questions any questions or confusion that we can clear up with syntax with conditions boolean variables of boolean values so a question came up so in python we are allowed to use the equals equal syntax to compare two strings yes so another really good catch in python there are no pointers underneath the hood they're still addresses like your memory hasn't gone anywhere but underneath the hood all of that is now managed for you by the language itself so if you want to conceptually compare one string against another just as i did here now on line seven you can indeed use equals equals and python will do the quote unquote right thing for you you don't need to regress into using stir comp instead now just for clarity let me go ahead and update this if s dot lower in quote unquote n or comma no we can achieve the same result there by doing the same technique well let me go ahead and open up another example that you might recall we did a progression of examples to sort of make it uh good better and then best this one involving just a cat meowing in some form so let me go ahead and open up from week one an example that was called meow zero relatively straightforward that simply did this it simply meowed three times so suffice it to say now in python it's pretty trivial to do something three times like this i'm gonna go ahead and call this meow dot pi and of course i can just do something like print meow and i can just copy paste that but of course the whole point of this example back in week one was not to devolve and to just copy paste surely there's a better way and we've seen a better way this time if we wanted to change this into a for loop in c we could have done something like for int i get 0 i less than 3 i plus plus then in some curly braces we could have done printf of meow new line semicolon so that was the next version of our meow code in c but in python of course it's a little more succinct i can just do four i in range three print quote unquote meow so very similar in spirit to our hello world of before but again we don't have to include any libraries for this we don't need to have a main function or we don't need any of those curly braces or semicolons or the like we can just dive in and focus on the code itself but recall that we also last time evolved the meow program into having our own helper function our own function that actually allowed us to create an abstraction on top of meowing and that was in our third version aka meow2 let me go ahead and open up this version in a tab and notice that this version starts to get a little involved because one we needed a prototype at the top because i now had a meow function at the bottom whose purpose in life was just to print meow but to abstract that away is a new helper function and then i had this code here with a for loop inside well in python it's going to work out to be a little simpler here too if i want to do something three times for i and range of three go ahead and call meow now of course meow doesn't yet exist so i can solve that problem we've seen earlier albeit quickly in speller that i can define my own functions like meow there's no more void because if you don't want to have arguments in a function just don't put them there there's no return value specified in python they're implicit instead so it suffices to do this and now i can just print out meow so here now i have a program that iterates three times calling meow each time and meow is defined down below let me go ahead and run this python of meow dot pi trace back most recent call last there's a problem on line two of meow dot pi because a name error name meow is not defined now the language being used there by python is a little different from c's it's frankly a little more uh human friendly but what just happened what problem has arisen that i yet haven't tripped over until now even if you've never programmed in python before and even if you haven't run help 50 yet what might be the uh the issue there ginny if that's other function is not found when we are trying to call it because it's described below when we are calling it yeah so this is no prototype yeah there's no prototype and it turns out in python there isn't a notion of prototype so unfortunately the solution we saw in week one is not to just copy and paste the first line up above and end it with a semicolon that's just not a thing i could do this i could just move my meow function to the top of the file thereby defining the function first and then using it last and that would actually solve the problem meow meow meow that of course doesn't really help us long term because you could probably imagine a situation where this function wants to call this function but this function calls this one and you just can't really neatly order them in some safe way and it's just not going to be as maintainable right recall that one of the values of putting main at the top of rc programs was that any reasonable person who wants to understand your code is probably going to start reading top to bottom they're not going to want to have to like scroll through all of your code looking for the actual main code so it turns out in python even though you don't need a main function it's actually common to define one nonetheless it's going to be implemented with something like this and i'm just going to indent my code below that there so now i've defined main but i haven't executed any code yet on line 6 i've now defined meow but i haven't executed any code yet and i mean that literally if i run python of meow now and hit enter i would hope to see meow meow meow but i see nothing and this is a little weird but python is doing literally what i told it to do i told it to define a function called main and i told it to define a function called meow what i never told it to do is to call either of those functions so the simplest fix here it's a little different from c and a little weird is just call main is your very last thought in the file so define main up at the top just where most programmers would expect it to be but call it all the way at the bottom and let me go ahead and now and run my program and now voila meow meow meow is back because i've defined main i've defined meow and now i am calling main now as an aside you will very often see in various documentation and tutorials online a much more cryptic incantation than this which will have you typing out this this achieves the same goal but it's not strictly necessary for our purposes this line of code if you see it in any online references or examples or books or sections or the like it is necessary only when you're implementing essentially your own libraries like your own cs50 library or your own image blurring library or the like it's not necessary when we're just writing individual programs of our own so i'm going to go ahead and keep mine simple and literally just call main and let me just wave my hand at why you need that syntax otherwise in this context but let me go ahead and modify this one last time because recall that in c the last version of my program had me running meow and passing in input because i defined meow was taking an input like n and then doing something like for int i gets 0 i less than n i plus plus and then inside of my curly braces did i print meow so that now i have a helper function that i've invented that takes one input in int called n and it loops that many times and prints out meow that many times and now i have a real nice abstraction and that now my program is distilled it's just meow three times and it doesn't matter how i implemented meow i can do the same thing in python i can go ahead and say that meow takes an argument called n i don't have to bother specifying its type i can now say for i in range of n and i can print meow that many times and now i can get rid of my loop in main and just say meow three times and so same functionality if i run this a final time meow meow meow but now i'm kind of designing my code in a more sophisticated way by actually giving myself now some of my own actual uh helper functions all right any questions then on this progression now we're not really seeing new python syntax we're now just seeing a translation of some actual past c programs into python to show really the equivalence all right well let's go ahead and open another version from week one of a program called positive.c which was an opportunity back then not only to define our own helper function called getpositiveint but it also introduced us to the familiar do while loop and unfortunately we're going to take that away from you now python does not have a do while loop but it's of course a very useful thing to be able to do something while a condition is true after all pretty much any time we've gotten user input in the class we've used do while so that we prompt them at least once and then optionally again and again and again until they cooperate so let me go ahead and implement this in python now in a file called positive.pi and go ahead here in positive.pi and translate this thing as follows let me go ahead and uh from cs50 import getint let me go ahead and define a function called main so now i'm just going to start to get into this habit i'm going to go ahead and give myself a variable called i and call get positive int and then i'm just going to go ahead and print out i keeping it nice and simple now i have to implement get positive int it doesn't need to take any input so i'm not going to give it any arguments and now i have to do the do while thing so the pythonic way to do this in python is almost always to deliberately induce an infinite loop and the idea being if you want to do something again and again just start doing it forever and then break out of the loop when you are ready to so what do i want to do forever in this function well i want to go ahead and get an int and prompt the human for a positive integer and then i want to go ahead on the next line and ask a question well if n is greater than zero thereby making it positive break and the last line of code here is going to be to return n so notice in c on the left i did this do while thing i had to declare n outside of the do while loop because it had to be outside the curly braces to be in scope but in python here notice what i'm doing here is actually a little bit different and did i screw up oh yes i did screw up okay if asked the actual question if n greater than zero so what did i do actually differently here on the right hand side will notice i deliberately induce this infinite loop on line 10 which just means do the following forever i then ask the user for variable n with get int and then i check is n greater than zero if so break out of the loop how do i break out of the loop well notice that the indentation here has been very consistent so when i break out of the loop that puts me back in line with the original indentation which is now on line 14. notice that the return lines up with the while loop which means it's the first line of code that's outside of that loop in the past we would have had very explicit curly braces now we rely only on indentation that then lets me return n so what are some of the differences here one the do while loop is completely gone but two scope is no longer an issue it turns out in python that the moment you declare a variable it exists until the end of that function you don't have to worry about the nuance of declaring a variable first like we did in c up here and then returning it down below the moment we execute this line of code 11 here n suddenly exists for the entirety of the remainder of that function so even though we declared inside of the loop so to speak as per the indentation it is still accessible to the return statement here at the end of the program all right let me pause there and see if there's any questions or confusion on getting user input doing the equivalent logically of do while but doing it now in this more pythonic way peter in python are variables accessible across functions or no good question no so if you declare a variable inside of a function it is scoped so to speak to that function it is not available elsewhere you would have to return it and pass it as output to input or you would have to define it for instance as a global variable instead all right well what else then might we translate well recall from uh recall from our earlier endeavors in week one we played around with these examples from mario and for instance we wanted to print something out in python or in c that mimicked the notion of these uh pyramids or these coins or these little bricks on the screen well here let me go ahead and open up a new file called mario.pi and i'm going to transition away from always showing the before and after and now just start to focus more on the python code that you can always look back if you wanted the corresponding c versions how do i go about printing out three bricks like this vertically well in python i might say something like for i in range of three quite simply as we've done a few times already and just go ahead and print out a hash i don't need to worry about the new line because you get it for free so to speak but i'm going to go ahead now and run python of mario.pi and voila there's my very simple ascii version of this mario structure but what if i want to do the coins instead what if i want to do this horizontal coins that appear in these four bricks and print out an uh version of that well how might i do that well let me go ahead and change this to be instead in my code for i in range of four so i can print four of these things let me go ahead and print out a question mark and then run this so let me run mario.pie and voila damn like not what i wanted and so here's that trade-off right you might have been kind of excited so far as it's possible to be excited about code that like oh my god you don't need to do the stupid new line characters anymore but what if you don't want it now we've kind of found a downside of getting those new lines automatically well it turns out if we read the documentation for the print function in python it too can take multiple arguments and what's powerful about python 2 is that it supports not just positional arguments where you just do a comma separated list of multiple arguments to a function python supports what are called named arguments whereby if a function especially one that's super powerful like print takes multiple inputs like uh this one this other one and this other thing each of those inputs can have names and you the user of that function can specify the name and it turns out that print in python supports an argument called end and you can explicitly say what value you want to give to that parameter by mentioning its name and here i'm going to literally do this i'm going to tell the print function that i want the value of end a parameter an argument to it to be quote unquote the reason for that is that if i read the documentation the default is actually this if you read the documentation it will tell you print default value for its end argument is backslash n this two is a feature that c did not have c did not have optional arguments they're either there or they're not rather they either have to be there or they cannot be there python supports optional arguments that even have default values and so in this case the default value of this per the documentation is that end is quote unquote backslash n which is why every line ends with that value if you want to change that to be nothing the so-called empty string you change it to quote-unquote so let me go ahead and run this now and voila closer it's a little stupid looking because now my cursor ended up my my prompt ended up on the same line so maybe after this line let me just go ahead and print nothing that is a new line and now if i run mario.pi voila now i get the effect i want and if you want to see what's really going on here i can do something stupid like hello and now i can end every print with hello hello hello hello right not that you would do that but that's all it means it's ending every call to print with that expression but the correct version of course is just to blank it out in this way but here's something that's kind of cool and this is where if you're kind of a geek like life starts to get really interesting fast i can actually change my python code to print out these four question marks in the sky to be quite simply print quote unquote question mark times four and now if i rerun this program boom done and here's where again you're getting a lot of features in the language where you don't have to think about loops you don't have to think about a lot of syntax if you want to take a question mark and do it four times you can literally use the star operator which has been overloaded to support not only multiplication with numbers but also automatic concatenation if you will with strings in this way so let me go ahead and do one final version from mario recall that the last thing we built with mario looked a little something like this let me go ahead and change my mario code now to b4i in range of three because this is a three by three grid of bricks let's say and let's go ahead now and inside of this loop do another nested loop where i do three columns as well and in here i want to print out a single hash at a time but i don't want to print out a new line i only want to print out a new line here so it turns out that essentially because python gives you the backslash ends for automatically essentially any logic you wrote in the past now needs to be reversed if you ever printed a new line now you don't want to print a new line and if you ever didn't print a new line now you do in some sense so let me go ahead and not make wrong language python of mario dot pi and voila my three by three grid so this is to say that in python we can nest loops just like we did in c i can use multiple variable names like i and j being conventional there's no curly braces there's no semicolons but again the logic the ideas are still the same it just takes a little bit of time to get used to for instance some of the new syntax you'll recall that in c we ran into a problem pretty early on with integers and let me create a program here called int dot pi and let me initialize a variable called i to one and let me go ahead and do this forever let me do this forever inside of a while true block let me print out whatever i is and then let me go ahead and just add one to i on each iteration let me go ahead and run this program and let me increase the size of my window for now and just run this thing whoops that was mario let me run this thing python of inta pi and you'll see that it's counting up to infinity and honestly this is gonna take a while you know what's faster than counting by one maybe multiplying by two so let me go ahead and multiply by two instead to kill the program just like in c i used ctrl c and that's why i see keyboard interrupt it respected my wanting to cancel the program let me rerun this now and just count really big and even though the internet is being a little slow which is why it's a little shaky that's a really big number already if i keep doubling i what would have happened already at this point if i were using c to implement this program if in c i declared a variable called i and it was an int and i kept doubling it again and again and again and again and again literally forever any thoughts yeah what would have happened in c joy yeah i think it would have crashed would have crashed why um because it would be taking much memory good thought so it wouldn't crash per se something would go wrong it wouldn't crash because it's still an ant and in c at least it would still be taking up on a typical computer 32 bits or four bytes but honestly the program probably would have started printing zero by now or even negative numbers because recall one of the limitations of c is that integers are a finite size only 32 bits or four bytes which means if you keep going from 1 2 4 8 16 a million uh 2 million 4 million 8 million and so forth eventually you're going to get into the billions and as soon as you cross the 2 billion threshold or maybe the 4 billion threshold if using assigned or unsigned numbers it's going to get too big you're going to have integer overflow but in the world of python integer overflow not a thing anymore in the world of python your numbers will get as big as you need them to get they will automatically address this problem for you unfortunately floating point imprecision still a thing so i only divided one by two earlier but if i continue to divide other values and i looked at enough decimal points we would still suffer unfortunately from floating point and precision however in the world of python like in java and other languages there are libraries scientific libraries that allow you to use as much precision as you need or at least as much memory as your computer has so those problems too have been better solved in more modern languages than in something out of the box like c code but just by multiplying that number again and again was i able then to demonstrate much larger numbers than we ever saw in weeks past well let me go ahead and do another program here this one called scores.pi that's going to be an example of really keeping track of scores which was an example we did early on in week two of the class and in python i'm going to go ahead and give myself a list of scores like this 72 73 and 33 again sort of a playful reference to our ascii numbers but in this context they're quiz score so two ok quiz scores and one kind of low quiz score assuming these things are out of like a hundred but notice the syntax i'm using square brackets in python give me a list i don't have to decide in advance how big it is it's not an array per se but it's similar in spirit but it will automatically grow or shrink and the syntax is even simpler suppose i want to average these scores in python i could do something like this i could print out that the average of these scores is for instance and then i could do something like this i could do the sum of scores divided by the length of scores and some of this is actually kind of new already it turns out in python that there is a sum function that will take a list as input and return to you the sum of those items and we've seen already there's a length function l e n that tells you the length of a list so if i add up all my scores and then divide by the total number of scores that should give me by definition my average so python of scores dot pi voila oops uh what did i do here ah i screwed up so unintended admittedly but let me try to save myself here so what just happened well this error message is a little cryptic says type error can only concatenate stir not float to stir so long story short python in this case does not like the fact that i'm trying to take a string average on the left and concatenate to it a float on the right so there's a couple of ways i can solve this and we saw the fundamental solution earlier if this expression here that i've highlighted is by definition mathematically a float but i want it to become a string i can just tell python convert that float to a string so much like there's the i2a function that some of you discovered which is the opposite of the a2i function i can take in python in this case a float and convert it to a string equivalent so now if i run python of scores dot pi voila my average is 59.3333 and you already see a bit of imprecision there's some rounding error at the end there that is not a perfect one-third but there's another way i could do this and it's a little uglier but i could use one of those f strings i could say go ahead and plug in a value here and just print out the user's average so it turns out that inside of these curly braces you don't have to print just variables you can actually put entire coding expressions and i would encourage you not to paste crazy long lines of code because it's going to very quickly get unreadable at that point you probably should use a variable but here i can go ahead and run python of scores.pi and voila i screwed up again also not intentional but i can fix this yeah i'm missing the uh the f at the beginning to make this a formatted string and now if i rerun it voila same exact answer so again i have multiple approaches there's a third one here i could do something and actually i don't need the stir in that context because now if it's inside of a format string python will presume that i want to automatically convert it to a string so that's nice or i can just factor this out and i can say something like this give me a variable called average assign it equal to that math and then print out the average so again just like in c so many different ways to solve the problem and which one is best depends really on what might be most readable most maintainable or easiest to do let me go ahead and add some scores dynamically now instead of hard coding my three scores let me ask myself for my scores over the course of a semester from cs50 let me import getint just so i can get some numbers easily let me give myself an empty list of scores the syntax for which is just open bracket close bracket so nothing inside of it initially and now let me go ahead and do this let me get myself three scores maybe it's the end of the term now for i in range of three let me go ahead and append to the scores array whatever the return value of getint is like this now this too i could do in a bunch of ways let me get rid of this here whoops nope we'll leave that there this i could do in a bunch of ways but notice what i'm doing i'm getting int and i'm passing the return value of int to a new function called append it turns out that lists the square brackets once you've defined them in a variable like scores they too have functions built into them so i can do scores.append in order to add a number to the list so now let me go ahead and run this python of scores.pi let me manually type in my 72 my 73 and my 33 and voila say make the exact answer but think about how much of a pain this would have been in c if you had to either decide and advance the size of the array or not decide it in advance and use malloc and re-alloc to keep growing and shrinking it python using this append function which comes inside of that list variable handles all of this automatically for us all right so that too was a whole bunch of features any questions though that i can answer here any questions no yeah over to uh santiago um yeah i had a question about um so even a pen even if a pen like reduces the the amount of code you have to write does it underneath the hood just do exactly what we were doing and see which is like malloc and realic or something like that like is that all is that happening inside python yeah that's exactly what you're getting for free so to speak with the language all of that malloc stuff reallock stuff maybe it's implemented with an array underneath the hood like in the actual computer's memory maybe it's a linked list like we saw last week but all of that is happening for you but that again is one of the reasons why the code ultimately runs a little slower because you have someone else's code in between you and the cpu of your computer doing a bit of that work for you sophia are there efficiency differences in between like the ways that we print of like utilizing the f um like formatting or the other forms that we used uh you don't have to be if i'm understanding correctly it's uh there are some fancy features of it for instance there is syntax you can use to specify how many decimal points you want to print after a floating point value um but it's no longer all of the percent i percent s percent f and so forth there's slightly different syntax but fortunately less of it since you don't have to worry as much about those conventions other questions or confusion no all right well let me go ahead and do one other example that might be familiar from some weeks past let me go ahead and whip up a quick example of uppercasing just to tie together one of our earlier examples that we saw more organically or lowercasing in this case a file called uppercase.pi let me go ahead and from the cs50 library let me go ahead and import getstring and then once i have this let me go ahead and get a string from the user and ask them for before for instance um and then let me go ahead and do the following let me go ahead and print out after the goal being i want to uppercase this whole string for the user and i'm going to keep this all on the same line so again i want a program that's going to print before ask the human for some input and then after show the capitalized version of the whole string so how can i do this well we've seen one way already i can do literally for instance s dot upper and let me go ahead and save this and now run python of uppercase.pi let me type in high and lowercase and boom now i get back the uppercase version but if you want you can actually manipulate individual characters as well let me go ahead and a little more pedantically do this for cns print c now this isn't quite what i want yet but it's a stepping stone notice now if i type in high and case i see h i exclamation point all still lowercase so i haven't done anything interesting but you know what let me get rid of the new line just so it all stays on the same line because that was kind of ugly let me do it again okay a little better let me actually add a new line at the very end of the program to move my cursor to the new line let's do it once more hi okay i'm not uppercasing anything but if i change c to c dot upper i can do that as expected and let me run it again hi and boom now i have another working program but the new feature now is notice this coolness on line five if you want to iterate over a strings characters you don't need to like initialize i to zero and then you square bracket notation like you did in c you just say for c and s or for x in y whatever it is 4 can also be used to iterate over the individual characters in a string as you might want to do when doing something like cryptography or the like so we don't have to just uppercase the whole string all at once we can still gain access to our individual values and there's other things you can do in python as well that we could do and see let me go ahead and create a program here called argv dot pi for argument vector which recall was uh the name of the input to main that allows you to access command line arguments now today we have seen that you can have a main function but you don't need to but it's conventional it's not required anymore and so we don't we haven't seen rxc or rv yet but that's because they're elsewhere in python if you want to access command line arguments in python it turns out that you can import a module called argv and this is a little new but it follows the same pattern as cs50s library i'm going to import from the system library a feature called rgv so this just means that it comes with python but to use it you have to import it explicitly and now i'm going to do this if the length of arg v equals 2 then i'm going to go ahead and print out just like we did a few weeks ago hello and then argv bracket 1. somewhat cryptic but i'll come back to this in a moment uh else i'm gonna go ahead and print out a default of hello world so we did this some weeks ago in week two whereby we ran a program that if the user typed their name at the prompt it would say hello david or hello brian if they didn't it would just say hello world so to be clear if i run this thing and run it without any command line arguments i just see hello world if i run it again though and type my name in and hit enter now i see hello david so how is that working well this first line of code gives me access to argv which is now tucked away in the cis library if you will the sys package so to speak but it works the same way there's no arg c but no problem if argv is a list of command line arguments which it is length len will tell me the length of that list which is equivalent to argc so i can reconstruct the same idea from my version in c and here then i have a format string that prints out hello comma and then whatever's in curly braces and argv is a list and just like in c which had arrays a list is just an array that can dynamically grow and shrink for you you can still use square bracket notation to get at in this case the second thing the human typed so let me change this just for clarity to be zero and if i rerun this now and type in david it says weirdly hello rgv.pi so what you don't see is the word python python is the interpreter but that's not part of your program's execution per se argv 0 is going to be the name of the python program you're running and argv1 is going to be the first word thereafter and so forth so we still have access to that feature but now we can convert it now to python and in fact if i want to print out all the command line arguments i can just more simply do this for arg in argv go ahead and print arg so very succinct if not obvious at first glance now let me go ahead and type in something like david malin two words enter you now see everything printed or typed after the program's name and so forth so here too notice how neatly we can iterate over a list in python there's no i there's no square brackets necessarily you can just say for arg and rgv just like a moment ago i said for c in s pretty much the python for loop is smart enough to figure out what it is you want it to iterate over whether it's a string or a list and my god it's just so much more fun or pleasant to program now when you don't have to worry about all the stupid mechanics of like incrementing and plus plus and semicolons and all of that syntactical mess all right let me pause here to see if there's any questions i know we're going through some of these examples quickly but they're really just translations again and for coming upcoming problems and problem sets will you be able to more methodically compare before and after as well anything at all on your end brian nothing here all right so let's look at some of our final past examples and then we'll reserve some time at the end of today to look at some even more powerful things that we can do because now of languages like python let me go ahead and create a program this time called exit.pi exit.i and this program's purpose and life is just going to demonstrate exit statuses recall that eventually in c we introduce the notion of returning 0 or returning 1 or any other value from main we do have that ability now in python 2 that you'll start to see in more larger programs here too i'm going to go ahead and import cis the whole thing this time just to show a different way of doing this i'm going to say if the length of sys.org v does not equal 2 let me go ahead and yell at the user missing command line argument and then after this i'm going to go ahead and do sys.exit of 1 otherwise i'm going to go ahead and print out a formatted string that says hello comma argv bracket 1 with cisnow in front of it for reasons i'll explain in a moment and then at the end i'm going to go ahead and by default print sys.exit zero all right so what is going on here one because i'm now using sys for two different things i decided not to import rv specifically but just to import the whole library but because i did that i can't just write the word argv anywhere i now have to prefix it with the name of the package or library that it's in so that's why i started doing sys.rv sys.rgv but i'm also using another feature of the sis library which gives me access to an exit function which is the equivalent to returning from main so this is a bit of a dichotomy in c you had a return zero or one or some other integer from main in python you instead call sys.exit with the same kinds of numbers so a little bit different syntactically but it's the same fundamental idea what's the purpose of this program well if i run this thing its purpose is just to make me type in one word and only one word after my program's name so notice if i just run python of exit dot pi it's yelling at me missing command line argument if i run it instead with my name after that now it says hello david so stupid program it's only meant to demonstrate how you can now return different values or really return prematurely from a program because you're no longer in maine you can't return per se but you can now in python exit as needed so that's the comparable line there are any questions then on exit statuses again we're just kind of churning through the list of features we saw and see even if they don't come to you supernaturally not pun not intended but rather there is uh there are analogs here in the python world no all right well recall that after that we started focusing really in the class on algorithms and that's when like the size of our data sets and our the efficiency of our code started to really matter let me go ahead and write a program called numbers.pi that for instance contains uh an import at the top for sys because i'll need that in a moment and then it gives me uh let me give myself an array of numbers like 4682750 and you might recall that those were the numbers behind the doors in week three and suppose that i want to search for the number zero well in c to implement linear search you would use a for loop and a variable like i and check all of the locations python's way simpler if zero in numbers go ahead and print out found and then i'll go ahead and else print out not found and that's it so let me go ahead now and do python of numbers dot pi hopefully i will see indeed sheet found because it's in fact there so that's it linear search is just a prepositional phrase if zero in numbers gives you the answer true or false that you want so there is our linear search what if i want to do it for names well let me go ahead and give myself a second file similar in spirit called names.pi let me again import and actually if i really want to be identical to our c version let me go ahead and exit with zero here and let me exit with one here but strictly speaking that's not necessary that just happens to be what i did when we did this in c instead in names let me go ahead and do something similar let me give myself a names list with a whole bunch of names bill and charlie and fred and george and ginny and percy and lastly ron all the way at the end and then let me just check if ron is in that list using linear search if ron in names go ahead and print out found else go ahead and print out not found and i won't bother printing out or exiting with 0 or 1 this time but let me go ahead and run python of names whoops python of names and voila we found ron and notice i'm not cheating i don't think i've screwed up if i go ahead and say ron old if that was in fact his formal name now i search for ron not found it's looking indeed for an exact match so that's pretty cool that we can distill something like that pretty readily but recall that a little bit ago i proposed that python has other data types as well among which are these things called dictionaries or dicks d-i-c-t which represent a collection of key value pairs similar in spirit to a dictionary like the spanish dictionary has spanish keys and english values converting one to the other this english dictionary has english words and english definitions but the same idea a collection of keys and values using one you can find the other well let's go ahead and translate this into python in a program called phonebook.pi and implement something like c phone book a while back which recall in pi in c we used a couple of arrays initially then we scratched that and we used an array of structs instead now let's use a dictionary which is a more general purpose data structure as follows let me go ahead here and from cs50 import getstring then let me go ahead and give myself a dictionary of people and the syntax here is a little different but i'm going to go ahead and preemptively use curly braces they are back for the purposes of dictionaries and then here's how you define key value pairs one key is gonna be brian and his value is gonna be one six one seven four nine five one thousand that's his number and then i'll be one of the other keys for now we'll keep it a very small phone book or dictionary mine will be plus one nine four nine four six eight two seven five o and that's it so the curly braces can technically be on different lines i could move this up here i could get rid of this but there are certain style conventions in python the point though here is that a dictionary is defined with curly braces at the beginning and end the keys and values are separated by colons and the key value pairs are separated by commas so that's why it's conventional to write it the way i did it's just a little more obvious that this is a dictionary with two keys each of which has a value it's just associating left with right so to speak now what does this mean suppose i want to search for someone's name well let me go ahead and give myself a name variable call getstring asking the human for a name and let me implement my own virtual phone book much like the contacts app in your phone let me go ahead and then say once i have the name if name in people that's great if i found the name in people let me go ahead and print out that the number for that person is people bracket name and this is where dictionaries are going to get really powerful let me run it first and then explain python of phonebook.pi enter whoops python of phonebook.pi let me search for brian's number boom there's brian's number let me go ahead and run it with david's name boom there's that number let me go ahead and run it with uh say uh montague's name don't have his phone number just yet he's unlisted as would be anyone else that i type in so what has gone on here well at the top i'm declaring this new variable called people and it's a dictionary a set of key value pairs left and right then i'm just getting a string from the user using get string as before and then this is powerful too this is essentially on line 9 searching the whole dictionary for the given name and it's returning to me down here the name associated with that sorry the number associated with that person's name and let me make this more clear by factoring this out let me give myself a variable called number and then more explicitly print out that variable's name here's what's different today if name and people is written here what this does is it python searches all of the keys for that name it doesn't search values when you say if name in a given dictionary like people is it searches only the keys if you then found the key i know definitively that david or brian are in the dictionary and notice this it's just like in c's array syntax you can now use square bracket notation to index into a dictionary using a word like david or brian and get back a value like our phone number in c and thus far even in python whenever we've seen square bracket notation it would only be typically for numbers because arrays or lists have indices numbers that address the first location middle and last and so forth everything in between but what's powerful about dictionaries is that they're otherwise known as associative arrays a dictionary is a collection of key value pairs and if you want to look up a key you simply use square bracket notation just like we used to use square brackets for numbers and because python is a pretty fancy language it handles the searching for you and even better it does not use linear search when searching a dictionary it aspires to give you constant time by using what we called last week a hash table dictionaries are typically implemented underneath the hood using something like a hash table and recall that even though it was really a a goal of achieving constant time if you choose a really good hash function and a really the right size array to hash into you can come close to constant time so again among the features of a dictionary in python are that it gives you very high performance it's not linear search and in fact set recall that when we began playing with python earlier and i re-implemented speller using what like 10 or 20 lines of code max instead of the many more that you might have written for pset five speller used a set and a set is just a collection of values long story short it's similar in spirit to a dictionary in that it too underneath the hood uses a hash table to get you answers quickly so if you think back to what that speller example was from earlier on today when i had a line of code that just said words equals set that one line of code was implementing pretty much the entirety of your spell checker all of those pointers all of those hash tables and chains and linked lists are distilled into just one line of code you get that with the language itself all right any questions then on dictionaries they will recur and they tend to be one of the most useful data structures because this ability to just associate something with something else is just a wonderful way it turns out to organize your data any questions here yeah sophia is there only a set hash function that python has defined like for these dictionaries or can we change that hash function in any way good question um the it comes with a hash function for you and python figures all of that out for you so you that's the kind of detail that you should leave to the library because someone else has spent all of the time thinking about how to dynamically adapt the data structure move things around as needed so that you no longer need to stress to the degree you might have when implementing speller yourself and it turns out other things get easy too this is not a commonly needed feature necessarily but it is something we can do let me go ahead and write a quick program called swap.pi recall that in swap.c a couple of weeks ago we gave x a value of one why a variable a value of 2. and then i printed out something like x is x y is y but this week i'm using format strings just to print that out then i did something like swap x y and i just kind of hoped for the best and then i printed out those values again well it turns out in python because you don't have pointers and you don't have addresses per se that you have access to you can't resort to the solution like last week and pass these variables around by reference so to speak by their address that's just not possible why is that a thing well it would seem to be taking a feature away from you but honestly if this past week was any indication including the week prior pointers are hard and like segmentation faults are frequent and like getting all of that stuff right is difficult and at worst your programs can be compromised because someone can access memory that they shouldn't so python takes that feature away java also takes that feature away from programmers to sort of protect you against yourself from screwing up like you may have and should have in some number of times this past week but it turns out in python there are solutions to these problems and if you want to swap x and y that's fine swap x and y and so now if i run python of swap on this program voila boom it's distilled into one other line so even though they take something away from us that you can do a lot of damage with or make a lot of mistakes with we can nonetheless hand you back a more powerful feature with this one liner for swap and notice that it's x comma y on the left but y comma x on the right and that has the effect of doing what brian did with the glasses of liquid of doing the switcheroo even without a temporary variable explicitly there though some magic is happening underneath the hood well let's go ahead and implement a couple final programs from week four and then introduce a few of our own here in week six let me go ahead and implement another phone book that this one's a little more persistent let me go ahead here and open create a file here called phonebook.csv and let me go ahead and name this name comma number so csv file recall is like a very simple spreadsheet i'm going to go ahead and just create that so i have it nearby and then i'm going to create a new file called phonebook.pi that's initially empty and this time i'm going to do this i'm going to import from cs50 the getstring function as before but i'm also going to import a library called the csv library it turns out python comes with a whole lot of functionality related to csv files to make your life easier and make it easier to do things with csvs among the things i might want to do is this let me go ahead and open up that file phonebook.csv in append mode similar to fopen two weeks ago and let me go ahead and assign that to a variable called file then let me go ahead and just get a name from the user so let me use getstring to get someone's name name here then let me go ahead and get use getstring again to get someone's number here so using number and then lastly and this is the new code let me save that name and number to a file and recall from pset four that saving files and writing bytes out to files is pretty involved like it probably took you a while to implement recover or blur any of those filters that involved creating new files turns out the csv library makes this pretty easy let me go ahead and give myself what's called a writer and i'm going to give myself the return value of calling csv.writer a file so what is this doing file again represents the file i'm trying to open csv writer is some function that comes with the csv library and it expects as input a file that you've already opened and it kind of wraps that file with some fancier functionality that's going to make it way easier for me the programmer to write to that file what am i going to do i'm going to use that writer variable to write a row that specifically contains a name and a number and i'm using a list because if you think of a spreadsheet with columns and rows a list is kind of the right idea each of the cells from left to right is kind of like a list a row is like a list so i'm going to deliberately use a list here and then lastly i'm going to close the file just as i've done in the past so it's a little cryptic here but again getstring getstring isn't uh is is is old now this is old now so the only things that are new are importing the csv i'm opening this file in append mode similar to what i did in c and then these lines here involve wrapping the file with the csv functionality writing a row to this file with right row and then closing it so let me go ahead and try this now let me open up phonebook.csv which for now only contains these headers which i created manually a moment ago and let me go ahead and run this python of phonebook.pi let me go ahead and add brian and brian will be plus one six one seven four nine five one thousand enter and now let me go to my csv file over here ah damn it i screwed up uh pretend i didn't hit enter there now it works let me go ahead now and do this again by inputting i should have hit enter when i created the file manually but i screwed up when creating it so let me wave my hand at that and convince you that i did this correctly in code by adding myself david plus one nine four nine four six eight uh 2750 enter let me go back to my csv file and voila now it's formatting correctly because i did it right row includes a line for me and notice too if i download this file let me download phonebook.csv like i did in a past week let me download this to my own mac let me open this csv file and whether you have apple numbers installed or microsoft excel you'll open something that looks like this and voila i've dynamically created using python code now my own sort of csv file and it turns out there's a way to tighten this up just a little bit not a big deal to do it the way i did but it turns out that you can also open and close files a little differently you can do this with file with rather with open as file then i can indent all of this here and i can get rid of my closed line so not a big deal to do it the way i did with open and close but the way i've done this here is a little more pythonic this with keyword which is not something analogous to anything we've seen in c the with keyword when you open a file it will automatically close it for you eventually so you might see that in some online references or other materials but again it just does that for you automatically well let's go ahead and do this i like the fact that we can now manipulate csvs and it turns out that if you've ever used google forms that's a very popular way of like collecting data from users in fact let me go ahead and go to a url which is going to show you a form like this here if brian you wouldn't mind typing that into the chat go to that url cs50.ly hogwarts and if everyone wouldn't mind just playing along just tell us what your what house you wish you were assigned to by the sorting hat in the world of hogwarts what house would you be in now if you've used google forms before you'll recall that you can see these results certainly in the google form itself and already 122 of you have buzzed in and we could see a distribution in a graph and so forth but what i want is not the distribution pictorially there i'm going to go ahead and open up a spreadsheet and so if you've never used google forms before you can click a button and then you can get a list of all of the responses that are coming in live right now and by default google keeps track of the timestamp when the form was submitted and what house was actually used so i'm going to go ahead now and do this let me go ahead and download that in another tab give me just a moment to do it on this screen here i'm going to go ahead and download that csv file onto my mac locally by going to file download csv that's going to put it into my downloads folder and then i'm going to go ahead and upload this into my ide by just dragging and dropping whoops i have to open the file browser i'm going to do this by dragging and dropping the file all right now i have that file there and let me go ahead now and make sure the file's there i have this file called sorting hat responses form responses one and so forth well let me go ahead and write a program now that manipulates this data much like you might if running a student group that's collecting data in a google form or you're just collecting information in general and have it in csv format how might you now tally up all of the results especially if google weren't just telling you graphically what the results were well let me go ahead and write a program called hogwarts which is not something that we've seen ever before in c let me go ahead and import this csv library let me give myself initially a dictionary called houses that contains a whole bunch of keys like gryffindor with initial count of zero uh hufflepuff with an initial count of zero uh raven claw with an initial count of zero and also also slytherin with an initial count of zero so notice in a dictionary or dict in python the keys and values don't need to be strings and strings it can certainly be strings and numbers because i'm going to use this dictionary ultimately to keep count of all of the votes for one house or another so let me go ahead and do this let me go ahead and open up with open the sorting hat file inform responses 1 dot csv long file name but that's the default from google as file so i'm going to use my one liner instead of having to open and close i'm going to give myself this time a reader which we did not see before a csv library has a reader function that allows me to read a csv file automatically i'm going to go ahead and skip the first row next is a function that just skips the first row because recall that that one is just uh timestamp and house which i do want to ignore i want the real data from you all and here's what's cool about csvs and python i can if i want to iterate over all of the rows that are in that spreadsheet i can do for row in reader and now let me go ahead and get at for instance the uh house in question so the house in a given row is going to be the rows first entry zero indexed so what is going on here well let me go back to the google spreadsheet a moment ago and in the google spreadsheet there's two columns and the way the csv reader works is it returns to one row at a time and that's conceptually pretty straightforward it maps perfectly to the idea of a spreadsheet but each row is returned to you as a list a list in this case of size 2 so row bracket 0 would give me a given timestamp row bracket 1 would give me a given house name so that's why here in the ide i'm going ahead and declaring a variable called house and i'm assigning it equal to row bracket one because i don't care about the timestamp we all just did this roughly at the same time but now that i have the house i can now index into the dictionary just like in c you could index into an array using a number but in a dictionary i can use strings so i'm going to go ahead and say go into the house's dictionary which i defined up above and go to the house key and go ahead and increment it by one and that's it at this point i have opened the csv file and read it using the library in this loop i'm iterating over every row in the spreadsheet that you all created by filling out that form again and again i'm just using a variable to get at whatever is in the second column otherwise known as row bracket one because row record zero would be the time stamp and then i'm going into the dictionary called houses which we defined up here i'm indexing into it just like an array but it's a list in this case using its house name which looks up the appropriate key and then plus equals one has the effect of incrementing its value so it's a nice way of going into the dictionary and incrementing go in and increment so now let's go ahead at the very end here and just print out the result for house in houses is the fancy way to iterate over all of the keys in a dictionary go ahead and print out a formatted string as follows let me print out the house name followed by a colon followed by a the houses uh dictionary indexing into it with house so again cryptic we'll come back to this in a second python of hogwarts let me cross my fingers that i didn't screw this up and i did the ide knew before i did all right now let me hope that i didn't screw this up and damn it all right the file is called something slightly different google's name must have changed sorry versus when i practiced uh let me copy this so close sorting hat responses ah it has parentheses which i forgot all right now let me cross my fingers rerun the program damn it okay no such file or direct oh i forgot the csv dot csv okay now cross fingers and oh thank god okay so gryffindor not surprisingly the most popular house uh hufflepuff at 40 ravenclaw 71 slytherin oh beat out hufflepuff very interesting for whatever sociological reason but here we have a program now that analyzed the csv now we happen to do it with silly harry potter data but again imagine collecting any data you want from users downloading it as a csv to your mac or pc or your ide than writing code that analyzes that data however you want i did a very simple summation but you could certainly imagine doing something fancier than that like doing summations or averages standard deviations all of that functionality could we get as well are any questions on dictionaries before we now offer up some of the most powerful features we've yet seen in a programming language anything at all on your end brian no hands raised here all right well let me go ahead now and i'm going to transition actually to my mac where i have in advance pre-installed python just so that i can do things locally it'll make things a little faster i don't have to worry about internet speeds and the like and this is indeed the case that on your own mac your own pc you can download and install the python interpreter run it on your own mac and pc however i would recommend you continue using this ide certainly for problem sets sake until the end of the semester maybe transitioning to your mac or pc for final projects only only because what i did this weekend was spent waste a huge amount of time just getting stupid libraries to work on my own mac which is often easier said than done just because when programmers are writing code that's supposed to work on every possible mac and pc in the world you and i and everyone else have slightly different version numbers different software installed different incompatibilities so those kinds of headaches very quickly arise when you're doing things locally um so let me encourage you to wait until terms end with final projects perhaps to sort of move off of the ide and do what i'm about to now do just because you'll be able to um uh see these demos more clearly here i'm going to go ahead and on my own mac i'm going to go ahead and create a program called speech dot pi in advance i've installed a library that supports speech synthesis and if i want access to that functionality it suffices to import pi ttsx which is the name of that person's open source free library that i downloaded and installed on my mac in advance i read the documentation i literally never used this before this past week and i found that i can declare a variable called engine for instance i can then call pyttsx3 dot init to initialize the library why that's just because of how the programmer designed it you have to initialize it first i then can use that engine to say things like say hello comma world then after that i should run the engine and wait for it to finish before my own program quits all right let me go ahead now and close that and run python of speech.pi on my own mac here hello world interesting so it said what i typed in and indeed i can probably make this even more interesting let me go ahead and say something like this let me open up speech.pi again and add some functionality i won't use the cs50 library but i will use maybe the input function let me go ahead and say name gets input what's your name question mark and then let me go ahead and say not hello world but let me use an f string which doesn't have to be used in print you can use it in any function that takes a string let me go ahead and say hello to that name all right let me go ahead and run python speech.pi again whoops let me go ahead and run python of speech.pi again what's my name david hello david weird in choice of inflection but indeed it synthesized it let's try brian hello brian okay so we could probably tinker with the settings to make the voice sound a little more natural but that's pretty cool well let me go into some code i wrote in advance this time using a different library this one related to faces and facial detection certainly uh very much in vogue when it comes to social media these days with facebook and other websites automatically tagging you very concerning increasingly with state governments federal governments and law enforcement using facial detection to find people in a crowd and let me go ahead and open up a file here for instance a little more benignly like a whole bunch of people in an office so here's a photograph of some people in an office and there's a lot of faces there but there's a lot of boxes of paper and other distractions besides those faces but let me go ahead and look at quickly a program called the tech dot pi most of this file is comments just so that if you want at home you can follow along and see what it does but let me just highlight a few salient lines here's that pillow library again where i'm accessing image related functionality from a pre-installed python function and this one's just kind of amazing if you want to use face recognition technology just import face recognition that is a library you can import that will give you access to that kind of power down here now i only knew how to figure this out by reading some documentation but you access the library called face recognition dot load image file which is a function that does what it means i'm opening up office.jpg and then scrolling down here to the white code which is the actual code all of the blue is comments recall this line of code here is all that's required in python to use the face recognition library find all of the face locations in a given image and store them in a list called face locations this line of code here is just a python loop that iterates over every face in the faces that were detected and then these several lines of code here long story short just crop out individual faces and create a new image with the found faces so without getting too much in the details of the library which are not that intellectually interesting the features are interesting to us for now let me run python of detect.pi let me give my mac a few seconds here to do its thing and voila if i zoom in here we see phyllis and jim and roy and pretty much every other face that was detected in that photograph cropped out as indeed an individual face so if you've ever noticed a little square on yourself and facebook when uploading a photo this is exactly the kind of code that facebook and others are using on their servers in order to execute that well you know what how about this um in the same office photo you know there's one person that always seems to stand out no one really likes him and that's toby what if we had sort of a mug shot of toby in a separate file like this can we find toby in a crowd among these people in the office well we can let me go ahead now and run a program called recognize.pi which you're welcome to look at online it's similar lines of code it's not terribly many that is going to do some thinking it's opening up both the office jpeg and this one and notice what just happened if i zoom in wonderfully toby is the only one with a great big green box around his face have indeed been recognized so again i'll just glance at the code this time if i open up recognize dot pi it's a few more lines of code but again i'm importing face recognition and some other things i'm loading toby.jpg and i'm loading office.jpg and then there's some more code here that's looking for toby looking for toby and then drawing a big green box around the face that is ultimately found so again at the end of the day it's just loops it's just functions it's just variables but now the functions are pretty darn fancy and powerful because again they're taking advantage of all of these other features that we ourselves have implemented in a language like c uh or have now seen glimpses of within the world of python well let's do another one let me go ahead and open up real quickly a program that will allow me to create one of these 2d barcodes a so-called qr code let me go ahead and create a file called qr.pi and in this file let me go ahead and do this import the operating system library for reasons we'll soon see and let me import the qr code library which will do all of the hard work for me let me go ahead and create an image called qr that's assigned the value of qr code making and let me paste in this url of one of the course's lecture videos for instance and then let me go ahead and save this image as qr.png portable network graphic as indeed a png very popular file format for photos and other things and then let me actually open this thing up open up system uh actually no that's fine let me keep it simple we don't need the os library no we do let's go ahead and open it up with openqr.png so three lines of code make a qr code with that url save it as qr.ping and open the file three lines of code let me go ahead and run python of qr.pi voila it was pretty fast if you would like to take out your own iphone or android phone turn on the camera if your phone supports this and scan this 3d barcode by awkwardly just pointing your phone at the lecture as we speak it should open up youtube for you hopefully and with such ease i apologize for to those yes thank you for showing me what you're now seeing i apologize for doing that yet again never gets old but all we've done is embed in a two-dimensional format details of which we won't go into in class a url which suggests that you can store anything inside of these 2d barcodes and if you decode them with something like your camera can the software running on your phones these days decode these things for you well let me do something else this time involving another sense this one listening let me go into a file called listen.pi and let me go ahead and do something very simple let me go ahead and get a user's input in a variable called word by using the input function say something and then let me just send it all to lowercase just to keep things simple and now let me do this once i get the user's words let me go ahead and say if the word hello is in their words go ahead and print out hello to you too so if they say hello i want to say hello back l if how are you in words then let me go ahead and print out something like i am well thanks as the computer uh l if goodbye in words then let me go ahead and say something reasonable like goodbye to you too and then lastly else let me go ahead and print out just something like huh unrecognized so if you will here's the beginnings of an artificial intelligence an a.i right a program that's going to somehow interact with me the human typing in phrases to this thing so if i did it correctly let me go ahead and run python of listen.pi uh i did not do something correctly uh oh not is in okay sorry let me go ahead and run python of listen.pi say something i'll say hello oh hello to you too what a nice friendly program let me ask it how it is how are you question mark it just seems to detect that let me go ahead and say okay goodbye for now and it detects that too because goodbye is in the phrase that the user typed in but if i say something like hey there it's not recognized so pretty cool we can use very simple string comparisons using the in preposition to detect things but i bet you know i bet if we use the right library we can really make this more powerful too let me go ahead and just like i imported facial recognition let me import speech recognition in python which is yet another library that i pre-installed let me go ahead and now do this recognizer equals speech recognition dot recognizer and this is just creating a variable called recognizer by my having followed literally the documentation for using this library then let me go ahead and do this also from the documentation with speech recognition dot mic micro phone uh as source so this is opening up my microphone in some sense again just following the documentation let me go ahead and say say something to the user and then after that let me go ahead and declare a variable called audio set it equal to the recognizers listen function passing in my microphone is the source and now down here let me go ahead and say print out you said and below that i will print out recognizer dot recognizes the hardest part today so far for some reason google audio all right so what's going on this line of code these lines of code here are opening up a connection to my microphone on my mac it's then using the speech recognition library to listen to my microphone and storing the audio from my microphone in a variable called audio these lines of code down here are literally printing you said and then it's passing to google thegoogle.com the file of audio that i just recorded on my microphone and it's printing out whatever comes back from google so let's see what comes out again crossing my fingers that i didn't mess up python of listen hello world how are you it's pretty good speech recognition it's using the cloud so to speak passing it up to google but now let's make things a little fancier and actually respond to the human so let me go back into here and add back some of the previous logic and say something like this if hello in words then go ahead and print out like before uh hello to you too l if uh how are you in the words that have come back from google go ahead and print out i am well thanks and down here if i said goodbye in words then go ahead and print out goodbye to u2 else if nothing comes back that i recognize let's just print out huh so if i did this right let's now go ahead and do python of listen.pi hello there oh damn it okay stand by uh oh sorry uh let me do a find and replace i called the variable words instead of audio and i just executed a fancy command to replace it everywhere so audio is what i meant to say this time now let's go ahead and run this python of listen.pi hello world damn it audio data is not iterable this is a bug give me one second to double check my notes very sorry to disappoint i'm audio in oh i did i sorry i did it right the first time but the wrong way let me change my variable back to words okay what i forgot to do was call one line of code here that's literally sitting in front of me i need to convert the recognizer's return value recognize google audio i need to store the return value of passing the audio to google and storing the resulting text here and so i have restored using the words variable here all right now let me go ahead and run python of listen.pie hello there very nice how are you today cool okay goodbye all right so there we have an even more compelling artificial intelligence granted it's not that intelligent it's just looking for pre-ordained strings but i bet we can do something even more and in fact let me go ahead and step aside and see if a colleague of mine can't help do something in real time on a big fancy pc here in the theater we are running some other python program on a cpu that's fast enough to do this in real time and we've connected one of our cameras to that pc so that what you're about to see is the result of one of our cameras being wired into this pc running that camera's input into python software running on that pc and we have trained the pc using this python software to recognize certain images in the past and let's see if we can't do this as well uh brian would you mind putting me on screen one and wrong shin do you want to go ahead and load up our first guest i think we are live so again you see my mouth moving in lockstep with uh einstein here his lips are matching mine his head movements are moving matching mine we can even be inquisitive if my eyebrows go up my mouth go through this way this way and you can see that the python program in real time is mapping my facial movements onto someone else's face of course otherwise known as a deep fake uh wrong shin could we try out brian's photo instead here now we have brian who similarly is matching a big smile gets a little fake at some point but again if we pre-rendered all of this instead of doing it live the pc could probably do an even better job uh how about could we uh invite harvard president larry bakau to join us wrong shin this is cs50 harvard university's introduction to the intellectual enterprises of computer science and the art of programming how about president peter salabe from yale wrong shin this is cs50 yale university's introduction to the intellectual enterprises of computer science and the art of programming now at this point the real world implications of this should be getting increasingly clear while it's all fun and games to do this on instagram and tick tock and the like using various mobile applications these days which are essentially doing the same thing and you can see the image doesn't quite keep up with me if i start moving a little too quickly right now this has very real world implications in the world of politics government business and really just the real world more generally because i'm essentially putting in someone else's mouth my own words and while it's clear that these examples thus far aren't really that compelling if i start to move too much you see that things start to get out of sync just imagine that if we wait one year our computers are going to be twice as fast with even more memory and the like software is only getting better and more powerful the libraries and the artificial intelligence is getting more trained and so among the themes for the coming weeks of the class is not just how to do some things with technology and how to write code but frankly asking the much bigger more important picture question of should you do certain things with technology and should you uh actually write such code we did ask president salovey and president back out for their permission in advance to spoof them in this way but we thought we would more playfully end with just a couple of other examples uh that you perhaps see on instagram tiktok and the like wrong shin could we invite pam to join us first and how about a certain gym all right that's it for cs50 and python today we'll see you next time [Music] [Music] [Music] you
Info
Channel: CS50
Views: 204,493
Rating: undefined out of 5
Keywords: cs50, harvard, computer, science, david, j., malan
Id: ZEQh45W_UDo
Channel Id: undefined
Length: 160min 46sec (9646 seconds)
Published: Thu Dec 31 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.