CS50 Video Player
    • 🧁

    • 🍨

    • 🍇

    • 🍿
    • 0:00:00Introduction
    • 0:25:28Python
    • 0:31:51Speller
    • 0:39:40Filter
    • 0:43:30Face Recognition
    • 0:47:35Functions
    • 1:00:09Types
    • 1:01:29Calculator
    • 1:11:28Conditionals
    • 1:14:48Compare
    • 1:23:23Object-Oriented Programming
    • 1:29:38Loops
    • 1:40:34Break
    • 1:54:37Meow
    • 2:02:06Truncation
    • 2:06:41Exceptions
    • 2:13:09Mario
    • 2:20:32Lists
    • 2:30:04Dictionaries
    • 2:41:03sys
    • 2:45:15pip
    • 0:25:28All right,
    • 0:25:29this is CS 50
    • 0:25:31this is finally week six.
    • 0:25:33And this is that week we promised,
    • 0:25:34wherein we finally transition from c this
    • 0:25:37lower level older language via which we've
    • 0:25:40explored memory and how computers work underneath
    • 0:25:42the hood to what's now called Python,
    • 0:25:44which is a more modern higher level language whereby we're still
    • 0:25:47going to be able to solve the same types of problems.
    • 0:25:49But it's going to suddenly start to get much,
    • 0:25:50much easier because what Python offers as do higher level languages more generally
    • 0:25:56are what we might just
    • 0:25:57as like abstractions over the very low level ideas that you've
    • 0:26:01been implementing in sections and problem sets and so much more.
    • 0:26:04But recall from week zero, where we began,
    • 0:26:06this was our simplest of programs that just printed.
    • 0:26:08Hello World, things escalated quickly thereafter in week one,
    • 0:26:11where suddenly we had all of this new syntax,
    • 0:26:14but the idea was still the same of just printing out Hello World.
    • 0:26:17Well as of today, a lot of that distraction,
    • 0:26:20a lot of the visual distraction goes away entirely such that what used to be this in C
    • 0:26:26will now be quite simply this
    • 0:26:28in Python.
    • 0:26:29And that's a bit of a head fake in that
    • 0:26:31we're going to see some other fancier features of Python.
    • 0:26:33But you'll find that Python's popularity in large part derives from
    • 0:26:37just how relatively readable it is.
    • 0:26:40And also as we ultimately see,
    • 0:26:42just how exciting and filled the ecosystem among Python programmers is that,
    • 0:26:47is to say there's a lot more libraries,
    • 0:26:49there's a lot more problems that people have solved in Python that you
    • 0:26:52can now incorporate into your own programs in order to stand on their
    • 0:26:55shoulders and get real work done faster.
    • 0:26:57But recall though from C that we had a few
    • 0:26:59steps by which to actually compile that kind of code.
    • 0:27:02So we got into the habit of make to make our program called hello.
    • 0:27:05And then we've been in the habit of running it
    • 0:27:07with dot slash hello.
    • 0:27:08The effect of which of course is to like feed all of the
    • 0:27:11zeros and ones that compose the hello program into the computer's memory.
    • 0:27:15And in turn, the CPU,
    • 0:27:16we revealed that what make is really doing is something a little more specific,
    • 0:27:21namely running clang, the C language compiler specific
    • 0:27:24with some automatic command line arguments.
    • 0:27:27So as to output the name that you want link in the library that you want and so forth.
    • 0:27:32But with Python wonderfully,
    • 0:27:33we're going to get rid of those steps too and quite simply run it
    • 0:27:37as follows.
    • 0:27:38Henceforth,
    • 0:27:39our programs will no longer be in files ending in dot C suffice it
    • 0:27:42to say our files starting today are going to start ending with dot py,
    • 0:27:46which is an indication to the computer Mac Os Windows,
    • 0:27:49Linux or anything else that this is a Python program.
    • 0:27:52But unlike C
    • 0:27:54wherein we've been in the habit of compiling our code and running it,
    • 0:27:58compiling our code and running it.
    • 0:27:59Anytime you make a change with Python,
    • 0:28:01those two steps sort of get reduced into one such that
    • 0:28:04anytime you make a change and want to rerun your code,
    • 0:28:07you don't explicitly compile it anymore.
    • 0:28:09You instead just run a program called Python similar in spirit to Clang.
    • 0:28:14But whereas Clang is a compiler,
    • 0:28:16Python will see is not only the name of the language but the
    • 0:28:18name of program and the type of that program is that of interpreter,
    • 0:28:23an interpreter is a program that reads your code
    • 0:28:25top to bottom left to right and really does
    • 0:28:27what it says without having this intermediate step of
    • 0:28:31first having to compile it in zeros and ones.
    • 0:28:33So with that said, let me do this, let me flip over here to VS code and within VS code,
    • 0:28:38let me write my first Python program.
    • 0:28:40And as always, I can create a new file with the code command within VS code on
    • 0:28:44create this file called hello dot P for instance. And quite, quite simply,
    • 0:28:49I'm going to go ahead and simply do print
    • 0:28:52quote
    • 0:28:52unquote
    • 0:28:53hello
    • 0:28:53world and if I go down to my terminal window instead of compiling this,
    • 0:28:57I'm instead going to interpret this program by running
    • 0:29:00Python space in the name of the file.
    • 0:29:02I want Python to interpret hitting enter and Voila.
    • 0:29:05Now you see hello world. But let me go ahead and compare this
    • 0:29:09at left.
    • 0:29:10Let me also go ahead and bring back briefly a file called hello dot C and I'm
    • 0:29:15gonna do this as we did in the very first day of C where I included standard
    • 0:29:19IO dot H I did in main void.
    • 0:29:22I did inside of their print f quote unquote hello comma world backslash
    • 0:29:26and close quote semicolon. And let me go ahead and VS code.
    • 0:29:29And if you drag your file over to the right or the left,
    • 0:29:32you can actually split screen things if he if of help.
    • 0:29:35And what I've done here is, and let me hide my terminal window.
    • 0:29:37I've now compared these two files left and right.
    • 0:29:39So here's hello dot C from say week one, here's hello dot pi from week six now.
    • 0:29:45And the obvious the differences are perhaps obvious, but there's still some,
    • 0:29:48there's a subtlety, at least one subtlety beyond getting rid of lots of syntax.
    • 0:29:53What did I apparently omit from my Python
    • 0:29:55version even though it didn't appear to behave in
    • 0:29:58any buggy way. Yeah,
    • 0:30:01sorry
    • 0:30:02is
    • 0:30:02much
    • 0:30:03the library.
    • 0:30:04So I didn't actually have to include any kind of library
    • 0:30:07like the standard IO library print apparently in Python just works.
    • 0:30:12So I don't need to use main anymore.
    • 0:30:14So this main function to be clear,
    • 0:30:16we was required in C because that's what told the compiler what the
    • 0:30:19main part of your program is and you can't just start writing code.
    • 0:30:22Otherwise, what else do you see?
    • 0:30:25So there's no more semicolon wonderfully enough at the end of this line,
    • 0:30:28even though there was here
    • 0:30:30and things are getting a little more subtle. Now, what, what else?
    • 0:30:33So the new line?
    • 0:30:34So recall that in print, if, if you wanted to move the cursor to the next line,
    • 0:30:37when you're done printing, you had to do it yourself.
    • 0:30:40So it seems as though Python because when I interpreted this program,
    • 0:30:44I won't want to go, the cursor did move to the next line on its own.
    • 0:30:48They sort of reversed the default behavior.
    • 0:30:50So those are just some of the salient differences here.
    • 0:30:52One, you don't have to explicitly include standard libraries, so to speak,
    • 0:30:56like standard IO you don't need to define a main function anymore.
    • 0:31:00You can just start writing your code.
    • 0:31:02You don't need these parentheses, these curly braces print F is now called print.
    • 0:31:06It would seem and you don't need
    • 0:31:08the backslash.
    • 0:31:09N.
    • 0:31:10Now there is one thing that's also a little looser even though I didn't do it here,
    • 0:31:14even though in C it was required to use double quotes.
    • 0:31:18Anytime you, anytime you want to use a string. A K, a char star
    • 0:31:22in Python as with a lot of languages.
    • 0:31:24Nowadays, you can actually get away with just using single quotes so long as you are
    • 0:31:29consistent.
    • 0:31:30Uh generally speaking,
    • 0:31:31some people like this because you don't have to hold shift
    • 0:31:33and therefore you just hit one key instead of two.
    • 0:31:35So there's an argument in terms of efficiency.
    • 0:31:38However, if you want to use an apostrophe in your string, then you have to escape it.
    • 0:31:42And so in general, stylistically, I'll use double quotes in this way.
    • 0:31:46But things are getting a little looser now with Python,
    • 0:31:48whereby that's not actually a requirement.
    • 0:31:51But what's especially exciting with Python and really a lot of higher level
    • 0:31:55languages is just how much real work you can get done relatively quickly.
    • 0:31:59So you've just spent quite a bit of time daresay,
    • 0:32:01implementing your spell checker and implementing your own dictionary of sorts.
    • 0:32:06Well, let me propose that
    • 0:32:08maybe we should have asked you to do that
    • 0:32:10in Python instead of C why? Well, let me go ahead and do this.
    • 0:32:13Let me close these two tabs and reopen my terminal window.
    • 0:32:17Let me go into a directory called speller
    • 0:32:19that I downloaded in advance from for class.
    • 0:32:22And if I type Ls in here,
    • 0:32:23you'll notice that it's very similar to you spent time on with problems set five.
    • 0:32:27But the file extensions are different.
    • 0:32:29There's a dictionary dot pi instead of dictionary dot C, there's a speller dot pi
    • 0:32:33instead of a speller dot C and there's the exact same directories,
    • 0:32:36dictionaries and texts that we gave you for problems set five.
    • 0:32:40So let me just stipulate that I spent time implementing speller dot C in Python.
    • 0:32:45And so I gave it a name of speller dot pi,
    • 0:32:47but I didn't go about really implementing dictionary dot pi yet.
    • 0:32:52And so
    • 0:32:53why don't we go ahead and actually implement
    • 0:32:55dictionary dot pi together by doing this.
    • 0:32:57Let me clear my terminal do code dictionary dot pi.
    • 0:33:01And let me propose that we implement ultimately four functions.
    • 0:33:05And what are those functions going to be?
    • 0:33:07Well, they're going to be the check function, the load function,
    • 0:33:09the size function and the unload function.
    • 0:33:12But recall that in problem set five, you use, you implemented your own hash table.
    • 0:33:17And so while there isn't a hash table data type in Python,
    • 0:33:21I'm gonna go ahead and do this,
    • 0:33:22I'm gonna create a variable global variable in dictionary dot pi called words.
    • 0:33:26And I'm gonna make it a set
    • 0:33:28in the mathematical sense.
    • 0:33:29A set is a collection of things that won't contain,
    • 0:33:32duplicates any duplicates will be filtered out.
    • 0:33:34So I'm gonna now after that creating that one global variable,
    • 0:33:37I'm gonna create a function called check just as you did and check,
    • 0:33:41takes as input a word.
    • 0:33:42And if I want to check if a word is in that set of words, I can simply do word dot lower
    • 0:33:50uh in words.
    • 0:33:52And that's it.
    • 0:33:53Let me now define another function called load,
    • 0:33:55which recall took an argument which that,
    • 0:33:57which was the name of the dictionary you want to load into memory.
    • 0:34:00Here's how I might in Python open that file, whose variable name is dictionary,
    • 0:34:05I might give it a temporary name as file.
    • 0:34:08Literally,
    • 0:34:08I can then go into that collection or set
    • 0:34:11of words and I can update it to actually contain
    • 0:34:14that file after reading entirely and splitting all of
    • 0:34:18the lines they're in based on the new line.
    • 0:34:19So it's a big file of words.
    • 0:34:21I'm gonna split them on new lines so that all of
    • 0:34:24those words end up in that uh inside of that set.
    • 0:34:28And then I'm just gonna go ahead and return true.
    • 0:34:31I'm now gonna go ahead and define a size function just as you did.
    • 0:34:34But in Python,
    • 0:34:35I'm gonna go ahead and just go ahead and
    • 0:34:36return the length of that set of words where length
    • 0:34:40Len is a function itself in Python. And I'm going to do one last function.
    • 0:34:44It turns out that in Python, even though for this program,
    • 0:34:48I'm going to go and implement a function called unload.
    • 0:34:50There's not actually anything to unload in Python
    • 0:34:53because Python will manage your memory for you.
    • 0:34:56Malo
    • 0:34:56is gone, free is gone. Pointers are gone.
    • 0:35:00It handles all of that seemingly magically for now for you,
    • 0:35:04but that's it here. I give you problem set five
    • 0:35:07in Python.
    • 0:35:09And I dare say I use more words verbally than I did actually type out.
    • 0:35:12And let me go ahead and now run this. Let me go into the same directory.
    • 0:35:15Let me run Python of dictionary
    • 0:35:17uh pipe. Whoops.
    • 0:35:19Let me go ahead and run Python
    • 0:35:20of speller dot pie because uh speller is still the name of the program.
    • 0:35:25And for text, let's go ahead and choose a big one like the Sherlock Holmes text.
    • 0:35:29And let me go ahead and increase the size of my terminal window. Hit, enter
    • 0:35:32and darn it uh
    • 0:35:34open dictionary as file
    • 0:35:38invalid sent.
    • 0:35:39Oh Sorry.
    • 0:35:41Uh
    • 0:35:43Let me fix this real quick for the team if you don't mind.
    • 0:35:49Uh
    • 0:35:49That was very
    • 0:35:51uh
    • 0:35:52disappointing.
    • 0:35:53All right,
    • 0:35:54inside of my load function, I'm now gonna do this,
    • 0:35:56I'm gonna say with open dictionary as a variable called file.
    • 0:36:00And in there,
    • 0:36:01I'm gonna go ahead and update the set of words
    • 0:36:03to be the updated version of whatever is in this file
    • 0:36:07as a result of reading it and then splitting its lines
    • 0:36:10whereby this file has a big long column of words,
    • 0:36:13each of which is separated by a new line.
    • 0:36:15Split line is gonna split all of those into one big collection.
    • 0:36:19And then I'm gonna assume that was correct and
    • 0:36:20I'm just gonna go ahead and return true thereafter,
    • 0:36:23I'm gonna go ahead and implement size as well as unload and for the team,
    • 0:36:27we'll edit around that afterward if we could
    • 0:36:29leaving me now with just these four functions.
    • 0:36:31So here then is I claim what you could have done
    • 0:36:34with problems at five if implementing it in Python instead,
    • 0:36:37let me go ahead and open my terminal window.
    • 0:36:39Let me increase its size.
    • 0:36:41Let me run Python of speller dot P which is the name of the actual program,
    • 0:36:44not the dictionary per se that I implemented.
    • 0:36:46Let's run it on a file called Holmes dot text because that was a particular big file.
    • 0:36:51And if I hit enter, now,
    • 0:36:53we'll see hopefully the same output that you saw and see flying across the screen.
    • 0:36:58And eventually we should see that same summary at the
    • 0:37:01bottom as to how many words seem to be misspelled,
    • 0:37:03how many words were in the dictionary
    • 0:37:05and ultimately how fast this whole process was.
    • 0:37:10Now, the total amount of time required was 1.93 seconds,
    • 0:37:13which was actually longer than it seemed to take.
    • 0:37:15That's because we're doing this in the cloud and it was taking some
    • 0:37:17amount of time to send all of the text to my screen.
    • 0:37:20But the code was only taking 1.93 seconds total on the actual server.
    • 0:37:24And hopefully the same kinds of numbers line up with your own.
    • 0:37:28The difference being what I did not have to implement for this
    • 0:37:31spell checker is your own hash table is your own dictionary.
    • 0:37:34Literally beyond what I've done using Python here
    • 0:37:37with some of these built in features.
    • 0:37:40So why you see, why not always use Python?
    • 0:37:44Assuming that you prefer the idea of being able to whip up within seconds,
    • 0:37:49the entirety of problems at five.
    • 0:37:52How might you choose now
    • 0:37:54between languages?
    • 0:37:56And I apologize if you're harboring resentment that this wasn't a week earlier.
    • 0:38:01Y Python or YC,
    • 0:38:04any instincts,
    • 0:38:07any thoughts? Hopefully a reason?
    • 0:38:09Yeah, we're here.
    • 0:38:12Yeah.
    • 0:38:19Ah, really good conjecture.
    • 0:38:21So,
    • 0:38:21you always thought that Python was slower than C
    • 0:38:23and takes up more space than C odds are.
    • 0:38:25That's in fact correct.
    • 0:38:26So, even though ultimately this 1.93 seconds is still pretty darn fast.
    • 0:38:30Odds are, it's a little slower than the C version would have been.
    • 0:38:33It's possible too that my version in Python actually does
    • 0:38:37take up more ra M or memory underneath the hood.
    • 0:38:39Why?
    • 0:38:40Well,
    • 0:38:40because Python itself is managing memory for you and it doesn't necessarily
    • 0:38:45know a priori how much memory you're going to need you.
    • 0:38:48The programmer might and you,
    • 0:38:49the programmer writing and C allocated presumably exactly as much memory
    • 0:38:54as you might have needed last week with problem set five.
    • 0:38:57But Python's got maybe do its best to effort for you and
    • 0:39:00try to manage memory for you and there's gonna be some overhead,
    • 0:39:03the fact that I have so many fewer lines of code,
    • 0:39:06the fact that these lines of code sort of solve problem
    • 0:39:09set five for me means that Python or whoever invented Python,
    • 0:39:13they wrote lines of code to sort of give me this functionality.
    • 0:39:17And so if you think of Python as a middleman of sorts, it's doing more work for me.
    • 0:39:22It's doing more of the heavy lift. So it might take me a bit more time. But my gosh,
    • 0:39:26look how much time it has saved in terms of writing this code more quickly.
    • 0:39:30And arguably this code is even more readable or at least will be after today week six,
    • 0:39:35once you have an eye for the syntax and features of Python itself.
    • 0:39:40So beyond that, it turns out, you can do other things pretty easily as well.
    • 0:39:43Let me go back into my terminal window.
    • 0:39:45Uh Let me close this dictionary dot P.
    • 0:39:48Let me go into a folder called filter in which I have this
    • 0:39:51same bridge that we've seen in the past uh across the river there.
    • 0:39:55So here's a bridge. This is the original version of this particular photograph.
    • 0:39:58Suppose I actually want to write a program that blurs this.
    • 0:40:01Well, you might recall from problem of four,
    • 0:40:02you could write that same code in C by manipulating all of the red, the green,
    • 0:40:07the blue pixels that are ultimately composing that file.
    • 0:40:11But let me go ahead and
    • 0:40:12this instead, let me create a file called blur dot pi.
    • 0:40:16And in this file,
    • 0:40:17let me go ahead and just go ahead and
    • 0:40:20import a library.
    • 0:40:22So from the Python image library pil let me go ahead and import something
    • 0:40:27called image capital I and image filter capital I capital F below that,
    • 0:40:32let me go ahead and create a variable called
    • 0:40:34before and set that equal to the return value
    • 0:40:36of image dot Before passing in input the name of that photograph that I want to blur.
    • 0:40:44Then let me go ahead and create another variable called
    • 0:40:46after and set that equal to before dot filter.
    • 0:40:50And then in parentheses,
    • 0:40:51image filter spelled as before dot box blur and then we'll give it a value of 10.
    • 0:40:57How much do I want to blur it?
    • 0:40:58For instance, after that,
    • 0:41:00I'm gonna literally call after dot save and let's save it as a file called out dot B MP.
    • 0:41:06And that's it.
    • 0:41:07I propose that this is how you can now write code in Python
    • 0:41:09to blur in image much like you might have for problem set four.
    • 0:41:13Let me go ahead and run blur dot P
    • 0:41:16hitting enter
    • 0:41:17uh
    • 0:41:17there. I've got uh I made a mistake here because I'm not
    • 0:41:22paying close enough attention to what I'm typing image dot Before bridge. Oh
    • 0:41:28oh Damn it.
    • 0:41:29I'm sorry,
    • 0:41:31how can we best fix it? I'm gonna do this. I don't want to go through all of that again.
    • 0:41:35So I'm gonna do before equals image dot open, quote unquote bridge dot B MP.
    • 0:41:41And then I'm gonna go ahead and declare a variable called after.
    • 0:41:44And for the team, if we can use that in the real version,
    • 0:41:47let me go ahead now,
    • 0:41:49sorry, movie magic to save some time.
    • 0:41:51Now, let me go ahead in my terminal window and run Python of blur dot P.
    • 0:41:56When I hit, enter those four lines of code will run,
    • 0:41:59it seems to have happened quite quickly.
    • 0:42:00Let me go ahead and open now out dot B MP.
    • 0:42:03And whereas the previous image looked like this a moment ago,
    • 0:42:07let me go ahead and open out dot B MP.
    • 0:42:10And hopefully, you can indeed see that it blurred it for me using that same code.
    • 0:42:15And if we want things to escalate a little more quickly, let me go ahead and do this.
    • 0:42:18Instead. Let me close blur dot B MP.
    • 0:42:20Let me go ahead and open a file called edges dot P and maybe in edges dot P,
    • 0:42:24we can use the same library.
    • 0:42:26So from the Python image library,
    • 0:42:28import image and import image filter.
    • 0:42:32Let me go ahead and create another variable called before set it
    • 0:42:34equal to image dot open quote unquote bridge dot B MP.
    • 0:42:38Just like before,
    • 0:42:39let me create another variable called after set that equal to before dot filter
    • 0:42:44image filter dot find
    • 0:42:46edges, which comes with this library automatically.
    • 0:42:50And lastly the same thing, save this as a file called out dot B MP.
    • 0:42:54So if you struggled perhaps with this one previously,
    • 0:42:57whereby you wrote for the more comfortable
    • 0:42:59version of problem set for edge detection,
    • 0:43:02so to speak.
    • 0:43:02Well, you might have then created a file that given an input like this.
    • 0:43:07The original bridge dot bm.
    • 0:43:09This new version out dot B MP with just four lines of code now looks like this.
    • 0:43:15So again,
    • 0:43:15if this is a little frustrating that we had to do all of this and see that was
    • 0:43:19sort of exactly the point to motivate that you
    • 0:43:22now understand nonetheless what's going on underneath the hood.
    • 0:43:25But with Python,
    • 0:43:25you can express the solutions to problems all the more efficiently,
    • 0:43:29all the more readily and just one last one too,
    • 0:43:32it's very common nowadays in the world of photography and social media
    • 0:43:36and the light to do face detection for better or for worse.
    • 0:43:39And it turns out that face detection,
    • 0:43:41even if you want to integrate it into your own application is something
    • 0:43:43that lots of other people have integrated into their applications as well.
    • 0:43:47So Python to my point earlier of having this
    • 0:43:49very rich ecosystem of libraries that other people wrote.
    • 0:43:53You can literally run a command like
    • 0:43:55pip install face underscore recognition if you want to add
    • 0:44:02support to your code space or to your programming environment,
    • 0:44:05more generally for the notion of of face recognition.
    • 0:44:09In fact, this is going to automatically install from some server elsewhere,
    • 0:44:11a library that someone else wrote called face recognition.
    • 0:44:15And with this library, you can do something like this.
    • 0:44:18Let me go into a directory that I came with in advance.
    • 0:44:21Let me go ahead and LS in there and you'll see four files, detect dot
    • 0:44:25P and recognize dot P which are going to
    • 0:44:27detect faces and then recognize specific faces respectively.
    • 0:44:31And then two files I brought from a popular TV show for instance. So if I open
    • 0:44:35office dot JPEG,
    • 0:44:36here is one of the early cast photos from the hit TV series, the office
    • 0:44:41and here is a photograph of someone specif
    • 0:44:44specific from the show. Uh Toby.
    • 0:44:47Now this is of course Toby's face.
    • 0:44:50But what is it that makes Toby's face a face
    • 0:44:55more generally if I open up office dot Jpeg and I asked you the human
    • 0:44:59to identify all of the faces in this picture,
    • 0:45:02wouldn't be that hard with a marker to sort of circle all of the faces.
    • 0:45:05But how, why,
    • 0:45:06how do you as humans detect faces, might you think? Yeah,
    • 0:45:12features, yeah, like eyes, nose generally in a similar orientation,
    • 0:45:16even though we all have different faces, ultimately,
    • 0:45:18but there's a pattern to the sort of uh to the shapes that you're seeing in screen.
    • 0:45:22Well,
    • 0:45:22it turns out this face recognition library has been trained
    • 0:45:25perhaps via artificial intelligence over time to recognize faces,
    • 0:45:29but any number of different faces perhaps among these folks here.
    • 0:45:32So if I go back into my terminal window here,
    • 0:45:35let me go ahead and run
    • 0:45:37uh say Python of detect dot P,
    • 0:45:40which I wrote in advance, which uses that library
    • 0:45:43and what that program is gonna do, it's gonna
    • 0:45:45think, do some thinking. It's just found some face.
    • 0:45:49And let me go ahead now and open a file.
    • 0:45:51It just created called detected dot JPEG,
    • 0:45:54which I didn't have in my folder a moment ago.
    • 0:45:56But when I opened this here file,
    • 0:45:58you'll now see all of the faces based on this library's detection thereof.
    • 0:46:03But suppose that we're looking for a very specific face among them, maybe Toby's,
    • 0:46:07well, maybe if we write a program that doesn't just take as input the office dot Jpeg.
    • 0:46:11But a second input Toby dot Jpeg,
    • 0:46:14maybe this library and code more generally
    • 0:46:17can distinguish Toby's face from gyms from Pam's
    • 0:46:20from everyone else in the show just based on this one piece of training data,
    • 0:46:25so to speak.
    • 0:46:26Well, let me instead run Python
    • 0:46:28of recognize dot pi and hit enter.
    • 0:46:31It's gonna do some thinking, some thinking,
    • 0:46:33some thinking and it is going to output now, a file called Recognized dot JPEG,
    • 0:46:39which should show me
    • 0:46:41his face ideally specifically.
    • 0:46:44And so what has it done?
    • 0:46:45Well with sort of a green marker there is Toby among all
    • 0:46:49of these faces that's maybe a dozen or so lines of code,
    • 0:46:52but it's built on top of this ecosystem of libraries.
    • 0:46:55And this is again, just one of the reasons why Python is so popular.
    • 0:46:58Undoubtedly, some number of years from now,
    • 0:47:00Python will be out and something else will be
    • 0:47:02in.
    • 0:47:02But that's indeed among the goals of CS 52 is not to teach you c not to teach you Python,
    • 0:47:06not in a couple of weeks to teach you javascript and other languages too,
    • 0:47:10but to teach you how to program and indeed all of the
    • 0:47:13ideas we have explored and we will now explore more today.
    • 0:47:16You'll see recurring for languages
    • 0:47:18in the years to come.
    • 0:47:20Any questions before we now dive into how it is this
    • 0:47:23code is working and why I typed the things that I did
    • 0:47:27before we forge ahead. Any questions along these lines,
    • 0:47:33anything at all?
    • 0:47:35No. All right. So
    • 0:47:37how does Python itself work?
    • 0:47:39Well,
    • 0:47:39let's do a quick review as we did when we
    • 0:47:41transition from scratch to c this time though from scratch,
    • 0:47:44say to Python.
    • 0:47:44So in Python, as with many languages, there are these things called functions,
    • 0:47:48the actions and verbs that actually get things done.
    • 0:47:50So here on the left,
    • 0:47:51recall from week zero was the simplest of functions we played with first,
    • 0:47:54the C block which just literally has the cat say
    • 0:47:57something on the screen we've seen in C for instance,
    • 0:48:00the equivalent line of code is
    • 0:48:02arguably this year with print f with the parentheses,
    • 0:48:05the quotation marks the backslash and the semicolon in Python.
    • 0:48:08Now it's going to indeed be a little simpler than that.
    • 0:48:11But the idea is the same as it was back in week zero libraries.
    • 0:48:15So we've seen already in C and now we've already seen in Python that these
    • 0:48:19things exist too in the world of C we're called that besides the standard ones,
    • 0:48:23like standard
    • 0:48:23IO dot H that header file, we very quickly introduced CS 50 dot A
    • 0:48:28which was like your entry point, the header file for the CS 50 library,
    • 0:48:31which gave you a bunch of functions as well.
    • 0:48:33Well,
    • 0:48:33we're gonna give you a similar library for at least
    • 0:48:35the next week or two training wheels for Python specifically
    • 0:48:39that again will take off so that you can stand on your own even with CS 50 behind you.
    • 0:48:43But the syntax for using a library in Python is a little different.
    • 0:48:47You don't include a dot H file, you just import instead
    • 0:48:51the name of the library.
    • 0:48:53All right, what does that actually mean?
    • 0:48:55Well, if there are specific functions in that library you want to use
    • 0:48:58in Python, you can be more precise.
    • 0:49:00You don't just have to say, give me the whole library for efficiency purposes.
    • 0:49:04You can say, let me import the get string function from the CS 50 library.
    • 0:49:10So you have finer grain control in Python,
    • 0:49:12which can actually speed things up if
    • 0:49:14you're not loading things unnecessarily into memory,
    • 0:49:16if all you want is say one feature there in.
    • 0:49:19So here, for instance, in scratch
    • 0:49:21was an example of how we might use not only a built in function like the C block or in C
    • 0:49:27in uh uh the print F but how we might
    • 0:49:30similarly now do the same but achieve this in Python.
    • 0:49:34So how might we do this? Well, in Python,
    • 0:49:37will you or rather in C this code looks a little something like this.
    • 0:49:40Back in week one, we declared a variable of type string.
    • 0:49:43Even though later we revealed that to be Char star,
    • 0:49:45I gave this variable name of answer for parody with scratch.
    • 0:49:49Then we use CS 50 zone get string function and ask for instance,
    • 0:49:52the same question as in the white oval here.
    • 0:49:54And then using this placeholder syntax,
    • 0:49:57these format codes which was print F specific,
    • 0:50:00we could plug in that answer to this premade string where the percent S is
    • 0:50:05and we saw percent I and percent F and a bunch of others as well.
    • 0:50:08So this is sort of how in C you
    • 0:50:10approximate the idea of concatenating two things together,
    • 0:50:13joining two things just as we did here in scratch.
    • 0:50:17So in Python, it turns out it's not only going to be a little easier,
    • 0:50:20but there's going to be even more ways to do this.
    • 0:50:23And so even what might seem today like a lot of different syntax,
    • 0:50:27it really is just different ways stylistically to achieve the same goals.
    • 0:50:30And over time as you get more comfortable with this,
    • 0:50:32you too will develop your own style or if
    • 0:50:35working for a company or working with the team,
    • 0:50:37you might collectively decide which conventions you want to use.
    • 0:50:41But here, for instance, is one way you could implement the same idea
    • 0:50:45in scratch. But in Python instead,
    • 0:50:48so notice I'm gonna still use a variable called answer.
    • 0:50:50I'm gonna use CS fifties function called get string.
    • 0:50:53I'm still gonna use quote unquote. What's your name?
    • 0:50:55But down here is where we see the most difference.
    • 0:50:58It's again, not called print f in Python. It's now called just print.
    • 0:51:02And what might you infer the plus operator is doing here?
    • 0:51:06It's not addition, obviously in a mathematical sense,
    • 0:51:10but those of you who have perhaps programmed before,
    • 0:51:12what is the plus represent in this context?
    • 0:51:16It's indeed joining the two strings together.
    • 0:51:18So this is indeed a concatenating the thing on the left with the thing on the right.
    • 0:51:22So you don't use the placeholder.
    • 0:51:24In this particular scenario, you can instead a little more simply just use plus,
    • 0:51:28but you want your grammar to line up.
    • 0:51:30So I still have hello comma space and then close
    • 0:51:34quote because I want to form a full phrase.
    • 0:51:37Um Notice too,
    • 0:51:37there's also one other slightly more subtle difference on the first line
    • 0:51:42besides the fact that we don't have a semicolon, what else is different?
    • 0:51:48I didn't declare the type of the variable.
    • 0:51:49So Python still has strings as we'll see,
    • 0:51:52but you don't have to tell the interpreter what
    • 0:51:55type of variable it is and this is going
    • 0:51:57to save up some keystrokes and it's just gonna
    • 0:51:59be a little more user friendly over time.
    • 0:52:01Meanwhile, you can do this also a little bit differently.
    • 0:52:04If you prefer,
    • 0:52:05you can instead trust that the print function in
    • 0:52:08Python can actually do even more for you automatically.
    • 0:52:11The print function in Python
    • 0:52:13can take
    • 0:52:14multiple arguments separated by commas in the usual way.
    • 0:52:17And by default,
    • 0:52:18Python is going to insert for you a single
    • 0:52:20space between its first argument and its second argument.
    • 0:52:23So notice what I've done here is my first argument is quote unquote,
    • 0:52:26hello with a comma but no space.
    • 0:52:29Then outside of the quotes,
    • 0:52:30I'm putting a comma because that just means here comes my
    • 0:52:32second argument and then I put the same variable as before.
    • 0:52:36And I'm just going to let Python figure out that it should by default per
    • 0:52:40documentation can uh join these two variables,
    • 0:52:43putting a single space in between them can do this yet another way.
    • 0:52:47And this way looks a little weirder.
    • 0:52:50But this is actually probably the most common way nowadays in Python is
    • 0:52:54to use what's called a format string or F string for short.
    • 0:52:58And this looks weird to me still, it looks weird.
    • 0:53:01But if you prefix a string in Python with an F literally,
    • 0:53:06you can then use curly braces inside of that string in Python and Python will,
    • 0:53:11I'll print out literally a curly brace and a close curly brace,
    • 0:53:14it will instead interpolate whatever is inside of those curly braces.
    • 0:53:18That is to say if answer is a variable
    • 0:53:20that has some value like David or something like that
    • 0:53:23saying F before the first quotation mark.
    • 0:53:26And then using these curly braces there in is going
    • 0:53:29to do the exact same thing of creating a string
    • 0:53:31that says hello comma space David. So it's
    • 0:53:35plug in the value for you.
    • 0:53:36So you can kind of think of this as percent S
    • 0:53:38but without that second step of having to like keep track
    • 0:53:41of what you want to plug back in for percent S
    • 0:53:44instead of percent S you literally put in curly braces.
    • 0:53:46What do you want to put right there, you format the string
    • 0:53:49yourself. So given all of those ways,
    • 0:53:53how might we actually go about implementing this or using this ourselves?
    • 0:53:58Well, let me propose that we do
    • 0:54:00this here. Let me propose that
    • 0:54:03I go back to BS code. Let me go ahead and open up uh hello dot
    • 0:54:07P again.
    • 0:54:09And as before, instead of just printing out something like uh quote
    • 0:54:13unquote.
    • 0:54:13Hello world, let me actually print out something a little more interesting.
    • 0:54:17So let me go ahead and from
    • 0:54:19the CS 50 library import the function called get string.
    • 0:54:23Then let me go ahead and create a variable called answer.
    • 0:54:26Let me set that equal to the return value of get string with as an argument,
    • 0:54:30quote unquote.
    • 0:54:31What's your name? Question mark
    • 0:54:35and then no semicolon at the end of that line.
    • 0:54:37But on the next line, frankly, here, I can pick any one of those potential solutions.
    • 0:54:41So let me start with the first. So hello
    • 0:54:44comma space, quote plus answer.
    • 0:54:47And now if I go down to my terminal window and run Python of hello dot pi,
    • 0:54:52I'm prompted for my name.
    • 0:54:53I can type in Dav ID and Voila
    • 0:54:55that there then works or I can tweak this a little bit.
    • 0:54:58I can trust that Python will concatenate its 1st and 2nd argument for me,
    • 0:55:03but this isn't quite right. Let me go ahead and rerun Python of hello dot P
    • 0:55:08hit enter and type in David.
    • 0:55:09It's gonna be ever so slightly buggy sort of grammatically or visually if you will.
    • 0:55:14What did I do wrong here?
    • 0:55:16Yeah.
    • 0:55:16So I had, I left the space in there even though I'm getting one for free from print.
    • 0:55:20So that's an easy solution here.
    • 0:55:22But let's do it one other way after running this to be sure Dav ID.
    • 0:55:26And OK, now it looks like I intended.
    • 0:55:28Well, let's go ahead and use with that placeholder syntax.
    • 0:55:31So let's just pass in one bigger string is our argument.
    • 0:55:34Do hello comma and then in curly braces answer like this.
    • 0:55:40Well, let me go down to my terminal window and clear it.
    • 0:55:42Let me run Python of hell lot out pi and enter type in Dav ID and Voila.
    • 0:55:47OK. I made a mistake.
    • 0:55:50What did I do wrong here? Minor though? It seems to be. Yeah.
    • 0:55:53So the,
    • 0:55:54the stupid little f that you have to put before the
    • 0:55:56string to tell Python that this is a special string.
    • 0:55:59It's a format string or F string that it should additionally
    • 0:56:03format for you. So if I rerun this, after adding that F I can do Python of hello dot P.
    • 0:56:08What's your name? David?
    • 0:56:09And now it looks the way I might intend,
    • 0:56:13but it turns out in Python,
    • 0:56:15you don't actually need to use get string in C recall that we introduced that because
    • 0:56:19it's actually pretty annoying in C to get
    • 0:56:21strings in particular to get strings safely.
    • 0:56:25Recall those short examples we did with scan F not too long ago and scan F
    • 0:56:29kind of scans what the user types at the keyboard and loads it into memory.
    • 0:56:33But the fundamental danger with scan F when it comes to strings was what,
    • 0:56:38why was it dangerous to use scan F to get strings from a user?
    • 0:56:45Oh
    • 0:56:45yeah,
    • 0:56:49exactly.
    • 0:56:49What if they give you a really long string that you didn't allocate space for?
    • 0:56:53Because you're not gonna know as the programmer in advance how
    • 0:56:55long of a string the human is gonna type in.
    • 0:56:58So you might under uh you might undercut it and
    • 0:57:01therefore have too much memory being too many characters being put
    • 0:57:05into that memory there by giving you some kind of buffer
    • 0:57:07overflow which might crash the computer or minimally your program.
    • 0:57:11So it turns out now in C gets string was especially useful in Python.
    • 0:57:16It's not really that useful. All it does is use
    • 0:57:19a function that does come with Python called input.
    • 0:57:24And in fact,
    • 0:57:24the input function in Python for all intents and purposes is the
    • 0:57:27same as the get string function that we give to you.
    • 0:57:31But just to ease the transition from C to Python,
    • 0:57:34we implemented a Python version of get string nonetheless.
    • 0:57:37But this is to say if I go to VS code here and I just change get string
    • 0:57:41to input. And in fact, I even get rid of the CS 50 library at the top.
    • 0:57:46This too should work fine if I rerun Python fa
    • 0:57:49O dot P type in my name David and Voila,
    • 0:57:52I have that now working as well.
    • 0:57:55All right, questions
    • 0:57:56about this use of get string or input
    • 0:58:00or any of our syntax thus far
    • 0:58:05now. All right. Well, what about variables?
    • 0:58:08We've used variables already and we already
    • 0:58:10identified the fact that you don't have to
    • 0:58:12specify the type of your variables proactively even
    • 0:58:15though clearly Python supports strings thus far.
    • 0:58:17Well, in Python, here's how you might
    • 0:58:20declare a variable that not necessarily is assigned
    • 0:58:23like a string but maybe an integer instead.
    • 0:58:25So in scratch,
    • 0:58:26here's how you could create a variable called counter if you want
    • 0:58:28to count things and set it equal to zero in c,
    • 0:58:32what we would have done
    • 0:58:33is this in
    • 0:58:34counter equals zero semicolon. That's the exact same thing as in scratch.
    • 0:58:39But in Python, as you might imagine,
    • 0:58:41we can chip away at this and type out the same idea a little more easily.
    • 0:58:46One, we don't need to say in anymore. Two, we don't need the semicolon anymore.
    • 0:58:50And so you just do what you intend. If you want a variable, just write it out.
    • 0:58:54If you want to assign it a value, you use the equal sign.
    • 0:58:56If you want to specify that value, you put it on the right.
    • 0:58:59And just as in C, this is not the equality operator.
    • 0:59:01It's the assignment operator from right to left.
    • 0:59:04Recall that in scratch if you wanted to increment a variable by one or any value,
    • 0:59:09you could use this puzzle piece here.
    • 0:59:11Well, in C, you could do syntax like this, which again is not equality.
    • 0:59:16It's saying add one to counter and then
    • 0:59:19assign it back to the counter variable.
    • 0:59:22In Python, you can do exactly the same thing minus the semicolon.
    • 0:59:26So you don't need to use the semicolon here.
    • 0:59:28But you might recall that in C,
    • 0:59:30there was some syntactic sugar for this idea because it was pretty popular.
    • 0:59:33And so you could shorten this in C as you can in Python
    • 0:59:38to actually just this plus equals one will add to the counter variable,
    • 0:59:42whatever that value is,
    • 0:59:44but it's not all, uh steps forward.
    • 0:59:47You might be in the habit of using plus plus or minus minus. Sorry, those are not
    • 0:59:52available in Python. Why?
    • 0:59:54It's because the designers of Python decided that
    • 0:59:56you don't need them because this is,
    • 0:59:58gets the job done anyway.
    • 1:00:00But there's a question down here in front unless it was about the same. All right.
    • 1:00:04So that's one feature we're taking away,
    • 1:00:06but it's not such a big deal to do plus equal in this case.
    • 1:00:09Well, what about the actual types involved here?
    • 1:00:12Beyond actually being able to define variables?
    • 1:00:16We'll recall that in the world of C, we had at least these data types,
    • 1:00:21those that came with the language in particular.
    • 1:00:23And we played with quite a few of these over time
    • 1:00:26in Python. We're going to take a bunch of those away
    • 1:00:29in Python. You're only going to have access to a bull true or false.
    • 1:00:35A float, which is a real number with a decimal point,
    • 1:00:38typically an inch or an integer and a string now known as Stir.
    • 1:00:42So Python here sort of cut some corners,
    • 1:00:44feels like it's too long to write out strings.
    • 1:00:46So a string in Python is called Stir
    • 1:00:48str but it's the exact same idea.
    • 1:00:52The notice though that missing from this now, in particular are double
    • 1:00:57and long, which recall actually used more bits in order to store information.
    • 1:01:02We'll see that that might not necessarily be a bad thing.
    • 1:01:05In fact,
    • 1:01:05Python just simplifies the world to do different types of variables
    • 1:01:08but gets out of the business of you having to decide.
    • 1:01:10Do you want like a small in or a large in or
    • 1:01:13something along those lines? Well, let me go ahead and do this.
    • 1:01:16Let me switch back over to BS code here.
    • 1:01:19And why don't we actually try to play
    • 1:01:20around with some calculations using these data types?
    • 1:01:23And more, let me go ahead and propose that we implement
    • 1:01:26like we did way back in week
    • 1:01:29uh one, a simple calculator. So let me do this code of calculator dot C.
    • 1:01:36So I'm indeed going to do this in C first just so that we have a similar example at hand.
    • 1:01:42So I'm going to include standard
    • 1:01:44IO dot H here on the uh at the top,
    • 1:01:47I'm going to go ahead and do in main void inside of main.
    • 1:01:51I'm going to go ahead and declare a variable called X and set that equal to get
    • 1:01:55in.
    • 1:01:56And I'm going to prompt the user for
    • 1:01:58that
    • 1:01:58value X. But if I'm using get in recall that actually is from the CS 50 library.
    • 1:02:02So in C, I'm going to need CS 50 dot H still for this example.
    • 1:02:06But back in week one, I then did something else.
    • 1:02:08I then said give me another variable called Y set that equal to get
    • 1:02:12in
    • 1:02:12and set that equal to that pass in that prompt there.
    • 1:02:16And then lastly, let's just do something super simple,
    • 1:02:18like add two numbers together.
    • 1:02:20So in C I'll use print F, I'm going to go ahead and do percent I backslash
    • 1:02:24N as a placeholder and then I'm just going to
    • 1:02:27plug in X plus Y.
    • 1:02:28So all of that was in C so it was a
    • 1:02:31decent number of lines of code to accomplish that task.
    • 1:02:33Only three of which are really the logical part of my program.
    • 1:02:37Like these are the three that we're really interested in.
    • 1:02:39So let me instead now do this code of calculator dot
    • 1:02:43pi which is going to give me a new tab.
    • 1:02:45Let me just drag it over to the right. So I can view these side by side.
    • 1:02:49And in calculator dot pi,
    • 1:02:51let's do this from the CS 50 library import the get in function
    • 1:02:55which is also available.
    • 1:02:57Then let's go ahead and create a variable called X
    • 1:03:00and set it equal to the return value of get
    • 1:03:02in passing in the same prompt, no semicolon, no mention of in
    • 1:03:06let's then create a second variable Y set it equal
    • 1:03:08to get in prompt the user for Y as before,
    • 1:03:11no, in explicitly, no semicolon.
    • 1:03:14And now let's just go ahead and print out X plus Y.
    • 1:03:17So it turns out that the print function in Python
    • 1:03:20is further flexible that you don't need these format strings.
    • 1:03:23If you want to print out an integer,
    • 1:03:25just pass it in integer even if that integer is the sum of two other integers.
    • 1:03:29So it just sort of works as you might expect.
    • 1:03:32So let me go down into my terminal here. Let me run Python of calculator dot pi.
    • 1:03:36And when I hit enter, I'm prompted for X. Let's do one.
    • 1:03:39I'm prompted for why? Let's do two and voila,
    • 1:03:42I should see
    • 1:03:44three as the result. So no actual surprises there.
    • 1:03:49But let me go ahead and you know what, let's take away this training wheel, right?
    • 1:03:53We don't wanna keep introducing CS 50 specific things.
    • 1:03:56So suppose we didn't give you get in.
    • 1:03:59Well,
    • 1:03:59it turns out that get in is still doing bit of help for you
    • 1:04:03even though gets string was kind of a throw away and we could replace,
    • 1:04:05gets string with input.
    • 1:04:06So let's try the same idea. Let's go ahead and prompt the user for input
    • 1:04:11for both X and Y using the input function in Python instead of get
    • 1:04:16in from CS 50. Let me go ahead and rerun Python of calculator dot pi and hit enter
    • 1:04:21so far. So good. Let me type in one.
    • 1:04:23Let me type in two. And what answer should we see?
    • 1:04:27Hopefully still three. But
    • 1:04:29nope.
    • 1:04:30Now the answer is 12
    • 1:04:34or is it,
    • 1:04:35why am I seeing 12 and not three?
    • 1:04:40Yeah. So we're, it's actually concatenating what seemed to be two strings.
    • 1:04:43So if you actually read the documentation for the input function.
    • 1:04:46It's behaving exactly as it's supposed to.
    • 1:04:48It is getting input from the user from their keyboard.
    • 1:04:51But anything you type at the keyboard is effectively a string.
    • 1:04:54Even if some of the symbols happen to look like or actually be decimal numbers,
    • 1:04:57they're still going to come to you as strings.
    • 1:05:00And so X is a string. A K A stir, Y is a stir and we've already seen that if you
    • 1:05:05plus in between two strings or stirs, you're gonna get concatenation,
    • 1:05:10not addition.
    • 1:05:11So you're not seeing 12 as much as you're seeing
    • 1:05:14+12,
    • 1:05:15not 12. So how can we fix this?
    • 1:05:18Well, in C,
    • 1:05:19we had this technique where we could cast one
    • 1:05:21thing to another by just putting like in,
    • 1:05:23in parenthesis.
    • 1:05:24For instance,
    • 1:05:25in Python,
    • 1:05:25things are a little higher level such that you can't
    • 1:05:27quite get away with just casting one thing to another
    • 1:05:31because a string recall is not necessary is not the same thing as a char.
    • 1:05:37A string has one, a zero or more characters. A char always has one.
    • 1:05:41And in C, there was a perfect mapping between single characters and single
    • 1:05:46uh numbers in decimal like 65 for capital A.
    • 1:05:49But in Python, we can do something somewhat similar
    • 1:05:52and not so much cast, but convert this input to an end
    • 1:05:56and con convert this input to an end.
    • 1:05:59So just like you see, you can nest functions,
    • 1:06:01you can call one function and pass its output as the input to another function.
    • 1:06:06And this now will convert X and Y to integers.
    • 1:06:09And so now plus is going to behave as you should as you would expect.
    • 1:06:12Let me rerun Python of calculator dot pi type in one type in two.
    • 1:06:16And now we're back to seeing three as the result.
    • 1:06:20If this is a little unclear this nesting, let me do this one other way.
    • 1:06:23Instead of just passing inputs, output into in,
    • 1:06:27I could also more pedantically do this
    • 1:06:29X should actually equal in of XY should actually equal in of Y.
    • 1:06:36This would be the exact same effect.
    • 1:06:38It's just like two extra lines where it's not really necessary,
    • 1:06:41but that would work fine.
    • 1:06:42If you don't like that approach, we could even do it in line.
    • 1:06:45We could actually convert X to an NT
    • 1:06:48Y to an ant Y. Well,
    • 1:06:50in int in the context of Python itself is a function and it takes as input here,
    • 1:06:55a string
    • 1:06:56or a stir
    • 1:06:57and returns to you
    • 1:06:59the numeric, the integral equivalent.
    • 1:07:02So similar idea, but it's actually a function.
    • 1:07:05So all of the syntax that I've been tinkering with here is sort of
    • 1:07:08fundamentally the same as it would be in C but in this case,
    • 1:07:11we're not casting but
    • 1:07:13converting
    • 1:07:14more specifically.
    • 1:07:15Well, let me go back to these data types.
    • 1:07:17These are some of the data types that are available to us.
    • 1:07:21In Python turns out there's a bunch of others as well that we'll start to dabble with.
    • 1:07:24Today.
    • 1:07:25You can get a range of values,
    • 1:07:26a list of values which is going to be like an array but better tuples which are
    • 1:07:31kind of like X comma Y often combinations
    • 1:07:33of values that don't change dict for dictionary.
    • 1:07:37It turns out that
    • 1:07:38in Python, you get dictionaries, you get hash tables for free,
    • 1:07:43they're built into the language.
    • 1:07:44And we already saw that Python also gives you a data type known as a set,
    • 1:07:47which is just a collection of values that gives
    • 1:07:50you uh gets rid of any duplicates for you.
    • 1:07:52And as we saw briefly in speller and we'll play more with these ideas soon,
    • 1:07:56it's gonna actually be pretty darn easy to get values
    • 1:07:59or check for values in those their data types.
    • 1:08:02So that in C we may, we were able to get input easily.
    • 1:08:05We had all of these functions in the CS 50 library for Python.
    • 1:08:08We're only gonna give you these instead, they're gonna be the same name.
    • 1:08:11So it's still get string, not get stir
    • 1:08:13because we wanted the functions to remain named the same.
    • 1:08:16But get float, get in, get string, all exist.
    • 1:08:18But again, get string is not all that useful but get
    • 1:08:22in
    • 1:08:22and get
    • 1:08:24float actually are. Why? Well, let me go back to VS code here
    • 1:08:29and let me go back to the second version of this program
    • 1:08:32whereby I proactively converted each of these return values to integers.
    • 1:08:39So recall that this is the solution to the 12 problem. And to be clear if I run
    • 1:08:44Python of calculator dot pi and input one and two, I get back now three as expected.
    • 1:08:50But what I'm not showing you is that there's still potentially a bug here.
    • 1:08:54Let me run Python of calculator dot pi
    • 1:08:56and let me just not cooper instead of typing what looks like a number.
    • 1:08:59Let me actually type something that's clearly a string like cat.
    • 1:09:03And unfortunately, we're going to see the first of our errors,
    • 1:09:06the first of our runtime errors and this like NC is going to look cryptic at first,
    • 1:09:10but this is generally known as a trace back where it's going to trace back for you.
    • 1:09:14Everything your program just did, even though this one's relatively short.
    • 1:09:17And you'll see that calculator dot pi
    • 1:09:20line one, I didn't even get very far before there's an error.
    • 1:09:23And then with all of these
    • 1:09:24symbols here,
    • 1:09:25like this is a problem why invalid literal for in function with base 10 quote
    • 1:09:30unquote cat again, just like NC. It's very arcane.
    • 1:09:33It's hard to understand this the first time you read it.
    • 1:09:35But what it's trying to tell me is that cat is not an integer
    • 1:09:40and therefore the in function cannot convert it to an integer for you.
    • 1:09:44We're going to leave this problem alone for now.
    • 1:09:46But this is why again,
    • 1:09:47get ins looking kind of good and get floats
    • 1:09:50looking kind of good because those functions CS F
    • 1:09:52library will deal with these kinds of problems for you
    • 1:09:57now.
    • 1:09:57Just so you've seen it,
    • 1:09:58there's another way to import uh functions from these things.
    • 1:10:01If you were to use, for instance, in a program get flow, get in and get string,
    • 1:10:05you don't need to do three separate lines like this.
    • 1:10:08You can actually separate them a little more cleanly with commas.
    • 1:10:11And in fact,
    • 1:10:12if I go back to a version of this program here in
    • 1:10:15vs code whereby I actually do use the get in function.
    • 1:10:20So let me actually get rid of all this and use get
    • 1:10:22in as before, let me get rid of all this and use get
    • 1:10:25in as before previously.
    • 1:10:28The way I did this was by saying from CS 50 import get
    • 1:10:32in if you know in advance what function you want to use.
    • 1:10:35But suppose for whatever reason, you already have your own function name, get
    • 1:10:38in
    • 1:10:39and therefore it would collide with CS
    • 1:10:41zone.
    • 1:10:42You can avoid that issue too by just using that first statement we saw earlier,
    • 1:10:46just import the library itself,
    • 1:10:47don't specify explicitly which functions you're going to use.
    • 1:10:50But thereafter, and you could not do this in C
    • 1:10:54you could specify CS 50 dot get
    • 1:10:56in
    • 1:10:57CS 50 dot get
    • 1:10:58in
    • 1:10:59in order to go into the library access its get in function.
    • 1:11:03And therefore,
    • 1:11:03it doesn't matter if you or any number of
    • 1:11:05other people wrote an identically named function called get in
    • 1:11:09you're using here clearly
    • 1:11:11CS 50 zone. So this is again, just a more ways to achieve
    • 1:11:16the same uh solution
    • 1:11:18but with different syntax,
    • 1:11:20all right, any questions
    • 1:11:22about any of this syntax or features
    • 1:11:25thus far?
    • 1:11:28No. All right.
    • 1:11:29Well, how about maybe another example here whereby we revisit conditionals,
    • 1:11:35which was the way of implementing,
    • 1:11:36do this thing or this thing sort of proverbial forks in the road in scratch recall,
    • 1:11:40we might use building blocks like these to just check is X less than Y.
    • 1:11:44And if so
    • 1:11:45say so
    • 1:11:46in C this code looked like this and notice that
    • 1:11:49we had parentheses around the X and the Y,
    • 1:11:51we had curly
    • 1:11:52braces, even though I did disclaim that for single lines of code,
    • 1:11:55you can actually omit the curly braces.
    • 1:11:57But stylistically, we always include them in CS fifties code,
    • 1:12:00but you have the backslash N and the semicolon
    • 1:12:02in a moment, you're about to see the Python equivalent of this,
    • 1:12:06which is almost the same.
    • 1:12:07It's just a little nicer. This then is the Python equivalent thereof.
    • 1:12:12So what's different at a glance here? Just to be clear
    • 1:12:17what's different. Yeah.
    • 1:12:19So the conditional is not in parentheses, you can use parentheses,
    • 1:12:24especially if logically, you need to group things.
    • 1:12:26But if you don't need them, don't use them is Python's mindset.
    • 1:12:29What else has changed here?
    • 1:12:31Yeah.
    • 1:12:33No curly braces. Yes. So, no curly braces around this.
    • 1:12:35And even though it's one line of code, you just don't use curly braces at all. Why?
    • 1:12:40Because in Python indentation is actually really,
    • 1:12:43really important and we know from office hours and problem sets occasionally that,
    • 1:12:47you know,
    • 1:12:47if you forgot to run style 50 or you didn't manually format your code beautifully.
    • 1:12:51You know, C is not actually gonna care if everything is aligned on the left.
    • 1:12:55If you never once hit the tab bar tab character or the space bar C or specifically
    • 1:13:00lang isn't really gonna care.
    • 1:13:02But your teaching fellow,
    • 1:13:03your T A is going to care or your colleague in the
    • 1:13:05real world because your code is just a mess and hard to read
    • 1:13:08Python though because you are not the only ones in the world that might be uh
    • 1:13:13might have bad habits when it comes to style Python as a language decided that's it.
    • 1:13:18Like everyone has to indent in order for their code to even work.
    • 1:13:22So the convention is Python is to use four spaces.
    • 1:13:24So 1234 or hit tab and let it automatically convert to the same
    • 1:13:28and use a colon instead of the curly braces.
    • 1:13:32For instance, to make clear what is associated with this particular
    • 1:13:36conditional,
    • 1:13:37we can omit though the backslash N for per before we can omit the semicolon.
    • 1:13:40But this is essentially the Python version thereof here in C in scratch.
    • 1:13:45If you wanted to do an, if else like we did back in week zero in C,
    • 1:13:50it's very similar to the if,
    • 1:13:51except you add the elts clause and write out
    • 1:13:54an additional print f like this in Python.
    • 1:13:56We can tighten this up
    • 1:13:58if X less than Y colon. That's exactly the same first lines.
    • 1:14:01The same, all we're doing now is adding an LT and the second print line here.
    • 1:14:05How about in scratch if we had a three way fork in the road? If,
    • 1:14:09if t in
    • 1:14:10C it looks pretty much like that. If,
    • 1:14:13if LT in Python, we can tighten this up
    • 1:14:16and this is not a typo.
    • 1:14:18What jumps out at you is weird but you gotta just get used to it.
    • 1:14:22Yeah.
    • 1:14:23L if and honestly, like years later, I still can't remember if it's L if or else if,
    • 1:14:29because other languages actually do el si F.
    • 1:14:33Uh So, and now I probably now biased all of you to now questioning this,
    • 1:14:36but it's LF in Python.
    • 1:14:37Elif is not a typo. It's in the spirit of
    • 1:14:40let's just save ourselves some keystrokes. So L if is identical to LT I,
    • 1:14:45but it's a little tighter to type it this way.
    • 1:14:48All right.
    • 1:14:48So if we now have this ability to express conditionals,
    • 1:14:53what can we actually do with them?
    • 1:14:55Well, let me go over to VS code here
    • 1:14:57and let me propose that we revisit maybe another program from
    • 1:15:01before uh where we just compare two integers in particular.
    • 1:15:05So I'm in vs code, let me open up a file called say compare dot pi.
    • 1:15:09And in compare dot pi,
    • 1:15:11we'll use the CS 50 library just so we don't risk
    • 1:15:13any errors like if the human doesn't type an integer.
    • 1:15:16So we're going to go ahead and say from CS 50 import, get
    • 1:15:19in
    • 1:15:19and in compare dot pi, let's get two variables X equals get
    • 1:15:23in
    • 1:15:23and prompt the user for X.
    • 1:15:25So what's X question mark to be a bit more verbose, Y equals get in quote unquote.
    • 1:15:30What's why question mark?
    • 1:15:32And then let's go ahead and just compare these two values.
    • 1:15:34So if X is less than Y, then go ahead and print out with print
    • 1:15:38X is less than Y close quote.
    • 1:15:42L if X is greater than Y, go ahead and print out X is greater than Y
    • 1:15:49quote, else go ahead and print out
    • 1:15:52X is equal to Y.
    • 1:15:55So the exact same program,
    • 1:15:57but I've added to the mix getting a value of X
    • 1:16:00and Y let me run Python of compare dot pi enter.
    • 1:16:03Let's type in one for X two for YX is less than Y. Let's run it once more.
    • 1:16:08X is two, Y is one, X is greater than Y and just for good measure,
    • 1:16:12let's run it a third time.
    • 1:16:13X is one,
    • 1:16:14Y is one,
    • 1:16:15X is equal to y so the code daresay works exactly as you would expect as you would hope.
    • 1:16:21But it turns out that in the world of Python,
    • 1:16:24we're actually gonna get some other behavior that might
    • 1:16:27actually have been what you expected weeks ago.
    • 1:16:30Even though C
    • 1:16:31did not behave this way in the world of Python and in the world of strings, A K A stirs
    • 1:16:37strings actually behave more like you would expect.
    • 1:16:41So by that, I mean, this,
    • 1:16:42let me actually go back to this code and instead of using uh integers,
    • 1:16:47let me go ahead and get rid of, I could do get string.
    • 1:16:50But we said that that's not really necessary.
    • 1:16:52So let's just go ahead and change this to input.
    • 1:16:55And actually, you know what, let's start fresh. Let's give myself a string called S
    • 1:16:58and use the input function and ask the user for S
    • 1:17:02let's use another variable called T just because it
    • 1:17:04comes after S and use the input function.
    • 1:17:06Get T,
    • 1:17:08then let's compare if S and T are the same now a couple of weeks ago, this backfired.
    • 1:17:12And if I tried to compare two strings for equality,
    • 1:17:15it did not work. But if I do, if S equals equals T
    • 1:17:19print quote unquote, same else. Let's go ahead and print different.
    • 1:17:24I dare say in Python, I think this is gonna work as you would expect.
    • 1:17:28So Python of compare do pi let's type in cat and cat
    • 1:17:32and indeed, those are the same,
    • 1:17:34let me run it again and type in cat and dog respectively.
    • 1:17:37And those are now different.
    • 1:17:39But in C we always got different, different, different.
    • 1:17:44Even if I type the exact same word, be a cat or dog or high or anything else. Why in C
    • 1:17:50were S and T always different a couple of weeks ago.
    • 1:17:53Yeah,
    • 1:17:57exactly. In C string is the same thing as Char star, which is a memory address.
    • 1:18:02And because we had called get string twice, even if the human type,
    • 1:18:06the same things that was two different chunks of memory at two different addresses.
    • 1:18:10So those two char stars were just naturally always different.
    • 1:18:13Even if the characters at those addresses were the same,
    • 1:18:16Python is meant to be higher level.
    • 1:18:18It's meant to be a little more intuitive.
    • 1:18:19It's meant to be more accessible to folks who might not
    • 1:18:22necessarily know or want to understand those lower level details.
    • 1:18:25So in Python equals equals even for strings just works the way that you might expect.
    • 1:18:32But in Python,
    • 1:18:33we can do some other things too even more easily than we
    • 1:18:36could in C let me go back to VS code here.
    • 1:18:39Let me close compare dot pi and let's re implement a program from C called Agree
    • 1:18:44which allowed us to prompt the user for like a yes, no question.
    • 1:18:47Like do you agree to these terms and conditions or something like that?
    • 1:18:50So let's do code of Agree dot pi.
    • 1:18:53And with Agree dot P,
    • 1:18:54let me go ahead and
    • 1:18:56actually let's go ahead and do this.
    • 1:18:58Let me also open up a file that I came with in advance
    • 1:19:02and this is called Agree dot C and this is what we did some weeks ago
    • 1:19:07when we wanted to check whether or not the user had agreed to something or not.
    • 1:19:10So we used the CS 50 library, the standard IO library, we had a main function,
    • 1:19:14we used get char and then we used
    • 1:19:17equals a lot and we use the two vertical bars which meant
    • 1:19:20logical or is this thing true or is this thing true?
    • 1:19:23And if so print f agreed or not agreed. So this works and this is relatively simple.
    • 1:19:28It's like the right way to do it in C but notice it
    • 1:19:32was a little verbose because we wanted to handle uppercase and lowercase,
    • 1:19:36uppercase and lowercase.
    • 1:19:37So that did start to bloat the code admittedly.
    • 1:19:40So let's try to do the same thing in Python and see what we can do the same or different.
    • 1:19:45No pun intended. So let me do this in agree.
    • 1:19:48Do P why don't we try to get input from the user as before?
    • 1:19:52And I will use, I could use get string, but I'll go ahead and use uh input.
    • 1:19:56So S equals input. Do you agree?
    • 1:20:00Question mark in double quotes and then let's go
    • 1:20:03ahead and check if S equals equals capital Y
    • 1:20:08and it's not vertical bar.
    • 1:20:09Now, it's actually more readable,
    • 1:20:11more English like or S equals equals lower case
    • 1:20:14Y then go ahead and print out agreed as before
    • 1:20:18L if say I did it there, L if S equals equals capital N or S equals equals lowercase N,
    • 1:20:28go ahead and print out,
    • 1:20:30not agreed. So it's almost the same as the C version except that
    • 1:20:35I'm using literally o
    • 1:20:36instead of two vertical bars. So let's run this. So Python of agree dot pi enter.
    • 1:20:41Do I agree? Yes. For little Y let's do it again.
    • 1:20:44Python of agree dot pi capital Y yes, that works there.
    • 1:20:48And if I do it again with lower case N
    • 1:20:50and if I do it with capital N, this program too seems to work.
    • 1:20:54But what if I do this? Let me rerun Python of agreed dot P.
    • 1:20:58Let me type in.
    • 1:20:59Yes.
    • 1:21:00OK. Just ignores me, let me run it again. Let me type in. No,
    • 1:21:03just ignores me. Let me try it very emphatically. Yes.
    • 1:21:05In all caps, it just ignores me.
    • 1:21:07So there's some explosion of possibilities that ideally we should handle right.
    • 1:21:12This is bad user interface design.
    • 1:21:14If I have the user has to type Y or N,
    • 1:21:16even if yes and no in English or perfectly reasonable and logical too.
    • 1:21:21So how could we handle that?
    • 1:21:23Well, it turns out in Python,
    • 1:21:25we can use something like an array technically called a
    • 1:21:28list to maybe check a bunch of things at once.
    • 1:21:31So let me do this.
    • 1:21:33Let me instead say not equality.
    • 1:21:35But let me use the in keyword in Python and
    • 1:21:38check if it's in a collection of possible values.
    • 1:21:41Let me say if S
    • 1:21:42is in
    • 1:21:44and here comes in square brackets, just like uh
    • 1:21:48in square brackets,
    • 1:21:49uh quote unquote y comma quote
    • 1:21:52unquote. Yes,
    • 1:21:53then we can go ahead and print out agreed.
    • 1:21:57Uh L if S is in this list of values, lower case N or lower case, no,
    • 1:22:04then we can print out for instance, not agreed.
    • 1:22:07But this is a bit of a step backwards because now I'm only handling lowercase.
    • 1:22:13So let me go into the mix and maybe add capital, Y, wait a minute, then maybe capital,
    • 1:22:18yes then or maybe yes.
    • 1:22:20Also,
    • 1:22:21I mean weird but should probably support this and like, yes, I mean,
    • 1:22:25there's like a lot of combinations.
    • 1:22:27So this is not gonna end well
    • 1:22:29or it's just gonna bloat my code unnecessarily and eventually for longer words,
    • 1:22:33I'm surely gonna miss uh capitalization.
    • 1:22:36So logically, whether it's in Python or C or any language,
    • 1:22:39what might be a better design for this problem of handling why?
    • 1:22:44And yes,
    • 1:22:45but who cares about the capitalization
    • 1:22:49or, but also
    • 1:22:49check the first care.
    • 1:22:51So don't OK. So don't use capitals, you could only support uh lower case.
    • 1:22:56That's fine. That's kind of a cop out, right?
    • 1:22:58Because now like the program's usability is worse.
    • 1:23:01Oh, so we can convert it to lower case.
    • 1:23:03Yeah, though, I did hear you say we could just check the first letter.
    • 1:23:06I bet that's gonna get us into trouble.
    • 1:23:08And we probably don't want to allow any words starting with y any word
    • 1:23:12starting with N just because it logically
    • 1:23:15especially you want like the lawyers happy,
    • 1:23:16presumably you should probably get an explicit semantically correct
    • 1:23:19word like Y or N or yes or no.
    • 1:23:22But yeah, we can actually go about converting this to something maybe smaller.
    • 1:23:26But how do we go about converting this and C that alone
    • 1:23:29was gonna be pretty darn annoying because we'd have to sort of
    • 1:23:32use the, the two lower function on each, every,
    • 1:23:35on every character and compare it for equality.
    • 1:23:37Just feels like that's a bit of work.
    • 1:23:39But in Python, you're gonna get more functionality for free.
    • 1:23:43So there might very well be a function like in C called too lower or too upper.
    • 1:23:48But the weird thing about C perhaps in retrospect is that those functions
    • 1:23:52just kind of worked on the honor system too lower and too upper,
    • 1:23:55just trusted that you would pass them
    • 1:23:58an input an argument that is in fact a Char
    • 1:24:02in Python and in a lot of other higher level languages,
    • 1:24:06they introduce this notion of object oriented programming,
    • 1:24:10which is commonly described as oop.
    • 1:24:12And in the world of object oriented programming,
    • 1:24:15your values can not only your variables for instance,
    • 1:24:19and your data types can not only have values,
    • 1:24:21they can also have functionality built into them.
    • 1:24:25So
    • 1:24:25you have a data type like a string.
    • 1:24:27Frankly, it just makes good sense that strings should be uppercase,
    • 1:24:30lowercase,
    • 1:24:31capitalize and any number of other operations on strings.
    • 1:24:35So in the world of object oriented programming
    • 1:24:37functions like two upper and two lower and
    • 1:24:40is upper and is lower are not just in some random library that you can use,
    • 1:24:44they're built into the strings themselves.
    • 1:24:46And what this means is that
    • 1:24:48and the world of strings in Python here for instance is the URL of the
    • 1:24:52documentation for all of the functions otherwise
    • 1:24:55known as methods that come with strings.
    • 1:24:58So you don't go check for like ac type library like
    • 1:25:00we did in C you check the actual data type,
    • 1:25:03the documentation.
    • 1:25:04Therefore, and you will see in Python's own documentation, what functions A K
    • 1:25:08A methods come with strings.
    • 1:25:10So a method is just a function but it's a
    • 1:25:13function that comes with some data type like a string.
    • 1:25:16So let me propose that we do this in the world of object oriented programming,
    • 1:25:23we can come back to agree dot Pie and we can actually
    • 1:25:26improve the program by getting rid of this crazy long list,
    • 1:25:30which I wasn't even done with
    • 1:25:31and just canonical.
    • 1:25:33Everything is lower case.
    • 1:25:34So let's just check for lower case Y and lower case yes, lower case N lower case, no.
    • 1:25:39And that's it.
    • 1:25:39But to your suggestion, let's force everything that the user types into lower case.
    • 1:25:44Not because we want to permanently change their input,
    • 1:25:47we can throw the value away thereafter.
    • 1:25:49But because we want to more easily,
    • 1:25:51logically compare it for membership in this list of values.
    • 1:25:56So one way to do this would be to literally do S equals S dot Lower.
    • 1:26:02So here's the difference in the world of C,
    • 1:26:05we would have done this to lower and pass in the value S
    • 1:26:10but in the world of Python and in general object oriented programming,
    • 1:26:14Java is another language that does this if S is a string A K A stir.
    • 1:26:19Therefore, S is actually what's known in Python as an object.
    • 1:26:22And objects can not only have values or attributes inside of them but
    • 1:26:26also functionality built in and just like in C with a struct.
    • 1:26:29If you want to go inside of something
    • 1:26:31you use the dot operator and inside of this string, I claim is a function A K A method
    • 1:26:38called lower. Long story short.
    • 1:26:40The only takeaway if this is a bit abstract is
    • 1:26:42that instead of doing lower and then in parentheses,
    • 1:26:45S in the world of object oriented programming, you kind of flip that and you do S dot
    • 1:26:49name of the method and then open print,
    • 1:26:52close print if you don't need to pass in any arguments.
    • 1:26:55So this actually achieves the same.
    • 1:26:56So let me go ahead and get rid of, let me go ahead and rerun, agree dot pi.
    • 1:27:01And let me type in lowercase Y that works. Let me run it again. Type in lowercase. Yes,
    • 1:27:06that works. Let me run it again. Type in capital. Y
    • 1:27:10that works. Let me type in capital. Yes. All capital uh all uppercase. Yes,
    • 1:27:14that too works. Let me try. No.
    • 1:27:17Uh let me try. No, in lower case.
    • 1:27:19And all of these permutations now actually work
    • 1:27:23because I'm forcing it to lower case.
    • 1:27:25But even more interestingly in Python,
    • 1:27:27if you're sort of becoming a languages person,
    • 1:27:29if you have a variable s
    • 1:27:31that is being set the return value of input function and
    • 1:27:35then you're immediately going about changing it to lower case.
    • 1:27:38You can also chain method calls together in something like Python by doing this,
    • 1:27:43we can get rid of this line altogether
    • 1:27:45and then I can just do
    • 1:27:47dot lower
    • 1:27:48and so whatever the return value of input is, it's going to be a stir,
    • 1:27:52whatever the human types in you can then immediately force it to lower case and then
    • 1:27:56assign the whole value to this variable called
    • 1:27:59S you don't actually have to wait around
    • 1:28:01and do it on a separate line
    • 1:28:03altogether.
    • 1:28:06Questions then
    • 1:28:08on any of this?
    • 1:28:12All right, let me do one other. That's reminiscent of something we did in the past.
    • 1:28:16Let me go into VS code here clear my terminal. Let's close.
    • 1:28:19Both the C and the Python version of agree.
    • 1:28:22And let's create a program called uppercase dot pi whose purpose in life
    • 1:28:25is to actually uppercase a whole string in the world of C,
    • 1:28:28we had to do this character by character, by character.
    • 1:28:31And that's fine. I'm gonna go ahead and do it similarly here in Python whereby
    • 1:28:37I want to convert it character by character.
    • 1:28:39But unfortunately, before I can do that,
    • 1:28:42I actually need like some way of looping in Python,
    • 1:28:45which we actually haven't seen yet.
    • 1:28:46So we need one more set of building blocks.
    • 1:28:48And in fact, if we were to consult the Python documentation,
    • 1:28:51we'd see this and much more.
    • 1:28:52So. In fact, here's a list of all of the functions that come with Python.
    • 1:28:56It's actually not that long of a list because so much of the functionality
    • 1:28:59of Python is built into data types like strings and integers and floats.
    • 1:29:04And more. Um here is
    • 1:29:05canonical source of truth for Python documentation. So as opposed to using
    • 1:29:10the CS 50 manual for C, which is meant to be a simplified version of,
    • 1:29:15of publicly available documentation will generally for
    • 1:29:18Python point you to the official docs,
    • 1:29:20I will disclaim
    • 1:29:21they're not really written for like introductory students and they
    • 1:29:25will generally leave some detail off and use arcane language.
    • 1:29:28But at this point in the term, even if it might be a little frustrating at first,
    • 1:29:31it's good to see documentation in the real world because that's what you
    • 1:29:35have after the course. And so you'll get used to it through practice over time.
    • 1:29:39But with loops,
    • 1:29:40let's introduce one other feature that we can compare to scratch here.
    • 1:29:43For instance, the scratch is how we might have repeated something three times.
    • 1:29:46Like now on the screen in C,
    • 1:29:48there were a bunch of ways to do this and the clunkiest was maybe to do it
    • 1:29:52with a Y loop where we declare a variable called I set it equal to zero.
    • 1:29:56And then iteratively increment I again and again until it exceeds,
    • 1:30:00until it equals three each time printing out.
    • 1:30:03Meow.
    • 1:30:04In Python, we can do this
    • 1:30:06in a few different ways as well.
    • 1:30:08The nearest translation of C into Python is perhaps this,
    • 1:30:13it's almost the same and logically,
    • 1:30:14it really is the same but you don't specify in and you don't have a semicolon,
    • 1:30:18you don't have curly braces, but you do have a colon.
    • 1:30:21You don't use print, F, you use print
    • 1:30:24and you can't use I plus plus, but you still can use I plus equals one.
    • 1:30:28So logically exactly the same idea is in C, it's just a little tighter.
    • 1:30:32I mean, it's a little easier to read, even though it's very mechanical.
    • 1:30:36If you will, you're defining all of these,
    • 1:30:38you're defining this variable and changing it incrementally.
    • 1:30:40Well, recall that in C,
    • 1:30:42we could also use a for loop which at first
    • 1:30:44glance was probably more cryptic than a wild loop.
    • 1:30:46But odds are by now you're more comfortable
    • 1:30:48or more in the habit of using loops. Same exact idea
    • 1:30:51in Python though.
    • 1:30:52We might do it like this.
    • 1:30:54We've seen how in square brackets you can have lists of values like y yes.
    • 1:30:59Uh, and, and, and so forth. Well, let's just do the same thing with numbers.
    • 1:31:02So if you want Python to do something three times,
    • 1:31:05give it a list of three values like 012
    • 1:31:08and then print out hello world that many times
    • 1:31:12now. This is correct. But it's bad design
    • 1:31:15even if you've never seen Python before, like
    • 1:31:18extrapolate mentally from this.
    • 1:31:19Why is this probably not the right way or the best way to do this looping?
    • 1:31:28Yeah, if you want to do it four times, five times, 50 times 100 times, I mean,
    • 1:31:33surely there's a better way than enumerating all of these values.
    • 1:31:36And there is, in fact, in Python,
    • 1:31:38there's a function called range that actually returns to you very efficiently.
    • 1:31:43A range of values.
    • 1:31:44And by default, it hands you the number zero and then one and then two.
    • 1:31:48And if you want more than that,
    • 1:31:49you just change the argument to range to be how many values do you want?
    • 1:31:53So if you passed in range of 50 you would get back
    • 1:31:56zero through 49 which effectively allows you to do something 50 times in total.
    • 1:32:01So this is perhaps the most Python way so to speak. And this is actually a term of art.
    • 1:32:06Python isn't necessarily the only way to do something, but it's
    • 1:32:10the way to do something based on sort of consensus in the Python community.
    • 1:32:14So it's pretty common to do this. But there's some curiosity here.
    • 1:32:18Notice I'm declaring a variable. I,
    • 1:32:20but I'm never actually using it.
    • 1:32:23In fact, I don't even increment it because that's sort of happening automatically.
    • 1:32:26Well,
    • 1:32:26what's really happening here is automatically in
    • 1:32:29Python on every iteration of this loop.
    • 1:32:33Python is assigning I to the next value.
    • 1:32:36So initially I is zero, then it goes through an iteration, then I is one,
    • 1:32:40then I is two and then that's it if you only asked for three values.
    • 1:32:44But there's this other technique in Python just so, you know,
    • 1:32:47whereby if you're the programmer and you know,
    • 1:32:49don't actually care about the name of this variable,
    • 1:32:52you can actually change it to an underscore which has no functional effect per se.
    • 1:32:56It just signals to the reader, your colleague, your teaching fellow
    • 1:33:00that it's a variable and you need it in order to achieve a four loop,
    • 1:33:04but you don't care about the name of the variable
    • 1:33:05because you're not going to use it explicitly anywhere.
    • 1:33:08So that might be an even more Python way of doing things. But
    • 1:33:11if you're more comfortable seeing the eye and using the variable more explicitly,
    • 1:33:15that's fine
    • 1:33:16underscore does not mean anything special.
    • 1:33:18It's just a valid character for a variable name. So this is convention.
    • 1:33:22Nothing more technical than that.
    • 1:33:24What about a forever loop in scratch. Like literally meow forever.
    • 1:33:28Well,
    • 1:33:29over here we can just use and see while true print f meow again and again and again,
    • 1:33:35um in Python
    • 1:33:36it's almost the same.
    • 1:33:38You still get rid of the curly braces, you add the colon, you get rid of the semicolon.
    • 1:33:41But there's a subtlety,
    • 1:33:44what else is different here? Yeah.
    • 1:33:47So true is upper case why I, who knows?
    • 1:33:50Like the, the world decided that in Python,
    • 1:33:52true is capitalized and false is capitalized in many other languages.
    • 1:33:55They say most they are not. It's just a difference that you have to
    • 1:33:59uh keep in mind or remember.
    • 1:34:01All right.
    • 1:34:02So now that we have looping constructs,
    • 1:34:04let me go back to my code here and recall that I propose that
    • 1:34:08we re implement a program like uppercase force an entire string to uppercase.
    • 1:34:11And in C, we would have done this with like a four loop iterating from left to right.
    • 1:34:15But what's nice in Python frankly is that it's a lot easier to loop in Python
    • 1:34:20than it is in C because you can loop over anything
    • 1:34:24that is iterable like a string is iterable in the sense that
    • 1:34:27you can iterate over it from left to right. So what do I mean by this?
    • 1:34:31Well, let me go ahead and in uppercase dot pi,
    • 1:34:34let's first prompt the user for a variable called before
    • 1:34:37and set that equal to the return value of input
    • 1:34:40um giving them a prompt of before uh colon.
    • 1:34:43Uh Then let's go ahead as we did weeks ago and print
    • 1:34:46out just the word after just to make clear to the user,
    • 1:34:50uh what is actually going to be printed.
    • 1:34:54Uh Then let me go ahead and specify
    • 1:34:57the following loop four.
    • 1:34:59And previously you saw me use I, but because I'm dealing with characters,
    • 1:35:02I'm actually gonna do this instead for C in before colon print out
    • 1:35:08C dot upper.
    • 1:35:11And that's it.
    • 1:35:12Now, this is a little flawed. I will concede.
    • 1:35:14But let me run this Python of uppercase dot pi
    • 1:35:17let's type in something like cat, cat in all lowercase. Enter,
    • 1:35:22all right.
    • 1:35:22Well,
    • 1:35:22you see after and I did get it right in the
    • 1:35:25sense that it is capital C capital A capital T,
    • 1:35:28but it looks a little stupid.
    • 1:35:29And in order to fix this,
    • 1:35:30we actually need to introduce something that's called uh named parameters.
    • 1:35:35So let me actually go ahead and
    • 1:35:38propose that we can fix this problem
    • 1:35:41by actually passing in another argument to the print function.
    • 1:35:45And this is a little different syntactically from C.
    • 1:35:47But if I go back to VS code here,
    • 1:35:49it turns out that there's two aesthetic problems here.
    • 1:35:52One, I did not want the new line automatically inserted after, after why?
    • 1:35:56Because just like in week one, I want them to line up nicely or in week two.
    • 1:36:01Um And I don't want a new line after cat.
    • 1:36:04So even though at first glance a moment a bit ago,
    • 1:36:06it might have seemed nice that Python just does the backslash N for,
    • 1:36:09you can backfire if you don't actually want a new line every time.
    • 1:36:13So the syntax is going to look a little weird.
    • 1:36:15But in Python with the print function,
    • 1:36:18if you want to change the character that's
    • 1:36:21automatically used at the end of every line,
    • 1:36:24you can literally pass
    • 1:36:25the second argument
    • 1:36:27called end
    • 1:36:28and set it equal to something else.
    • 1:36:31So if you want to set it equal to something
    • 1:36:34else and that something else is nothing quote unquote,
    • 1:36:37then that's fine.
    • 1:36:38You can actually
    • 1:36:40specify and equals quote unquote down here too.
    • 1:36:44If you want to specify that the end at the
    • 1:36:46end of every one of these characters should be nothing
    • 1:36:49can specify N equals quote unquote. What this implies is that by default in Python,
    • 1:36:54the default value of this N parameter is actually always backslash
    • 1:36:58N.
    • 1:36:59So if you want to override it and take that away,
    • 1:37:02you just literally change it to quote unquote instead.
    • 1:37:05And now if I clear my, if I rerun this program uppercase, do
    • 1:37:09I type in cat in all lowercase? Now, you'll see.
    • 1:37:13I'm
    • 1:37:13022 minor bugs here. One was just stupid. I had one too many spaces here,
    • 1:37:18but you'll notice that I didn't move the cursor to the next line after
    • 1:37:22C was printed in all upper case and that we can fix by just printing nothing.
    • 1:37:26It turns out when you don't pass, print an argument at all,
    • 1:37:29it automatically gives you just the line ending nothing else.
    • 1:37:32So I think this will move the cursor as expected.
    • 1:37:35So let me clear it now,
    • 1:37:36run Python of uppercase dot P and hit enter type
    • 1:37:39in cat and all lowercase cross my fingers this time.
    • 1:37:41And now
    • 1:37:42I have indeed capitalized this character by character by character just like we did
    • 1:37:48in C
    • 1:37:49but honestly, this too not really necessary.
    • 1:37:52Turns out I don't need to loop over a
    • 1:37:54whole string because strings themselves come with methods.
    • 1:37:56And if you were to visit the documentation for strings,
    • 1:37:59you would see that indeed upper is a method that comes with every string.
    • 1:38:03And you don't need to call it on every character individually.
    • 1:38:07I could instead get rid of all of this
    • 1:38:09and just print out.
    • 1:38:11For instance, I can just print out
    • 1:38:15uh before
    • 1:38:17upper.
    • 1:38:18And the upper function that comes with strings
    • 1:38:20will automatically apply to every character they're in
    • 1:38:23and I think achieve the same result.
    • 1:38:25So let me go ahead and try this again, Python of uppercase dot P type in cat enter.
    • 1:38:29And indeed, it works exactly the same way.
    • 1:38:32Let me take this one step further, let me go ahead and combine a couple of ideas.
    • 1:38:36Now here, let me go ahead. And for instance, let me get rid of this last print line.
    • 1:38:42Let me change my logic to be after
    • 1:38:44equals the return value of this.
    • 1:38:46And now I can use one of those F strings and plug this in maybe here after
    • 1:38:51and I can get rid of the new line ending. I can specify this is an F string.
    • 1:38:54So I'm just changing this around a little bit logically.
    • 1:38:57So that now I have a variable called after that is the uppercase version of before.
    • 1:39:02And now if I do Python of uppercase dot pi type in cat
    • 1:39:06that too now works.
    • 1:39:07And if I actually let me add a space there,
    • 1:39:10if I run Python of uppercase P type in cat that too now works.
    • 1:39:14And lastly here,
    • 1:39:16if you don't want to bother creating another variable like this,
    • 1:39:19you can even put short bits of code inside of these format strings.
    • 1:39:24So I for instance,
    • 1:39:24could go in here into these curly braces and not just put a variable name,
    • 1:39:28I can actually put Python code inside of the curly braces inside of my string.
    • 1:39:34And so now if I run Python of uppercase dot P type in cat,
    • 1:39:37even that too
    • 1:39:39now works. Now, which one is the best,
    • 1:39:42this is kind of reasonable to put the bit of code inside of the string,
    • 1:39:45I would not start writing long lines of code inside of curly braces that start to
    • 1:39:49wrap no less because then it's just going to be a matter of bad style.
    • 1:39:52But this again is to say that there's a bunch of different ways
    • 1:39:56to solve each of these problems. And so up until now, we've generally seen
    • 1:39:59not named parameters. N is the first parameter we've ever seen that has a name.
    • 1:40:05Literally end
    • 1:40:06up until now in C and up until a moment ago in Python,
    • 1:40:09we've always been assuming that our parameters
    • 1:40:12are positional. What matters is the order in which you specify them,
    • 1:40:17not necessarily
    • 1:40:19something else.
    • 1:40:21OK. That was a lot.
    • 1:40:23Any questions
    • 1:40:25about any of this here?
    • 1:40:28No. All right. Feels like a lot. Let's take our 10 minute break here.
    • 1:40:31Fruit roll ups are now served. We'll be back in
    • 1:40:3310.
    • 1:54:37All right, we are back
    • 1:54:40and
    • 1:54:41recall that as we left off, we had just introduced loops and we've seen a different,
    • 1:54:47a bunch of different different ways by which we could get say a cat to meow.
    • 1:54:50Let's actually translate that to some code and start to
    • 1:54:53make sense of some of the programs with which we began
    • 1:54:56like creating our own functions as we did for the speller example
    • 1:54:59at the very beginning and actually do this a little more methodically.
    • 1:55:02So let me go over to VS code here.
    • 1:55:03Let me go ahead and create a program called Meow dot Pie instead
    • 1:55:07of meow dot C as in the past and suffice it to say
    • 1:55:11if you want to implement the idea of a cat, we can do better than just saying,
    • 1:55:15print meow, print meow, print meow.
    • 1:55:18This of course would work.
    • 1:55:20This is correct if the goal is to get the thing to meow three times.
    • 1:55:22But when I run Python of
    • 1:55:24meow dot pie,
    • 1:55:26it's going to work as expected, but this is just not good design, right?
    • 1:55:30We should minimally be using a loop.
    • 1:55:32So let me propose that we improve this per the building blocks we've
    • 1:55:35seen and I could say something like four I in range of three,
    • 1:55:39go ahead and print out now, quote unquote meow.
    • 1:55:42So this is better in the sense that it still
    • 1:55:44prints meow meow meow.
    • 1:55:46But if I want to change this to like a dog
    • 1:55:47and change the meow to a wolf or something like that,
    • 1:55:50I can change it in one place and not three different places.
    • 1:55:52So just in general, better design.
    • 1:55:55But what if now much like in scratch and in C I wanted to create
    • 1:55:58my own meow function which did not come with either of those languages as well.
    • 1:56:03Well as a teaser at the start of class,
    • 1:56:05we saw that you can define your own functions with this keyword deaf,
    • 1:56:09which is a little bit different from how C does it.
    • 1:56:12But let me go ahead and do this indeed in Python and define my own function meow.
    • 1:56:17So let me go ahead and do de
    • 1:56:19space meow open par N close per
    • 1:56:22N.
    • 1:56:22And then inside of that function I'm just gonna literally do for now,
    • 1:56:26quote unquote meow with print.
    • 1:56:29And now down here notice I can actually go ahead and just call meow
    • 1:56:34and I can go ahead and call meow and I can call meow.
    • 1:56:37And this is not the best design at the moment,
    • 1:56:40but Python does not constrain me to have to
    • 1:56:43implement a main function as we've seen thus far.
    • 1:56:45But I can define my own helper functions if
    • 1:56:49you will like a helper function called meow.
    • 1:56:51So let me go ahead and just run this for demonstration sake and run Python of meow dot P
    • 1:56:55that does seem to work. But this is not good design.
    • 1:56:58And let me go ahead and actually do this for I in range of three.
    • 1:57:03Now let me call the meow function and this too should work if I do Python of meow dot pi
    • 1:57:09there we have meow meow
    • 1:57:10meow.
    • 1:57:11But I very deliberately did something clever here.
    • 1:57:14I defined meow at the top of my file,
    • 1:57:16but that's not the best practice because as in C
    • 1:57:19when someone opens the file for the first time,
    • 1:57:21whether it's you a T fa ta a colleague,
    • 1:57:23you'd like to see the main part of the program at the top of the file
    • 1:57:27just because it's easier mentally to dive right in and know what this file is doing.
    • 1:57:31So let me go ahead and practice what I'm preaching and put the main part of my code,
    • 1:57:35even if there's no main function per se at the top of this file.
    • 1:57:39So now I have the loop at the top. I'm calling Mea
    • 1:57:42online two and I'm defining mea
    • 1:57:43on line five and six. Well, instinctively, you can perhaps see where this is going.
    • 1:57:48If I run Python of meow dot P and hit enter.
    • 1:57:50There's one of those trace backs that's tracing my error.
    • 1:57:54And here my error is apparently online too
    • 1:57:57in mea
    • 1:57:57dot
    • 1:57:58P.
    • 1:57:58And you'll notice that, huh? The name Mia
    • 1:58:01is not defined. And so previously, we saw a different type of error, a value error.
    • 1:58:06Here,
    • 1:58:06we're seeing a name error in the sense that
    • 1:58:08Python does not recognize the name of this function.
    • 1:58:11And intuitively why might that be
    • 1:58:13even if the error is a little cryptic? Yeah.
    • 1:58:17Yeah, Python two is,
    • 1:58:18is uh fancier as it seems to be than c it
    • 1:58:21still takes things pretty literally top to bottom left to right.
    • 1:58:24So if you define me
    • 1:58:26on line five, you can't use it online too. OK.
    • 1:58:28So I could undo this and I could flip the order.
    • 1:58:31But let me just stipulate that as soon as we have a bunch of functions,
    • 1:58:34it's probably naive to assume I can just keep putting my functions above, above,
    • 1:58:37above, above.
    • 1:58:38And honestly, that's gonna move all of my, you know, main code,
    • 1:58:41so to speak to the bottom of the file,
    • 1:58:42which is sort of counterproductive or less obvious.
    • 1:58:45So it turns out in Python, even though you don't need a main function,
    • 1:58:49it's actually quite common to define one nonetheless.
    • 1:58:52So what I could do to solve this problem is this,
    • 1:58:56let me go ahead and define a function called
    • 1:58:59main that takes no arguments in this case,
    • 1:59:01let me indent that same code beneath it.
    • 1:59:05And now let me keep meow defined at the bottom of my file.
    • 1:59:08So if we read this literally on line one, I'm defining a function called main
    • 1:59:11and it will do what is prescribed on lines two and three on line six,
    • 1:59:16I'm defining a function called meow
    • 1:59:17and it will do what's prescribed on line seven.
    • 1:59:20So it's fairly straightforward, even though the keyword de is of course new today.
    • 1:59:24If I run though Python of Mao dot P,
    • 1:59:26you'd like to think I'll see three meows, but
    • 1:59:29I see nothing,
    • 1:59:31I don't see an error, but I see nothing why intuitively
    • 1:59:34what explains the lack of behavior
    • 1:59:38I didn't call Maine.
    • 1:59:39So this is the thing even though it's not required in Python to have a main function,
    • 1:59:43but it is conventional in Python to have a main function,
    • 1:59:47you have to call the function yourself.
    • 1:59:50It doesn't get magically called as it does in C.
    • 1:59:52So this might seem a little stupid and that's fine.
    • 1:59:55But it is the convention in Python.
    • 1:59:57Generally,
    • 1:59:58the very last line of your file might just be literally this Call
    • 2:00:02Maine because this satisfies the constraint that Maine is defined on line one
    • 2:00:07me
    • 2:00:07to find on line six, but we don't call anything
    • 2:00:10until line 10. So line 10 says call Maine.
    • 2:00:14So that means execute this code, line three says call meow,
    • 2:00:18which means execute this code.
    • 2:00:19So now it all works because the last thing I'm doing is called main uh is called main.
    • 2:00:24You can think of C as just kind of secretly having this line there for you
    • 2:00:28the whole time.
    • 2:00:29But now that we have our own functions,
    • 2:00:31notice that we can enhance this implementation of meow
    • 2:00:34to maybe be parameterized and take actually an argument
    • 2:00:37self.
    • 2:00:37So let me make a tweak here just like in C and just like in scratch,
    • 2:00:42I can actually let meow
    • 2:00:43meow a specific number of times. So let me do this.
    • 2:00:46Wouldn't it be nice instead of having my loop
    • 2:00:49in Maine to instead just distill Maine into a
    • 2:00:52single line of code and just pass in the number of times you want the thing to meow.
    • 2:00:56What I could do in meow here is I have to give
    • 2:00:58it a parameter and I could call it anything I want.
    • 2:01:01I'm going to call it N for number which seems fine.
    • 2:01:03And then in the meow function, I could do this for uh
    • 2:01:07in range of not three,
    • 2:01:09but N now I can tell range to uh give me a range that is a variable
    • 2:01:14length based on what N is and then I indent the print below the loop now.
    • 2:01:19And this should now do what I expect to. Let me run Python of meow dot pi enter
    • 2:01:25and there's three.
    • 2:01:26But if I change the three to a five and rerun this Python of meow dot pi,
    • 2:01:30now I'm getting five meows.
    • 2:01:32So we've just seen a third way how in Python now, we can implement the I
    • 2:01:36idea of meowing as its own abstracted function.
    • 2:01:40And I can assume now that now exists, I can now sort of treat it as out of sight,
    • 2:01:44out of mind.
    • 2:01:45It's an abstraction and frankly, I could even put it into a library,
    • 2:01:48import it from a file like we've done with CS 50
    • 2:01:52sort of make it usable by other people as well.
    • 2:01:55So the takeaway here really though is that in Python, you can similarly to see,
    • 2:01:59define your own functions,
    • 2:02:01but you should understand the slight
    • 2:02:02differences as to what gets called automatically
    • 2:02:05for you.
    • 2:02:06All right,
    • 2:02:07other differences or similarities with C we recall
    • 2:02:10that in C truncation was an issue.
    • 2:02:13Truncation is whereby if you, for instance, divide an in by an inch
    • 2:02:18and it's a fractional answer everything after the decimal point
    • 2:02:22gets truncated by default because an in divided by an N
    • 2:02:25in C gives you an inch and if you can't fit the remainder in that integer,
    • 2:02:30everything the decimal gets cut off.
    • 2:02:32So what does this mean? Well, let me actually go back to VS code here.
    • 2:02:35Let me go ahead and open say calculator dot pi again.
    • 2:02:40And let's change up what the calculator now does. Let me do this.
    • 2:02:43Let me define a variable called X set it equal
    • 2:02:45to the input function prompting the user for X.
    • 2:02:49Let me ask the user for why? Let me not repeat past mistakes.
    • 2:02:53And let me proactively convert both of these to ins and I'll do it in one pretty
    • 2:02:58one liner here so that I definitely get X and Y and on the honor system,
    • 2:03:02I just won't type cat.
    • 2:03:03I won't type dog even though this program
    • 2:03:05is not really complete without error checking.
    • 2:03:07Now let me go ahead and declare a third variable Z equals X divided by Y.
    • 2:03:11And now let's just go ahead and print out Z I don't need a format code.
    • 2:03:15I don't need an F string. If all you want to do is print a variable.
    • 2:03:18Print is very flexible. You can just say print
    • 2:03:21Z in parentheses. Let me run Python of calculator dot pi hit enter,
    • 2:03:26let's type in 14, X 34 Y I left out a space there
    • 2:03:31and oh,
    • 2:03:32interesting.
    • 2:03:34What seems to have happened here. Let me fix my spacing and rerun this again.
    • 2:03:37Python of calculator. Dot pi. So 13, what did not happen?
    • 2:03:44Yeah. So it didn't truncate.
    • 2:03:46So Pythons a little smarter when it comes to converting one value to another.
    • 2:03:50So an integer divided by an integer,
    • 2:03:52if it ends up giving you this fractional component, not to worry,
    • 2:03:55now you'll get back.
    • 2:03:56What is effectively a float in Python here?
    • 2:03:59Well, what else do we want to be mindful of in
    • 2:04:03say Python?
    • 2:04:04Well,
    • 2:04:05recall that in C we had this issue of floating point and
    • 2:04:08precision whereby if you want to represent a number like one third
    • 2:04:11and on a piece of paper,
    • 2:04:12it's like 0.3 with a line over it because the three infinitely repeats.
    • 2:04:17But we saw a problem in, in C last time when we actually played around with some value.
    • 2:04:22So for instance,
    • 2:04:23let me go back to VS code here and this is going to be the ugliest syntax.
    • 2:04:26I do think we see today,
    • 2:04:28but there was a way in C
    • 2:04:29using percent F to show more than the default number
    • 2:04:33of digits after the decimal point to see more significant digits
    • 2:04:36in Python, there's something similar. It just
    • 2:04:38very weird. And the way you do it in Python is this,
    • 2:04:41you specify that you want an F string, a format string
    • 2:04:44and I'm just going to start and finish my thought first f before quote unquote.
    • 2:04:49If you want to print out Z, you could literally just do this.
    • 2:04:52And so this is just an F string,
    • 2:04:54but you're interpolating Z.
    • 2:04:56So it doesn't do anything more than it did a
    • 2:04:58moment ago when I literally just passed in Z.
    • 2:05:00But as soon as you have an F string,
    • 2:05:02you can configure the variable to print out specific number of digits.
    • 2:05:07So if you actually want to print out Z
    • 2:05:09to say 50 decimal points, just to see a lot, you can use crazy syntax like this.
    • 2:05:15So it's just using the curly braces as I introduced before.
    • 2:05:19But you then use a dot after a colon and
    • 2:05:21then you specify the number of digits that you want.
    • 2:05:23And then f to make clear, it's a float honestly.
    • 2:05:26I Google this all the time when I don't remember
    • 2:05:27the syntax but the point is the functionality exists.
    • 2:05:31All right. Let me go down here
    • 2:05:33and rerun Python of calculator dot pi.
    • 2:05:36And unfortunately, if I divide one by three, not all of my problems are solved.
    • 2:05:40Floating point precision
    • 2:05:42is still a thing.
    • 2:05:43So be mindful of the fact that there are these limitations in the world of Python.
    • 2:05:48Um floating point precision remains if you want to do
    • 2:05:50even better than that though there exists a lot,
    • 2:05:53lot more libraries,
    • 2:05:54third party libraries that can give you
    • 2:05:56much greater precision for scientific purposes,
    • 2:05:59uh financial purposes or the like.
    • 2:06:01But what about another problem from C integer overflow?
    • 2:06:04Like if you just count too high recall that you might accidentally overflow,
    • 2:06:08the capacity of an integer and end up going back to zero or worse,
    • 2:06:11going negative altogether.
    • 2:06:13In Python, this problem does not exist in Python when you have an integer A K A in,
    • 2:06:19even though we haven't needed to use the keyword in
    • 2:06:22it will grow and grow and grow.
    • 2:06:25And Python will reserve more and more memory for that integer to fit it.
    • 2:06:29So it is not a fixed number of bits.
    • 2:06:31So floating point precision still a problem
    • 2:06:34in your overflow, not a problem in the latest versions of Python.
    • 2:06:38So a difference worth knowing.
    • 2:06:41But what about other features of Python that we didn't have in C Well,
    • 2:06:44let's actually revisit one of those trace backs,
    • 2:06:46one of those errors I ran into earlier to see how we might actually solve it.
    • 2:06:50So let me go back to VS code here
    • 2:06:53and just for fun, let me go ahead and do this.
    • 2:06:55Let me clear my terminal and let me change
    • 2:06:57my calculator to actually have a get in function.
    • 2:07:00We've seen how to define our own functions. Let me not bother with the CS 50 library.
    • 2:07:03Let me just invent my own get in function as follows. So def get
    • 2:07:07in
    • 2:07:08and just like the CS 50 function, I'm gonna have get in,
    • 2:07:11take a prompt a string to show the user to ask them for an integer.
    • 2:07:14And now I'm gonna go ahead and return the return value of input,
    • 2:07:19passing that same prompt to input because input just like get string,
    • 2:07:22shows the user a string of text.
    • 2:07:25But I do want to convert this thing here to an end.
    • 2:07:28So this is just a one liner really of an implementation of get in.
    • 2:07:32So this is kind of like what the
    • 2:07:3350 did in its Python library
    • 2:07:35but not quite why because there's a problem with it. So let me do this.
    • 2:07:39Let me define a main function just by convention.
    • 2:07:41Let me use this implementation of, get in to ask the user for X.
    • 2:07:45Let me use this, get in function to prompt the user for Y.
    • 2:07:48And then let me do something simple, like print out X plus Y.
    • 2:07:53And then very last thing I have to call main
    • 2:07:56and this is a minor point.
    • 2:07:57But I'm deliberately putting multiple blank lines between my functions.
    • 2:08:01This too is Python. It's a matter of style, style 50 will help you with this.
    • 2:08:05It's just meant for larger files to really make your functions
    • 2:08:08stand out and be a little more separated visually from others.
    • 2:08:12All right, let me go ahead and run Python of calculator dot pi enter,
    • 2:08:16let me type in one. Let me type in three and that actually works.
    • 2:08:20One plus three is four. Let me do the more obvious one plus two, gives me three.
    • 2:08:24So the calculator is in fact working
    • 2:08:26until such time as I the human don't co-operate and type in something like cat for X,
    • 2:08:32then we get that same trace back as before.
    • 2:08:35But I'm seeing it now
    • 2:08:36in this file and let me zoom in on my terminal just to make clear,
    • 2:08:39we don't need to see the, the remain the uh old history there.
    • 2:08:42Let me,
    • 2:08:43let me type in cat enter and you'll see the same trace back and you'll see that. OK?
    • 2:08:49Here's where now there's multiple functions involved. So what's going on
    • 2:08:53the first problems at line 12 in Maine?
    • 2:08:56But that's not actually the problem because main calls might get in function.
    • 2:09:00So on line six of calculator dot pi,
    • 2:09:03this is really the issue.
    • 2:09:05So again,
    • 2:09:05it's tracing everything that just happened from top to bottom here and value error,
    • 2:09:10invalid literal four inch with base 10 cat, which is to say like before
    • 2:09:15it just cat is not an integer in base 10 or any other base,
    • 2:09:19it just cannot be converted to an integer.
    • 2:09:21So how do you fix this or really how does the CS 50 library fix this?
    • 2:09:25You won't have to write much code like this. But it turns out that Python supports
    • 2:09:30what are called exceptions.
    • 2:09:32And generally an exception is a better way of handling certain types of errors.
    • 2:09:36Because in C recall that the only way we could really
    • 2:09:39handle errors is by having functions return special values like MLO
    • 2:09:44could return null, which means it ran out of memory, something went wrong.
    • 2:09:49Some functions we wrote in C could return one, could return two,
    • 2:09:53could return negative one recall that we could write our
    • 2:09:56own functions that return values to indicate something went wrong.
    • 2:09:59But the problem in C is that if you're stealing certain values,
    • 2:10:03be it null or one or two or three,
    • 2:10:06your function can never return null or one or two or three actual values.
    • 2:10:12Why? Because other people are going to interpret them as errors.
    • 2:10:15So you kind of have to use up some of your possible return values in a language like C
    • 2:10:20and treat them specially as errors
    • 2:10:23in Python and other languages, Java and others, you don't have to do that.
    • 2:10:26You can instead have more out of band error handling known as exceptions.
    • 2:10:31And that's what's happening here when I run calculator dot pi and I
    • 2:10:35in cat, what I'm seeing here is actually an exception.
    • 2:10:39It's something exceptional but not in a good way.
    • 2:10:42Like this exception means this was not supposed to happen.
    • 2:10:45The type of exception happens to be called a value error.
    • 2:10:48And within the world of Python,
    • 2:10:49there's this whole taxonomy that is to say a whole list of possible exceptions.
    • 2:10:53Value error is one of the most common.
    • 2:10:55We saw another one before name error when I
    • 2:10:57said meow when Python didn't know what meow meant.
    • 2:11:00So this is just an example of an exception.
    • 2:11:02But what this means is that there is a way for me to try to handle this myself.
    • 2:11:06So I'm actually gonna go ahead and do this
    • 2:11:09instead of get in simply blindly returning
    • 2:11:12the integer conversion of whatever input the user gives me.
    • 2:11:16I'm gonna instead literally try to do this instead.
    • 2:11:20So it's kind of a aptly named phrase.
    • 2:11:22It literally means that please try to do this except if something goes wrong,
    • 2:11:27except if there is
    • 2:11:29a value error.
    • 2:11:31In which case, I want Python to do something else, for instance, quote unquote,
    • 2:11:35not an integer.
    • 2:11:38So what does this mean?
    • 2:11:39It's a little weird the syntax but in the debt get in function,
    • 2:11:42Python will first try to do the following.
    • 2:11:45It will try to get an input from the user,
    • 2:11:47it will try to convert it to an integer and it will try to return it.
    • 2:11:51But if one of those operations fails, namely the integer step in this case,
    • 2:11:55then an exception could happen and you might get what's called a value error.
    • 2:11:59Why?
    • 2:11:59Because the documentation tells you that might happen or in my case,
    • 2:12:02I experienced it first hand.
    • 2:12:03And now I want to catch this kind of exception in my own code.
    • 2:12:07So if there is a value error, I'm not going to see that crazy trace back anymore.
    • 2:12:11I'm instead going to see quote unquote, not an integer,
    • 2:12:15but with the CS 50 library, does
    • 2:12:17you technically is it lets you try again and again and again,
    • 2:12:21recall in the past if I dip in cat and dog and bird,
    • 2:12:24it's just gonna keep asking me again and again until I actually give it an inch.
    • 2:12:27So that kind of implies that we really need a loop inside of this function.
    • 2:12:31And the easiest way to do something forever is to loop while true,
    • 2:12:35just like in C but a capital T in Python.
    • 2:12:38And what I'm going to do now is implement a better
    • 2:12:41version of get in here because what's it going to do?
    • 2:12:44It
    • 2:12:44is going to try, it's going to do this forever.
    • 2:12:47It's going to try to get an input, convert it to an in and return it
    • 2:12:51and just like break,
    • 2:12:53breaks you out of a loop return also breaks you out of the loop as well, right?
    • 2:12:58Because once you've returned, there's no more need for this function to execute.
    • 2:13:01So long story short, you won't have to write much code like this yourself.
    • 2:13:05But this is essentially what the CS 50 library is
    • 2:13:09doing when it implements the Python version of Get in.
    • 2:13:12So what happens now if I run Python of calculator dot pi and I type in
    • 2:13:17C
    • 2:13:18I get yelled at but I'm prompted again because of the loop, I type in dog,
    • 2:13:21I'm yelled at but I'm prompted again.
    • 2:13:23I type in burr yelled at but I'm prompted again. If I type in one, then I type in two.
    • 2:13:28Now it proceeds because it tried and succeeded this
    • 2:13:31time as opposed to trying and failing last time.
    • 2:13:34And technically,
    • 2:13:35the CS 50 library doesn't actually yell at you with like not an integer.
    • 2:13:39So technically, if you want to handle the error, that is to say, catch the exception,
    • 2:13:44you can actually just say oh pass
    • 2:13:46and it will just silently try again
    • 2:13:49and again.
    • 2:13:49So let me go ahead and run this Python of calculator dot pi works almost the same.
    • 2:13:54But notice now it works just like the C version.
    • 2:13:57It doesn't yell at you, but it does prompt you again
    • 2:13:59and again and again, but I'll do one and two and that now is satisfied.
    • 2:14:03So that then is exceptions which you'll encounter,
    • 2:14:06but you yourself won't have to write much code along those lines.
    • 2:14:09Well, what else can we now do?
    • 2:14:11Well, let's revisit something like this for Mario recall
    • 2:14:14whereby we had this two
    • 2:14:16world with things in the way for Mario like this uh column of three bricks.
    • 2:14:20Let me actually play around now for a moment
    • 2:14:22with some loops just to see how there's different
    • 2:14:24ways that might actually resonate with you just in
    • 2:14:26terms of the simplicity of some of these things.
    • 2:14:29Let me go ahead and create a program called Mario dot pi.
    • 2:14:32And suppose that I want to print a column of three
    • 2:14:35bricks kind of doesn't get any easier than this in Python.
    • 2:14:38So for I in range three, just go ahead and print out a single hash
    • 2:14:43done
    • 2:14:44like that. Then is what we took, uh took us more lines of code in the past.
    • 2:14:48But if I run Mario dot
    • 2:14:49P
    • 2:14:49that there gets the job done, I could change the I to an underscore,
    • 2:14:53but it's not bad to remind myself that I is what's really doing my counting.
    • 2:14:57Well, what else could we do beyond this? Well, recall that in the world of Mario,
    • 2:15:03um we prompted the user actually for a specific height,
    • 2:15:06we didn't just always hard code three.
    • 2:15:08So I could actually do something like this. Let me actually open up
    • 2:15:12from today's code that I came with in advance and pull up this C version of Mario.
    • 2:15:19So this was from some time ago in week one
    • 2:15:22and this is how we implemented
    • 2:15:24a loop that ensures that we get a positive integer from
    • 2:15:28the user by just doing while and is not positive.
    • 2:15:32And then we use this for
    • 2:15:33loop to actually print out that many hashes.
    • 2:15:35Now in Python,
    • 2:15:36it's actually going to be pretty similar except for the fact that in Python,
    • 2:15:39there is no do while loop.
    • 2:15:41But recall that a dow
    • 2:15:42loop was useful because it, you can get the user to try something
    • 2:15:45and then maybe try again, maybe try again, maybe try again. So it's really good
    • 2:15:49for user input. So let's actually do this. Let me borrow the CS F
    • 2:15:53library get
    • 2:15:54in function just so we don't have to re implement that ourselves again. And again,
    • 2:15:58let me in Python do this, the Python way in Python.
    • 2:16:02If you want to prompt the user to do something again and again and again,
    • 2:16:05potentially you deliberately by convention, induce an infinite loop,
    • 2:16:09you just get yourself into an infinite loop.
    • 2:16:12But the goal is going to be try something, try something, try something.
    • 2:16:15And as soon as you have what you want break out of the loop instead.
    • 2:16:18So we're like implementing the idea of a do while loop ourselves.
    • 2:16:22So I'm going to do this N for number equals get in.
    • 2:16:25And let's ask the user for a height,
    • 2:16:28then let's just check if N is greater than zero,
    • 2:16:31you know what break we've got the value we need.
    • 2:16:34And if not, it's just going to implicitly keep looping again and again and again.
    • 2:16:38So in Python,
    • 2:16:39this is to say super common to deliberately induce an infinite loop
    • 2:16:43and break out of it when you have what you want.
    • 2:16:46All right.
    • 2:16:46Now, I can just do the same kind of code as before for I in range, not of uh rage,
    • 2:16:51sometimes I in range, not three, but N now I can go ahead and print out
    • 2:16:57uh oops a hash like this.
    • 2:17:00If I open my terminal window, it's going to work almost the same.
    • 2:17:02But now Mario is going to prompt me for the height.
    • 2:17:05So I could type in three or I could type in four or I
    • 2:17:09could be uncooperative and type in zero or negative one or even cat.
    • 2:17:13And because I'm using the CS 50 library cat is ignored because I'm using
    • 2:17:18my wild loop and breaking out of it only when N is positive, I'm also ignoring the zero
    • 2:17:24and the negative one.
    • 2:17:25So again, this would be a Python way of implementing this particular idea.
    • 2:17:30Uh If I want to maybe enhance this a bit further,
    • 2:17:35let me propose that
    • 2:17:37for in
    • 2:17:38uh
    • 2:17:39we consider something like the two dimensional version of the
    • 2:17:43or actually the uh the horizontal version of this instead.
    • 2:17:46So recall that some time ago,
    • 2:17:47we printed out like four question marks in the sky
    • 2:17:50that might have looked a little something like this.
    • 2:17:52Now, the very mechanical way to do this would be as follows. Let me close my C code.
    • 2:17:59Let me clear my terminal and let me just delete my old Mario
    • 2:18:02version here and let's just do this four I in range of four.
    • 2:18:07Let's go ahead and print out
    • 2:18:09a question mark. All right, I'm gonna run Python of Mario dot pi enter and uh
    • 2:18:15it's still a column
    • 2:18:16instead of a row. But what's the fix here? Perhaps?
    • 2:18:22What's the fix? Yeah.
    • 2:18:25Yeah,
    • 2:18:25we can use that named parameter and say N equals
    • 2:18:28quote unquote to just press the default backslash N.
    • 2:18:31But let's give ourselves one at the very end
    • 2:18:34of the loop just to move the cursor correctly.
    • 2:18:36So now if I run Python of Mire A
    • 2:18:37pi. Now it looks like what it might have in the sky here.
    • 2:18:41But it turns out Python has some neat features too, more syntactic sugar.
    • 2:18:45If you will, for doing
    • 2:18:46a
    • 2:18:46little more easily, it turns out in Python, you could also do this.
    • 2:18:49You could just say print,
    • 2:18:50quote unquote question mark times four and just like plus means concatenation
    • 2:18:57star here means really multiply the string by itself that many times.
    • 2:19:01So sort of automatically concatenate it with itself.
    • 2:19:04So if I run Python of Mario dot pi this two,
    • 2:19:07so again, just some features of Python that make it a little more pleasant to use.
    • 2:19:10So you don't always have to like slog through
    • 2:19:12implementing a loop or something along those lines.
    • 2:19:15Well,
    • 2:19:15what about something more two dimensional like in the world of uh this brick here?
    • 2:19:20Well, in the context of this sort of grid of bricks,
    • 2:19:23we might do something like this in VS code.
    • 2:19:26Let me go back to Mario up high
    • 2:19:28and let me do a three by three grid for that uh block like we did in week one.
    • 2:19:32So for I in range of three,
    • 2:19:35I can nest loops just like in C four J in range of three, I can then print out a hash here
    • 2:19:43and then let's leave this alone even though it's not quite right yet.
    • 2:19:46Let's do Python of Mario dot pi.
    • 2:19:48OK.
    • 2:19:48It's like nine bricks all in a column which so
    • 2:19:51your mind might wander to the end parameter again.
    • 2:19:54So yeah, let's fix this N equals quote unquote.
    • 2:19:56But at the end of that loop, let's just print out a new line.
    • 2:19:59So this logically is the same
    • 2:20:01as it was in C but in this case,
    • 2:20:03I'm now doing it in Python just a little
    • 2:20:05more easily without I plus plus without a conditional.
    • 2:20:08I'm just relying on this for I
    • 2:20:11in syntax using range, I can tighten this up further.
    • 2:20:15Frankly, if I already have the outer loop, I could do something like this.
    • 2:20:18I could print out a single hash times three.
    • 2:20:22And now if I run Python of my dot pi like that works too.
    • 2:20:25So I can combine these ideas in interesting ways as well.
    • 2:20:28The goal is simply to seed you with some of these
    • 2:20:31building blocks. All right. How about code?
    • 2:20:33That was maybe a little more logical in nature.
    • 2:20:36Well, in Python, we indeed have some other features as well.
    • 2:20:39So, no, not a break that was before.
    • 2:20:41In Python, we have some other features as well,
    • 2:20:43namely like lists and lists are denoted by those
    • 2:20:45square brackets reminiscent of the world of arrays.
    • 2:20:48But in Python,
    • 2:20:49what's really nice about lists is that their memory is all dramatically
    • 2:20:53handled for you in array is about having values contiguously in memory.
    • 2:20:57In Python, a list is more like a linked list like you can,
    • 2:21:01it will allocate memory for you and grow and shrink these
    • 2:21:03things and you do not have to know about pointers.
    • 2:21:06You do not have to know about nodes.
    • 2:21:08You do not have to implement linked lists yourself.
    • 2:21:10You just get list as a data type in Python itself here.
    • 2:21:14For instance, is some of the documentation for lists specifically.
    • 2:21:17And in particular lists also like strings or stirs have methods,
    • 2:21:21functions that come with them that just make it easy to do certain things.
    • 2:21:26So for instance, if I wanted to
    • 2:21:29maybe do something like taking averages of scores like we did some time ago,
    • 2:21:33we can do that using a combination of lists and the function called L
    • 2:21:37which I alluded to earlier, which will tell you the length of a list.
    • 2:21:41Now, how might we do this?
    • 2:21:42Well, if we read the documentation for lang,
    • 2:21:44it turns out there's other functions there too that might be helpful.
    • 2:21:46So let me go back to VS code here.
    • 2:21:48Let me close Mario dot pi and let me open a file
    • 2:21:51called scores dot pi reminiscent of something we did weeks ago too.
    • 2:21:55Let me go ahead and just for demonstration sake.
    • 2:21:57Give myself a variable called scores that has my
    • 2:22:00three test scores or whatnot from weeks ago.
    • 2:22:03So I'm using square brackets, not curly braces as in see,
    • 2:22:06this is a linked list or a list in Python.
    • 2:22:09And let me get like the average of these values. Well, I could do this average equals.
    • 2:22:14And it turns out in Python, you just get a lot of functionality for free.
    • 2:22:18And those functions sometimes take not single
    • 2:22:20arguments but lists as their argument.
    • 2:22:23So for instance, I can use Python's built in some function
    • 2:22:26and pass in those scores.
    • 2:22:27I can then divide that sum by the length of the scores list as well.
    • 2:22:32So length of a list just tells you how many things are in it.
    • 2:22:36So this is like doing magically 72 plus 73 plus 33 all divided by
    • 2:22:41three in total. If I want to. Now do the math out,
    • 2:22:45I can print the result so I can print out using an F string
    • 2:22:49and maybe like some prefixed text here. Let's print out that
    • 2:22:53here.
    • 2:22:54So let me do Python of scores dot pi enter
    • 2:22:57and there's the average slightly imprecisely.
    • 2:23:00But at that point, I'm not doing so well anyway, so that's fine. So
    • 2:23:03at this point, we've seen that we have a sort of more functionality than C like in C.
    • 2:23:09How would we have computed the average weeks ago?
    • 2:23:12I mean, we literally created a variable, we then had a loop, we iterate
    • 2:23:15over the array, we added things together. There was just so much more work.
    • 2:23:18It's nice when you have a language that
    • 2:23:20comes with functions among them lang among them,
    • 2:23:23some that just does more of this for you.
    • 2:23:26But suppose you actually want to get the scores from the user,
    • 2:23:29we c we used an array and in C we used, get in,
    • 2:23:32we could do something a little similar here.
    • 2:23:34Let me propose that instead of hard coding those three values,
    • 2:23:38let me do this from CS 50 import, get
    • 2:23:40in.
    • 2:23:41Now let me give myself an empty list by just saying scores equals open bracket,
    • 2:23:46close bracket.
    • 2:23:47And unlike C where you just can't do this, you can't say, give me A N
    • 2:23:51and I'll figure out the length later unless you resort
    • 2:23:54to pointers and memory management or the like in Python,
    • 2:23:57you can absolutely give yourself an initially empty list.
    • 2:24:01Now, let's do this for I in range of three,
    • 2:24:04let's prompt the human four threes test scores.
    • 2:24:07So the first score will be the return value
    • 2:24:09of get in prompting the user for their score.
    • 2:24:12And now if I want to add this score to that otherwise empty list,
    • 2:24:16here's where methods come into play functions that come with objects like lists,
    • 2:24:21I can do scores plural because that's the name of my variable from line three
    • 2:24:26dot append
    • 2:24:27and I can append that score.
    • 2:24:29So if we read the documentation for lists in Python,
    • 2:24:31you'll see that lists come with a function,
    • 2:24:34a method called append which literally just T A
    • 2:24:36value onto the end, tax a value onto the end.
    • 2:24:39Like all of that annoying code we would have written in C to like
    • 2:24:43iterate with pointer and pointer and pointer to the end of the list,
    • 2:24:46append it.
    • 2:24:46Mal
    • 2:24:47a new node,
    • 2:24:47Python does all of that for us.
    • 2:24:50And so once you've done that,
    • 2:24:51now I can do something similar to before the average equals the,
    • 2:24:54some of those scores divided by the length of that list of scores.
    • 2:24:58And I can again print out with an F string,
    • 2:25:01the average
    • 2:25:02value in that variable like this.
    • 2:25:05So again, you just have more building blocks at your disposal when it comes to
    • 2:25:09something like this.
    • 2:25:11Um You can also do this just so you've seen other syntax,
    • 2:25:14it turns out that instead of doing scores dot append,
    • 2:25:18you could also do this, you could
    • 2:25:21kate scores with itself by adding two lists together like this.
    • 2:25:28This looks a little weird
    • 2:25:29but on the left is my variable scores. On the right here, I'm a pa
    • 2:25:34I am taking whatever is in that list and I'm adding the current score by
    • 2:25:39adding it to its own list and this will update the value as we go.
    • 2:25:43Um But it does in fact change the value of score as opposed to a pending
    • 2:25:47to the initial list.
    • 2:25:49All right. How about some other building blocks here? Let me propose this.
    • 2:25:54Let me uh let me close out scores dot Pie.
    • 2:25:58Let me open up a file called phone book dot pie to re uh reminiscent of
    • 2:26:02what we did weeks ago in C and let me give myself a list of names.
    • 2:26:05We won't bother with numbers just yet.
    • 2:26:06Let's just play with lists for another moment. So here's a variable called names.
    • 2:26:10It has maybe three names in it, maybe Carter and David
    • 2:26:14and John Harvard as in past weeks.
    • 2:26:17And now let me go ahead and ask the user
    • 2:26:19to input a name because this is gonna be like a phone book.
    • 2:26:22I want to ask the user for a name and then look up that person's name in the phone book.
    • 2:26:26Even though I'm not bothering by having any uh phone numbers just yet.
    • 2:26:30How could I search for a la linear search someone's name?
    • 2:26:33Well, in Python, I could do this for uh name uh rather for n in names.
    • 2:26:40If the current name equals what the human typed in, then go ahead and print out found,
    • 2:26:47then break out of this loop.
    • 2:26:49Uh
    • 2:26:50Otherwise we'll print out,
    • 2:26:51not found at the bottom.
    • 2:26:54All right. So let's try this Python of phone book dot P.
    • 2:26:57Let's search for maybe Carter. That's easy. He's at beginning. Oh.
    • 2:27:01Well, he was found but then I printed not found. So that's not quite what I want.
    • 2:27:04How about David? Dav ID
    • 2:27:07found? Not found? All right. Not very correct. How about this?
    • 2:27:11Let's search for eli not in the list. OK.
    • 2:27:14So at least someone not being in the list is
    • 2:27:16working but logically for Carter for David and even John,
    • 2:27:19why are we seeing found and then not found.
    • 2:27:28No. Why is it not found? Yeah.
    • 2:27:33Ok. I, I don't have, seem to have indented the print, but let me try this.
    • 2:27:37If I just go with the elts here, let me go up here and indent this and say,
    • 2:27:42I'm not sure, logically, this is what we want
    • 2:27:45because what I think this is gonna do if I search for maybe Carter. Ok. That worked.
    • 2:27:50So it's partially fixed the problem.
    • 2:27:52But let me try searching for maybe David.
    • 2:27:55Uh Now we're sort of the opposite problem not found, found. Why?
    • 2:27:58Well,
    • 2:27:59I don't think we want to immediately conclude that someone's not found
    • 2:28:02just because they don't equal the current name in the list.
    • 2:28:07So it turns out we could fix this in a couple of different ways.
    • 2:28:11But there's kind of a neat features of Python
    • 2:28:14in Python. Even four loops can have an ELTS clause and this is weird,
    • 2:28:19but the way this works is as follows in Python,
    • 2:28:22if you break out of a loop, that's it for the for loop.
    • 2:28:26If though you get all the way through the list that
    • 2:28:29you're looping over and you never once call line eight,
    • 2:28:32you never break out of the loop.
    • 2:28:34Python is smart enough to realize, OK,
    • 2:28:36you just went through lines five through eight.
    • 2:28:38You never actually logically called break.
    • 2:28:40Here's an Ielts clause to be associated with it. Semantically, this is weird.
    • 2:28:44We've only ever seen if and elts associated with each other
    • 2:28:47but four loops in Python actually can have ielts as well.
    • 2:28:50And in this case, now, if I do Python, a phone book dot P type
    • 2:28:54Carter, now we get only one answer.
    • 2:28:56If I do it again and type in David, now we get only one answer. Do it again with John.
    • 2:29:00Now we get only one answer, do it with Eli. Now we get
    • 2:29:03only one answer.
    • 2:29:04So again,
    • 2:29:04you just get a few more tools in your tool kit when it comes to a language like Python,
    • 2:29:08that might very well make solving problems a little more pleasant.
    • 2:29:12But this is kind of stupid in Python.
    • 2:29:14This is correct,
    • 2:29:15but it's not well designed because I don't need to iterate over lists like this.
    • 2:29:19So pedantically like we've been doing for weeks,
    • 2:29:21see I can actually tighten this up and I can just do this, I can get rid of the loop.
    • 2:29:26And I can say if name in names, then print out quote
    • 2:29:30unquote found like that's it in Python.
    • 2:29:32If you want Python to search a whole list of values for you just let Python do the work.
    • 2:29:37And you can literally just say if the name
    • 2:29:40that the human inputted is in names, which is this list here.
    • 2:29:44Python will use linear search for you search automatically from left to right,
    • 2:29:48presumably
    • 2:29:49looking for the value.
    • 2:29:50And if it doesn't find it then and only then will this elts clause execute instead.
    • 2:29:56So again,
    • 2:29:56Python is just starting to save us some time because this
    • 2:30:00too will find Carter but it will not find for instance,
    • 2:30:03Eli.
    • 2:30:04All right. So we get that functionality for free.
    • 2:30:07But what more can we perhaps do here? Well, it turns out
    • 2:30:12that Python has yet other features we might want to explore, namely dictionary,
    • 2:30:16shorten dead
    • 2:30:17and a dictionary in Python is just like it was
    • 2:30:20in c and really in computer science in general,
    • 2:30:22dictionary was an abstract data type.
    • 2:30:24And it's a collection of key value pairs. It looks a little something like this.
    • 2:30:28If in C if in Python, if in any language,
    • 2:30:30you want to associate something with something like a name with a number,
    • 2:30:34you had to in problem set five,
    • 2:30:36implement the darn thing yourself by implementing an entire spell checker with an
    • 2:30:41array and linked list to sort of store all of those words in your
    • 2:30:44dictionary.
    • 2:30:45In Python.
    • 2:30:46As we saw earlier,
    • 2:30:47you can use a set or you can use more simply a
    • 2:30:49dictionary that implements for you all of problem sets I fives ideas.
    • 2:30:55But Python does the heavy lifting for you.
    • 2:30:58A
    • 2:30:58dict in Python is essentially a hash table, a collection of key value pairs.
    • 2:31:03So what does this mean for me in Python?
    • 2:31:05It means that I can do some pretty handy things pretty easily.
    • 2:31:09So for instance, let me go back here to VS code
    • 2:31:12and let me change my phone book altogether
    • 2:31:15to be this. Let me give myself a list
    • 2:31:19of dictionaries.
    • 2:31:21So people is now gonna be a global list and I'm gonna
    • 2:31:25demarcate it here with open square bracket and closed square bracket.
    • 2:31:28And just to be nice in the entirety,
    • 2:31:30I'm going to have these people no longer just be Carter
    • 2:31:35and David and John as in the previous example.
    • 2:31:38But I want each of the elements of this list to
    • 2:31:41be a key value pair like a name and a number.
    • 2:31:46So how can I do this in Python?
    • 2:31:48You can use this syntax and this is I think the last of the weird looking syntax today,
    • 2:31:54you can define a dictionary
    • 2:31:57that is something like this by using two curly braces like this.
    • 2:32:01And inside of your curly braces, you get to invent the name, the keys and the values.
    • 2:32:06So if you want one key to be the person's name,
    • 2:32:09you can do quote unquote name and then quote unquote Carter.
    • 2:32:12If you want another key to be number,
    • 2:32:15you can do quote unquote number and then quote unquote something like last time.
    • 2:32:18161749 5 1000, for instance, for Carter's number there.
    • 2:32:23And collectively everything here online too represents a dictionary.
    • 2:32:27It's as though on a chalkboard,
    • 2:32:29I wrote down name Carter number +1617495 1000 row by row by row in this table.
    • 2:32:37This is simply the code equivalent thereof.
    • 2:32:39If you want to be really nitpicky or tidy, you could style
    • 2:32:43code to look like this,
    • 2:32:44which makes it a little more clear perhaps as to what's going on,
    • 2:32:48it's just starting to add a lot of white space to the screen,
    • 2:32:50but it's just the collection of key value pairs again,
    • 2:32:53akin to a two column table like this.
    • 2:32:56I'm gonna undo the white space just to kind of tighten things
    • 2:32:59up because I want to cram two other people in here.
    • 2:33:01So I'm gonna go ahead and do another um
    • 2:33:04set of curly braces with quote unquote name
    • 2:33:06and David
    • 2:33:07quote unquote number and we'll have the same number. So +1617495 1000.
    • 2:33:13And then lastly, let's do another set of curly braces for name of say John
    • 2:33:18and John Harvard's number, quote unquote number will be uh plus one
    • 2:33:24plus one.
    • 2:33:25Let's see, 9494682750 is always John Harvard's number.
    • 2:33:31And then by convention, you typically end even this element with a comma,
    • 2:33:35but it's not strictly necessary syntactically,
    • 2:33:37but stylistically that's often added for you.
    • 2:33:40So what is people? People is now a
    • 2:33:42list of dictionaries, a list of dictionaries. So what does that mean?
    • 2:33:47It means I can now do code like this.
    • 2:33:49I can prompt the user with the input function for someone's name.
    • 2:33:53If the goal now is to look up that person's number, how can I look up their number.
    • 2:33:57Let me fix this typo.
    • 2:33:59How can I look up that number?
    • 2:34:00Well, for each person in the list of people, let's go ahead and do this.
    • 2:34:06If the current person's name
    • 2:34:09equals equals whatever name the human typed in,
    • 2:34:13then get that person's number by going into that person
    • 2:34:17and doing quote unquote number and then go ahead
    • 2:34:21and print out something like this F string,
    • 2:34:23found
    • 2:34:25that person's number.
    • 2:34:26And then since we found them, let's just break out all together.
    • 2:34:29And if we get through that whole thing, let's just at the very end,
    • 2:34:32print out not found.
    • 2:34:34So what's weird here? If I focus on this code here, this syntax obviously is new.
    • 2:34:39The square brackets though just means, hey, Python, here comes the list.
    • 2:34:43Hey Python, that's it. For the list inside of this list are three dictionaries.
    • 2:34:47The curly braces mean, hey Python here comes the dictionary.
    • 2:34:50Hey Python, that's it for the dictionary.
    • 2:34:52Each of these dictionaries has two key value pairs,
    • 2:34:55name and its value number and its value.
    • 2:34:58So you can kind of think of each of these lines
    • 2:35:01as being like ac struct like with type de instruct,
    • 2:35:04but I don't have to decide in advance what the keys and the values are gonna be.
    • 2:35:08I can just on the fly, create a dictionary like this again,
    • 2:35:11reminiscent of this kind of chalkboard design.
    • 2:35:13All right. So what am I actually doing in code?
    • 2:35:16A dictionary in Python, lets you index into it similar to an array with numbers in C.
    • 2:35:25So in C,
    • 2:35:26this is a little bit different in C you might
    • 2:35:28have been in the habit of doing person dot name.
    • 2:35:30But because it's a dictionary,
    • 2:35:32the syntax in Python is you actually use square brackets with strings
    • 2:35:36as being inside the square brackets rather than numbers.
    • 2:35:40But all this is now doing is it's creating a variable on line 11.
    • 2:35:45Setting that number equal to that same person's number. Why?
    • 2:35:48Because we're inside of this loop, I'm iterating over each person one at a time.
    • 2:35:53And that's what four,
    • 2:35:54that's what in does it assigns the person variable to this dictionary?
    • 2:35:59Then this dictionary, then this dictionary automatically for me.
    • 2:36:04No need for I and I plus plus and all of that.
    • 2:36:06So this is just saying if the current
    • 2:36:08person's name equals the name we're looking for,
    • 2:36:10get a variable called number and assign it that
    • 2:36:12person's number and then print out that person's number.
    • 2:36:16So
    • 2:36:16whereas last time we were just printing found and not found,
    • 2:36:18now I'm gonna print an actual number.
    • 2:36:20So if I run Python a phone book dot P and I search for Carter
    • 2:36:23there then is his number.
    • 2:36:24If I run Python a phone book dot P run uh type in John, there then is John's number.
    • 2:36:29And if I search for someone who's not there,
    • 2:36:31I instead just get not found.
    • 2:36:34So what's interesting and compelling about dictionaries
    • 2:36:37is they're kind of known as like the Swiss Army Knives of data structures
    • 2:36:40and programming because you can just use them in so many interesting clever ways.
    • 2:36:45Like if you ever want to associate something with something else,
    • 2:36:47like a dictionary is your friend and you no
    • 2:36:50longer have to write dozens of lines of code.
    • 2:36:52As in P at five, you can write single lines of code to achieve the same idea.
    • 2:36:57So for instance, if I too want to tighten this up,
    • 2:37:00I actually don't need this loop altogether.
    • 2:37:02An even better version of this code would be this.
    • 2:37:05Let me go ahead and get rid of this loop
    • 2:37:07and instead just say this
    • 2:37:09uh rather
    • 2:37:11if I wanna instead do this here, notice that
    • 2:37:15I don't need this variable.
    • 2:37:17Technically,
    • 2:37:18even though this will look a little uglier notice that I'm only creating a
    • 2:37:21variable called number because I want to set it equal to this person's number.
    • 2:37:24But strictly speaking,
    • 2:37:25anytime you've declared a variable and then used it the next line,
    • 2:37:29you don't really need it.
    • 2:37:30So I could do this, I could get rid of that line.
    • 2:37:33And instead of printing number in my curly braces, I could actually do person
    • 2:37:37square brackets and you might be inclined to do this.
    • 2:37:41But this is going to confuse Python because you're
    • 2:37:43mixing double quotes on the inside and the outside.
    • 2:37:46But you can use single quotes here compellingly.
    • 2:37:49So you don't have to do it this way.
    • 2:37:51But this is just to show you syntactically,
    • 2:37:53you can put most anything you want in these curly braces.
    • 2:37:56So long as you don't confuse Python by using
    • 2:37:59the same syntax.
    • 2:38:01But let me do one other thing here.
    • 2:38:03This is even more powerful.
    • 2:38:05Let me propose that if all you're storing is names and numbers, names and numbers,
    • 2:38:10I can actually simplify this dictionary
    • 2:38:13uh significantly. Let me actually red declare this
    • 2:38:17people
    • 2:38:18data structure
    • 2:38:20to be not
    • 2:38:21a
    • 2:38:21list of dictionaries. But how about just one big dictionary?
    • 2:38:25Because if I'm only associating names with numbers,
    • 2:38:27I don't technically need to create special keys called name and number.
    • 2:38:31Why don't I just associate Carter
    • 2:38:33with his number +1617495 1000.
    • 2:38:38Why don't I just associate quote unquote David with his number +1617495 1000.
    • 2:38:47And then lastly let's just associate John with his number +19499494682750
    • 2:38:57and that too would work.
    • 2:38:59But notice that I'm going to get rid of my list
    • 2:39:02of people and instead just have one dictionary of people.
    • 2:39:05The downside of which is that
    • 2:39:07you can only have one key, one value, one key, one value.
    • 2:39:10You can't have a name key and a number key and an email key and
    • 2:39:13an address key and any number of other pieces of data that might be compelling.
    • 2:39:17But if you've only got key value pairs like this,
    • 2:39:19we can tighten up this code significantly.
    • 2:39:21So that now down here, I can actually do this.
    • 2:39:24If the name I'm looking for is somewhere in that people dictionary,
    • 2:39:30then go ahead and get the person's
    • 2:39:32number by going into the people dictionary indexing
    • 2:39:36into it at that person's name and then printing out uh found for instance,
    • 2:39:42uh that here number, making this an F string.
    • 2:39:46Else you can go ahead and print out not found in this case here.
    • 2:39:50So again,
    • 2:39:50the difference is that the previous version created a list
    • 2:39:53of dictionaries and I very manually methodically iterated over it,
    • 2:39:56looking for the person.
    • 2:39:58But what's nice again about dictionaries is that Python gives you
    • 2:40:01a lot of support for just looking into them easily.
    • 2:40:04And this syntax just like you can use it for lists,
    • 2:40:07you can use it for dictionaries as well.
    • 2:40:09And Python will look for that name among the keys in the dictionary.
    • 2:40:13And if it finds it, you use this syntax
    • 2:40:16to get at
    • 2:40:18that person's
    • 2:40:19number.
    • 2:40:21OK, a lot all at once.
    • 2:40:23But are there any questions on this here syntax?
    • 2:40:26We'll then introduce a couple final features with the final flourish.
    • 2:40:30Yes.
    • 2:40:34In this case, I do not need to use brake because I don't have any loop involved.
    • 2:40:38So brake is only used as we've seen it
    • 2:40:41in the context of like looping over something
    • 2:40:43when you want to terminate the loop early.
    • 2:40:45But here, Python is doing the searching for you. So Python's taking care of that
    • 2:40:50automatically.
    • 2:40:52All right,
    • 2:40:52just a couple of final features so that you have a couple of more building blocks.
    • 2:40:56Here is the documentation for dictionaries themselves in case you want to
    • 2:40:59poke around as to what more you can do with them.
    • 2:41:02But it turns out that there are other libraries that come with Python,
    • 2:41:05not even third party.
    • 2:41:06And one of them is the SIS
    • 2:41:08uh uh the CIS library whereby you have system related functionality.
    • 2:41:13And here's its official documentation for instance.
    • 2:41:15But what this means is that certain
    • 2:41:17functionality that was just immediately available.
    • 2:41:19C is sometimes tucked away now into libraries in Python.
    • 2:41:23So for instance,
    • 2:41:24let me go over to VS code here and let me just create a program called greet dot pi,
    • 2:41:28which is reminiscent of an old C program that
    • 2:41:30just greets the user using command line arguments.
    • 2:41:33But in C recall that we got access to command line arguments with Maine and in
    • 2:41:38ARG
    • 2:41:38C and ARG V. But none of those have we seen at all today.
    • 2:41:42And in fact Maine itself is no longer required.
    • 2:41:45So if you want to do command line arguments
    • 2:41:47in P
    • 2:41:48on, you actually do this from the SIS library, you can import something called ARG V.
    • 2:41:54So ARG V still exists,
    • 2:41:55it's just tucked away inside of this library otherwise known as a module.
    • 2:41:59And I can then do this if the length of ARG V for instance, does not equal to, well,
    • 2:42:06then we're going to go ahead and do what we did.
    • 2:42:08Or rather let's do this.
    • 2:42:09If the length of RG V does equal two,
    • 2:42:11we're gonna go ahead and do what we did
    • 2:42:13a couple of weeks ago whereby I'm gonna print out
    • 2:42:16hello
    • 2:42:17comma
    • 2:42:18and then RG V bracket one for instance. So whatever is in location, one of that list
    • 2:42:23else, if the length of RG V is not equal to two,
    • 2:42:27that is the human did not type two words at the prompt.
    • 2:42:29Let's go ahead and print out hello world by default.
    • 2:42:33So we did the exact same thing in C.
    • 2:42:35The only difference here is that this now is how you get access to RG V.
    • 2:42:39So let me run this Python of greet dot pi and hit, enter. Hello World is all I get.
    • 2:42:44And actually I got an extra line break because out of habit,
    • 2:42:46I included backslash N but I don't need that in Python.
    • 2:42:48So let me fix that Python of Grete dot pi Hello
    • 2:42:51World. But if I do Python of Greek dot Pi Dav ID,
    • 2:42:55now notice that RG V equals two.
    • 2:42:58If I instead do something like Carter RG V now equals two. But there is a difference.
    • 2:43:03Technically, I'm typing three words at the prompt, three words at the prompt,
    • 2:43:07but RG V still only equals two because the command Python is ignored from RG V.
    • 2:43:12It's only the name of your profile and the thing you type after it.
    • 2:43:16So that's then how we might print out arguments uh in
    • 2:43:20uh Python using RV. Well, what else might we do using some of these here features?
    • 2:43:26Well, it turns out
    • 2:43:27that
    • 2:43:28uh you can exit from programs using the same sis library. So let me close greet dot
    • 2:43:32P. Let me open up exit dot P just for demonstration sake.
    • 2:43:36And let's do something like this. Let's import sis
    • 2:43:38and if the length of uh sis dot org V.
    • 2:43:43So here's just another way of doing this and actually,
    • 2:43:45I'll do it the same first from CIS
    • 2:43:47import
    • 2:43:48RG V
    • 2:43:49if the length of RG V
    • 2:43:53does not equal to.
    • 2:43:55Well,
    • 2:43:55let's actually yell at the user with something like missing command line argument.
    • 2:44:00And then what we can do is exit out of the program entirely using CIS dot exit,
    • 2:44:06which is a function there in,
    • 2:44:08but this only works if I also import sorry.
    • 2:44:12Uh
    • 2:44:14But notice that exit is a function in sis.
    • 2:44:17So you know what, it's actually more convenient in this case,
    • 2:44:19let's just import all of sis.
    • 2:44:21But because that has not given me direct access to RG V.
    • 2:44:24Let me do sis dot org V here and sis dot exit here.
    • 2:44:28Otherwise, if all is, well, let's just go ahead and print out something like hello,
    • 2:44:31comma cis dot org V bracket, one close quote and that will print out
    • 2:44:38uh hello. So and so, and when I'm ready,
    • 2:44:40exit with a non zero AAA non with a zero exit status,
    • 2:44:45I can actually start to specify these things here.
    • 2:44:47So just like in C if you wanna exit from a program with one or two or anything else,
    • 2:44:51you can use C dot Exit. And if you wanna exit with a zero,
    • 2:44:55you can do this here instead.
    • 2:44:57So we have the same capabilities as NC just accessed a little bit differently.
    • 2:45:03Let me propose that.
    • 2:45:05Let's see.
    • 2:45:07Let me propose that.
    • 2:45:09How about this?
    • 2:45:12Um
    • 2:45:15How about this?
    • 2:45:16If we want to go ahead and create something,
    • 2:45:19a little more interactive recall that there was that command a while back,
    • 2:45:22namely uh pip, whereby I ran PIP install face recognition.
    • 2:45:28That's one of the examples with which we began.
    • 2:45:29And that allows me to install more functionality from a third party
    • 2:45:32into my own code space or my programming environment more generally.
    • 2:45:36Well, we can have a little fun with this.
    • 2:45:37In fact,
    • 2:45:37let me go back to VS code here and just like there's a command in Linux called Kay,
    • 2:45:42whereby you can get the cow to
    • 2:45:43something you can also use this kind of thing in Python. So if I do pip, install
    • 2:45:48C
    • 2:45:49say this, if it's not installed already will install a library called cow say.
    • 2:45:52And what this means is that if I actually want
    • 2:45:55to code up a program called like Moo dot P,
    • 2:45:57I can import the Kose
    • 2:45:59library and I can do something simple like cows say dot cow
    • 2:46:03because there's a bunk function in this library called cow.
    • 2:46:07And I can say something like this is CS 50 quote
    • 2:46:10unquote.
    • 2:46:11How do I run this program? I can run Python of Moo dot pi
    • 2:46:15and
    • 2:46:16underwhelming
    • 2:46:17if I increase the size of my terminal window, run Python of mo dot pi.
    • 2:46:20We have that same adorable cow as before,
    • 2:46:23but I now have programmatic capabilities with which to manipulate it.
    • 2:46:27And so in fact, I could make this program a little more interesting.
    • 2:46:30I could do something like name equals quote
    • 2:46:33or
    • 2:46:33rather name equals input. What's your name and combine some of today's ideas?
    • 2:46:38And now I can say not this is CS 50 but something like quote unquote.
    • 2:46:41Hello comma person's name.
    • 2:46:44And now if I increase the size of my terminal rerun, Python of mo dot P,
    • 2:46:48it's not gonna actually move or say this is CS 50.
    • 2:46:51It's gonna say something like hello,
    • 2:46:53David and so forth and suffice it to say through other functions.
    • 2:46:56You can do not only cows but dragons and other fancy things too,
    • 2:47:00but even in Python two,
    • 2:47:01can you generate not just ask the art but actual art and actual images.
    • 2:47:06And the note I thought we'd end on is doing one other library.
    • 2:47:09I'm going to go back into VS code here. I'm gonna close mo
    • 2:47:12dot P.
    • 2:47:12I'm gonna do pip install QR code,
    • 2:47:15which is the name of a library that I might
    • 2:47:18want to install to generate uh QR codes automatically.
    • 2:47:22And QR codes are these two dimensional bar codes.
    • 2:47:24If you want to generate these things yourself,
    • 2:47:26you don't have to go to a website and type in a URL.
    • 2:47:28you can actually write this kind of code yourself. So how might I do this?
    • 2:47:32Well, let me go into a new file called say QR dot Pi.
    • 2:47:38And let me do this. Let me go ahead and
    • 2:47:42uh
    • 2:47:42import this library called QR code.
    • 2:47:45Let me go ahead and create a variable called image or anything else.
    • 2:47:48Let me set it equal to this library's QR
    • 2:47:51codes function called make no relationship to see.
    • 2:47:53It's just called make because you want to make a QR code.
    • 2:47:56Let me type in maybe the URL of a lecture video here on youtube. So like youtube
    • 2:48:02uh slash
    • 2:48:03XVFZJO five PGG
    • 2:48:10zero,
    • 2:48:12quote unquote.
    • 2:48:13And then I can go ahead and do image dot save because inside of this image variable,
    • 2:48:19which is a different data type that this library gave me.
    • 2:48:22It doesn't come with Python per se.
    • 2:48:23I can save a file like QR dot PNG and I can save it in the ping
    • 2:48:29format, the portable network graphic. And so just to be clear what this should
    • 2:48:33hopefully do for me is create a QR code
    • 2:48:35containing that particular URL but not as text,
    • 2:48:40but rather as an actual image that I can send,
    • 2:48:43I can post online or in our case generate into my code space and then open.
    • 2:48:47And so with all that said, we've seen a bunch of new syntax today,
    • 2:48:50a bunch of new features, but the ideas underlying Python
    • 2:48:54exactly the same as they've been in C.
    • 2:48:56It's just that you don't have to do nearly as much heavy lifting yourself.
    • 2:49:00And here, for instance, in just three lines of code,
    • 2:49:03can you generate a massive QR code that people can scan as you
    • 2:49:06can in a moment with your phones and actually link to something like
    • 2:49:10a CS 50 class. So let me go ahead and run Python of QR dot P
    • 2:49:15seems to have run, let me run code of QR dot PNG, which is the file I created.
    • 2:49:21I'll close my terminal window allow you an opportunity to scan this here.
    • 2:49:25Very CS 50 lecture
    • 2:49:29and,
    • 2:49:33and
    • 2:49:35is someone's volume up?
    • 2:49:38There we go. What a perfect ending. All right. That was CS 50. We'll see you next time.
  • CS50.ai
Shortcuts
Before using a shortcut, click at least once on the video itself (to give it "focus") after closing this window.
Play/Pause spacebar or k
Rewind 10 seconds left arrow or j
Fast forward 10 seconds right arrow or l
Previous frame (while paused) ,
Next frame (while paused) .
Decrease playback rate <
Increase playback rate >
Toggle captions on/off c
Toggle mute m
Toggle full screen f or double-click video