CS50P - Lecture 0 - Functions, Variables — Transcript
Full transcript
- 0:24All right. This is CS50's introduction
- 0:26to programming with Python. My name is
- 0:28David Malan, and this is our week on
- 0:30functions and variables. But odds are
- 0:32many of you, most of you, have never
- 0:34actually programmed before. So, let's
- 0:36start by doing just that. Let me go
- 0:38ahead here and open up my computer and
- 0:40on it a program called Visual Studio
- 0:42Code or VS Code, which is just a very
- 0:44popular program nowadays for actually
- 0:46writing code. Now, you don't have to
- 0:47write code using this particular tool.
- 0:50In fact, all we need at the end of the
- 0:51day is a a so-called text editor, a
- 0:53program for writing text. And heck, if
- 0:55you really want, you could even use
- 0:56something like Google Docs or Microsoft
- 0:58Word. You'd have to save it in the right
- 1:00format, but really at the end of the
- 1:02day, all you need is a program for
- 1:03writing text because that's what code
- 1:05is, text. Now, within this particular
- 1:07program, I'm going to have the ability
- 1:08to create one or more files via this top
- 1:11portion of the screen. And I'm going to
- 1:12do so by diving right in and doing this
- 1:15at the bottom of my screen. At the
- 1:17bottom of my screen is a so-called
- 1:18terminal window, and this is a command
- 1:21line interface or CLI interface to the
- 1:24underlying computer, be it your Mac or
- 1:26your PC or even some server in the
- 1:28cloud. And what I'm going to do here is
- 1:30literally write code and then the name
- 1:32of the file that I want to code, for
- 1:34instance, hello.py.
- 1:36As we'll soon see, any program that you
- 1:38write in Python generally has a file
- 1:40name that ends in .py to indicate to the
- 1:42computer that it's indeed a program
- 1:44written in Python. Now, you'll see here
- 1:46at the top of my screen I have a
- 1:47blinking cursor at line one, which is
- 1:49where the very first line of my code is
- 1:51going to go, and then just a tab that
- 1:52reminds me of the name of this file,
- 1:54hello.py. And without even knowing much
- 1:57Python, I'm going to write my very first
- 1:59program here as follows. Print open
- 2:02parenthesis, quote hello, {comma} world,
- 2:06close quote, and close parenthesis. And
- 2:08you'll see that at my keyboard, some of
- 2:10my thoughts were finished for me. I only
- 2:11had to type one parenthesis and the
- 2:13other one automatically appeared. And
- 2:15that's just a feature that we'll see of
- 2:16tools like this tool here. Now, even if
- 2:19you've never programmed before, odds are
- 2:21you can guess and fur what this simple
- 2:24program is going to do. And it's only
- 2:26one line. Print open parenthesis, quote
- 2:28hello world, close quote, close
- 2:30parenthesis. Indeed, when I run this
- 2:33program, ultimately it's just going to
- 2:34say hello to the world. And in fact,
- 2:36this is a very famous, perhaps the most
- 2:37canonical program that you can write as
- 2:39your very first program in Python or any
- 2:41other language. And so that's what I've
- 2:43done here. But on my Mac, my PC, even my
- 2:46phone, I'm generally in the habit like
- 2:48you of running programs by double
- 2:50clicking an icon or just tapping on the
- 2:52screen. But I see no such icons here.
- 2:54And in fact, that's because my interface
- 2:57to at least my current Mac or PC or some
- 2:59server in the cloud is again only a CLI,
- 3:02command line interface, which even
- 3:04though it might feel like it's a step
- 3:05back from the menus and buttons and
- 3:07icons that you and I take for granted
- 3:08every day, you'll find we think that
- 3:10it's ultimately a much more powerful
- 3:12interface and incredibly popular to use
- 3:14among programmers in the real world. So
- 3:17to run this program, I'm going to have
- 3:18to use a command. And I'm going to move
- 3:20my cursor back down to the bottom of the
- 3:22screen here, where previously I already
- 3:24ran one command. The command code, which
- 3:27has the effect of opening VS Code in my
- 3:29computer, and then I passed in the name
- 3:31of the file that I wanted to code up.
- 3:33Now I have a chance to type a second
- 3:35command, and you'll see I see a second
- 3:37dollar sign. Now the dollar sign here
- 3:38doesn't indicate any kind of currency or
- 3:40money, it just is the symbol that's
- 3:42generally used to indicate your prompt,
- 3:44where the command line interface wants
- 3:46you to put those commands. Now the
- 3:48command I can run here is going to be
- 3:50this. I'm going to run python of
- 3:53hello.py.
- 3:55Now, why is that? Well, it turns out
- 3:57that when I actually write code in a
- 3:58language like python, it's of course
- 4:00stored in that file hello.py, but I need
- 4:04to interpret the code top to bottom left
- 4:07to right so that the computer knows what
- 4:08to do. Indeed, at the end of the day,
- 4:10even if you don't really know much about
- 4:11computers, you've probably heard that
- 4:13computers only understand zeros and
- 4:15ones, the so-called binary system. Well,
- 4:17if that's the case, then something that
- 4:19says print and parenthesis and quote
- 4:22unquote hello world is not surely zeros
- 4:24and ones. We have to somehow translate
- 4:26it into the zeros and ones that the
- 4:27computer understands. Now, fortunately,
- 4:30so long as you've installed such a
- 4:32program in advance, there's a program as
- 4:34well as a language called python. So,
- 4:36python is not only a language in which
- 4:38we're going to write code, it's also a
- 4:39program, otherwise known as an
- 4:41interpreter, that you install for free
- 4:43on your own Mac or PC or some server in
- 4:45the cloud, and you can then run that
- 4:47program, that interpreter, passing to it
- 4:50as input the name of your file like mine
- 4:52here, hello.py, and then that program,
- 4:54that interpreter, will handle the
- 4:56process of reading it top to bottom left
- 4:58to right and translating it effectively
- 5:00into those zeros and ones that the
- 5:02computer can understand. So, let's do
- 5:04just that. Let me go back to VS Code
- 5:06here. I already typed out python of
- 5:08hello.py, but I didn't yet hit enter.
- 5:11And that's what's now going to kick off
- 5:12this command and hopefully, if I didn't
- 5:14mess any of this up, I should see my
- 5:17very first program's output to the
- 5:19screen. And voilà, hello world. So, if
- 5:23you too have typed exactly that same
- 5:25code and have executed exactly that same
- 5:27command, you will have written your very
- 5:29first program, in this case, in python.
- 5:32Well, now let's take a step back and
- 5:34consider what is it that we actually
- 5:36just did and what is it we're looking
- 5:37here on the screen. Well, first and
- 5:39foremost, in most any programming
- 5:40language, you tend to have access to
- 5:42what are called functions. A function is
- 5:44like an action or a verb that lets you
- 5:46do something in the program. And
- 5:49generally speaking, any language comes
- 5:51with some predetermined set of
- 5:52functions, some very basic actions or
- 5:55verbs that the computer will already
- 5:57know how to do for you, that the
- 5:58language really will know how to do for
- 6:00you, and you, the programmer, the human,
- 6:02can use those functions that will to get
- 6:04the computer to do those things. Now,
- 6:07the program in question here, hello.py,
- 6:10is using one function, and you can
- 6:12perhaps guess what it is. That function
- 6:13is of course going to be this function
- 6:15print. And that print function, of
- 6:18course, doesn't print some preordained
- 6:20string of text, that is to say, it
- 6:23prints whatever it is you want it to
- 6:25print. And here, too, do we have another
- 6:28uh piece of terminology in the world of
- 6:29programming, namely arguments. An
- 6:31argument is an input to a function that
- 6:34somehow influences its behavior. The
- 6:36people who invented Python, of course,
- 6:38didn't necessarily know what it is you
- 6:40and I are going to want to print to the
- 6:41screen, so they designed this print
- 6:43function using these parentheses with
- 6:46the ability to take as input some string
- 6:48of text, be it in English or any other
- 6:50human language, that is what you want
- 6:53this function ultimately to print onto
- 6:55the screen.
- 6:56And what is it that the program's
- 6:58ultimately doing on the screen? Well,
- 6:59it's printing, of course. It's showing
- 7:01us "Hello, world" on the screen, and
- 7:03that's generally in programming known as
- 7:04a side effect. It can be visual, it can
- 7:06be audio. In this case, it's something
- 7:08that appears on the screen, and
- 7:09functions, therefore, can indeed have
- 7:11these side effects. One of the things
- 7:13they can do as this verb or action is to
- 7:15display on the screen as a side effect
- 7:17something like those world words that we
- 7:19wanted, "Hello, world."
- 7:21So, that's my first program. And, you
- 7:23know, I'm feeling pretty good.
- 7:24Everything worked as planned. I didn't
- 7:26make any mistakes, but honestly, when
- 7:28you're learning how to program, and even
- 7:30once you've learned how to program years
- 7:32later, you're going to make mistakes.
- 7:34And those mistakes, of course, are
- 7:36referred to a term you might already
- 7:37know, which is that of a bug. A bug is a
- 7:39mistake in a program, and they can take
- 7:40so many forms and take comfort perhaps
- 7:44in knowing that over the coming weeks
- 7:46you're going to make so many mistakes.
- 7:48You're going to have so many bugs in
- 7:49your code just as I did and just as I
- 7:52still do and those bugs themselves are
- 7:54just mistakes that are problems for you
- 7:56to solve. And over the weeks to come
- 7:58we're going to give you a lot of tools,
- 8:00both mental and technical via which you
- 8:02can solve those problems, but just don't
- 8:04get discouraged if when writing your
- 8:05program for the first time it doesn't
- 8:07even work that first time. It will with
- 8:10time, with practice, and with
- 8:11experience. So, let me deliberately now
- 8:14make a mistake that there was a non-zero
- 8:16chance I might have done accidentally
- 8:18already, but I got lucky. Let me go
- 8:20ahead and just suppose I forgot to
- 8:22include something like the closing
- 8:24parenthesis at the end of this line of
- 8:25code. You know, the code is almost
- 8:27correct. It's like 99% of the way there,
- 8:30but now that I've pointed it out it's
- 8:31pretty obvious that it's missing that
- 8:33close parenthesis, but even little,
- 8:35seemingly minor details like that that
- 8:38you and I as humans wouldn't really care
- 8:40about and if you're sending an email or
- 8:41a text message out if whatever, it's
- 8:43just a typo, it's not that big a deal.
- 8:45It is going to be a big deal to the
- 8:46computer. A computer is going to take
- 8:48you literally and if you don't finish
- 8:50your thought in the way the language
- 8:52expects, it's not going to necessarily
- 8:54run at all. So, let's do this. I'm going
- 8:56to go ahead here and clear my screen
- 8:57down at the bottom just so I can start
- 8:59fresh and I'm going to go ahead and run
- 9:01this version of my program after having
- 9:04made that change by deleting the
- 9:05parenthesis. I'm going to go ahead and
- 9:06type python again of hello.py
- 9:09and this time when I hit enter I'm
- 9:11hoping I'm going to see hello world, but
- 9:13here we have an error on the screen, a
- 9:15so-called syntax error, which refers to
- 9:18my having made a mistake at my keyboard.
- 9:21And if this one fortunately is pretty
- 9:23straightforward, it indeed says that
- 9:25this open parenthesis was never closed
- 9:28and so that's probably pretty intuitive
- 9:30now what I need to do. I need of course
- 9:32to close it. Unfortunately, sometimes
- 9:34the error messages we'll see in the
- 9:35coming weeks are not going to be nearly
- 9:37that user-friendly, but there too again,
- 9:39with experience, with practice, will you
- 9:41get better at debugging such programs.
- 9:44Let me now make sure that I indeed fixed
- 9:46it correctly. Let me go ahead and run
- 9:47now hello.py and hit enter, and voila,
- 9:50we're back in business. Well, let me
- 9:52pause here and see if we have any
- 9:54questions now about Python itself,
- 9:56writing, or running even the simplest
- 10:00of these programs.
- 10:02Uh could I write code inside of Word or
- 10:04for example Microsoft Excel, and what's
- 10:07the barrier to doing that?
- 10:10A really good question, and allow me to
- 10:11very explicitly say to the entire
- 10:14internet that you should not write code
- 10:16with Microsoft Word. I mentioned that
- 10:18only because it's a tool via which you
- 10:20can write text, and code is at the end
- 10:22of the day just text, but it's not the
- 10:23right tool for the job. We don't need
- 10:25bold facing, underlining, paragraphs,
- 10:27and the like. We generally want
- 10:28something much simpler than Microsoft
- 10:30Word or Google Docs, and so VS Code is
- 10:33an example of just a more general
- 10:34purpose text editor. Its purpose in life
- 10:36is to allow you, the human, to edit
- 10:38text. Nowadays, these text editors come
- 10:40with many more features. In fact, you'll
- 10:42notice that even in my code here, even
- 10:44though it's just one line, there's a bit
- 10:46of color to it. The word print for me is
- 10:48appearing in blue. The parentheses are
- 10:50black, and we'll see as we might write
- 10:52more lines of code, more and more of the
- 10:53lines will come to life in various
- 10:55colors. Now, that's just one feature of
- 10:58a text editor. We'll see too that it has
- 10:59features like this built-in terminal
- 11:01window. It's going to have a built-in
- 11:02tool for debugging or finding problems
- 11:05with code, and it's just a very popular
- 11:06tool nowadays. are many, many others out
- 11:09there. You're welcome to use them for
- 11:10this course and beyond. We just happen
- 11:12to use this one in large part too
- 11:14because you can also use VS Code
- 11:15nowadays uh for free in the cloud. I
- 11:18have one other question here on
- 11:20programming with Python or Hello World
- 11:23or syntax more generally.
- 11:24Okay, I was going to ask if
- 11:28is it not possible to run the computer
- 11:29using the terminal window?
- 11:31I think I heard is it not if it's
- 11:33possible to run the program without the
- 11:36terminal window?
- 11:37Are you
- 11:37Yes, sir.
- 11:38Okay, you froze for me again, but let me
- 11:40infer what the question is. So, in this
- 11:42environment, as I've configured my
- 11:44computer, I can only run these Python
- 11:46programs via the terminal window. Now,
- 11:48that's good for me, the programmer or
- 11:50the person who's trying to learn how to
- 11:52program, but it's not very good if you
- 11:53want to ship this software and have
- 11:55other people use your actual code. You
- 11:57can absolutely write programs and then
- 12:00allow other people to use not a command
- 12:02line interface, but a graphical user
- 12:03interface or GUI, GUI. This is just one
- 12:06mechanism and perhaps I think the best
- 12:09one with which to start writing code
- 12:10because eventually it's going to give us
- 12:12a lot more control. Allow me to forge
- 12:14ahead here, but please feel free to
- 12:16continue asking questions along the way
- 12:18if only via the chat. And let's consider
- 12:20now how we might go about improving this
- 12:23program. Let's go about improving this
- 12:24program to make it a little more
- 12:26interactive and not just assume that
- 12:28everyone is going to want to be greeted
- 12:30more generically as hello world. Let's
- 12:32see if I can't get this program to say
- 12:33something like hello David or hello
- 12:35Jeremiah or hello Horatio or whatever
- 12:37the actual user's name is. Well, to do
- 12:40this, I'm going to go back up to
- 12:42hello.py and I'm going to add another
- 12:44line of code at the very top that simply
- 12:46says, for instance, uh what's your name,
- 12:49quote unquote, with a little extra space
- 12:51at the end. So, I'm printing to the user
- 12:53asking them a question for some input,
- 12:55but now I need another function to
- 12:56actually get input from the user. And
- 12:59perfectly, enough Python comes with a
- 13:01function named input. So, here I'm going
- 13:03to go ahead and call a function input
- 13:05open paren close paren. And that's going
- 13:08to prompt the user with just a blinking
- 13:09cursor waiting for them to type
- 13:11something in. Now, it turns out if I
- 13:13read the documentation for the input
- 13:15function, it actually takes an argument
- 13:17itself. I don't need to use print
- 13:19separately and then prompt the user for
- 13:21input, so I can actually simplify this
- 13:23code before we even use it. I'm going to
- 13:25go ahead here and take that same string
- 13:27from print, put it as an argument to the
- 13:29input function, and get rid of the print
- 13:31altogether. And in fact, that print
- 13:33would have added a new line anyway, so
- 13:34now I've just got a prompt where the
- 13:36user's cursor is going to end up
- 13:37blinking at the end of the line asking
- 13:39them, "What's your name?" In my terminal
- 13:41window, I'm going to run python of
- 13:44hello.py.
- 13:45Enter. Okay, we're making progress. It
- 13:48seems that this new function input is
- 13:49indeed prompting me, the human, for
- 13:51input. So, I'm going to type in my name,
- 13:52David, and hit enter. Unfortunately, it
- 13:55doesn't really do anything with my name.
- 13:57It just outputs it immediately. All
- 13:59right, well, I could fix this, right? I
- 14:01could go up to line two, and I could
- 14:03change world to David, and then back in
- 14:06my terminal window here, I can do python
- 14:08of hello.py. Enter. What's your name?
- 14:11David. Enter. And there we go. All
- 14:13right, now I'm up and running. Now my
- 14:14program is working as intended. Of
- 14:17course, this isn't really working as
- 14:20intended here. Let me go ahead and try
- 14:22pretending to be my colleague Carter
- 14:24here. Well, Carter's name is this. I'm
- 14:26going to go ahead and hit enter, and
- 14:27I'll see of course hello Carter. Well,
- 14:29obviously not, because I've hard-coded,
- 14:31so to speak, I've written literally my
- 14:33name inside of the string. So, we need
- 14:35some way now of actually getting back
- 14:38what the user's input is and doing
- 14:40something with it ultimately. And for
- 14:41this, we're going to leverage another
- 14:43feature of programming, specifically a
- 14:45feature of some functions, which is that
- 14:47they can have return values as well. If
- 14:50you think of input as being again this
- 14:52action, this verb, you can actually
- 14:53personify it as maybe a person, like a
- 14:56friend of yours that you've asked a
- 14:57question of, and you've asked your
- 14:58friend to go get input from someone
- 15:00else. Go ask that person their name. And
- 15:02if your friend comes back knowing that
- 15:04person's name, well, wouldn't it be nice
- 15:06if they handed that name back to you?
- 15:08That's kind of what we need
- 15:09metaphorically the function to do is get
- 15:11the user's input and then hand it back
- 15:13to me so that I, the programmer, can do
- 15:15something with it. But if it's going to
- 15:17be handed back to me, I kind of want to
- 15:19put it somewhere so that I can then
- 15:22print it back on the screen. I need to
- 15:24do the equivalent of take out like a
- 15:25piece of paper or post-it note, write
- 15:27down on this piece of paper what it is
- 15:30the human has said so that I can then
- 15:32feed it into as input that print
- 15:34function. And to do that, we're going to
- 15:35need one more feature of programming,
- 15:37namely variables. And odds are most
- 15:40everyone's familiar with variables from
- 15:41math class way back when, X and Y and Z
- 15:44and the like. Well, programming has that
- 15:46same capability, this ability to create
- 15:48a variable, in this case in the
- 15:50computer's memory, not just on a piece
- 15:52of paper, and that variable can store a
- 15:54value, a number, some text, even an
- 15:57image or video or more. A variable is
- 15:59just a container for some variable
- 16:02a variable is just a container for some
- 16:04value inside of a computer or inside of
- 16:07your own program. So, how do I go about
- 16:09expressing myself in this way? Well, I
- 16:11think what I'm going to do is introduce
- 16:13a variable that's a little more
- 16:14interestingly named than X or Y. I could
- 16:17just say this, X equals input, but I'm
- 16:20going to use a better name than a
- 16:21typical mathematical variable here, and
- 16:23I'm going to literally call my variable
- 16:25name. Why? Well, in programming, because
- 16:27I have a whole keyboard in front of me,
- 16:28I can use more descriptive terms to
- 16:30describe what it is I'm writing. And now
- 16:32though, there's an opportunity to
- 16:34consider a specific piece of syntax.
- 16:36We've seen parentheses, we've seen
- 16:38quotes, all of which are necessary when
- 16:40passing inputs to a function, but this
- 16:42equal sign here, that's in between input
- 16:46on the right and name on the left, is
- 16:48actually important. And it's technically
- 16:50not an equal sign per se. It doesn't
- 16:52mean equality as much as it means
- 16:54assignment. So, in Python and many
- 16:57programming languages, a single equal
- 16:59sign is the assignment operator. And
- 17:01what that means specifically is that you
- 17:03want to assign from right to left
- 17:06whatever the user's input is. So, the
- 17:09equal sign copies from the right to the
- 17:12left whatever the return value of the
- 17:15function on the right is. So, again, the
- 17:18input function clearly gets input from
- 17:20the user. That's why I was able to type
- 17:21my name or Carter's, but it also, sort
- 17:24of behind the scenes, hands that value,
- 17:27that return value, back to me, the
- 17:29programmer. And if I use an equal sign
- 17:31and a variable, no matter what I call
- 17:33it, I can store that input in that
- 17:36variable so as to reuse it later. So,
- 17:38now sitting in the computer's memory
- 17:39somewhere is a container containing
- 17:42David, {quote} {unquote}, or Carter,
- 17:44{quote} {unquote}, or whatever the human
- 17:46has typed in.
- 17:47But here it's easy to make a mistake.
- 17:49Suppose I decide to try to print that
- 17:52name, and so I I kind of, on a hunch,
- 17:55type in this, "Hello, {comma} name."
- 17:57Just kind of plugging in the name of the
- 17:59variable. Well, let me go ahead here and
- 18:01run python of hello.py and hit enter.
- 18:05That's going to prompt me for my name,
- 18:06and let me type in my name, d a v i d,
- 18:08but I haven't hit enter yet. And perhaps
- 18:10via the chat, what's going to happen
- 18:12here when I now hit enter? I'm hoping it
- 18:16says, "Hello, David." I'd be okay if it
- 18:19says, "Hello, world." But I don't want
- 18:21it to say what it's actually going to
- 18:22say. And yep, what we're seeing in the
- 18:24chat is, well, it's probably going to
- 18:26say, literally, "Hello, {comma} name."
- 18:28So, that's not quite right. So, we need
- 18:30another way of printing out the value
- 18:33inside of that variable rather than just
- 18:35this word name. Well, let me try this in
- 18:37a couple of different ways. Let me try
- 18:39this as follows. Let me go ahead and
- 18:41maybe undo this because I've gotten
- 18:43pretty good already at saying hello. So,
- 18:45let's let, you know, let's draw that
- 18:46line in the sand and just say, "All
- 18:47right, let's get at least get hello,
- 18:48{comma}, out the door." Let's now print
- 18:51name, and just on a hunch, I'm going to
- 18:53try this. I'm going to use print again,
- 18:55cuz you can use this functions as many
- 18:56times as you need, and I'm going to pass
- 18:59to the name, to the print function, the
- 19:01variable called name. But notice, I'm
- 19:03being a little clever now. I'm not
- 19:05putting it in double quotes because
- 19:06we've seen already that double quotes
- 19:08means literally print out n a m e. I'm
- 19:10getting rid of the quotes this time in
- 19:12hopes that now by passing the variable
- 19:15called name to the function called
- 19:18print, it will in fact go about printing
- 19:20the contents of that variable, that is
- 19:23it's so-called value. All right, let's
- 19:25go ahead and do this here. Python of
- 19:27hello.py enter. What's your name? David.
- 19:30And now crossing my fingers still, I see
- 19:32hello, {comma} David. All right, so it's
- 19:36not the best program. I'm I'm kind of
- 19:38cutting some corners here so to speak.
- 19:40I'm saying hello David on two separate
- 19:43lines. So it's not as elegant, it's not
- 19:45as pretty, it's not as grammatically
- 19:46appropriate in English as just saying it
- 19:48all in one breath on one line, but at
- 19:50least I've solved the problem. Just not
- 19:52very well yet. But let me take a step
- 19:54back now and perhaps introduce a couple
- 19:56of other concepts with which we should
- 19:58be familiar, which is as our programs
- 20:00get longer and they're no longer just
- 20:02one line or two or even three,
- 20:04eventually our programs are going to
- 20:05become dozens of lines, maybe even
- 20:07hundreds of lines long. Let's set the
- 20:09stage for success moving forward. It
- 20:12turns out that Python and a lot of
- 20:13programming languages also support
- 20:15something called comments. Comments are
- 20:18notes to yourself in your code. And you
- 20:21include comments by way of a special
- 20:23symbol. In Python it's going to be the
- 20:24hash symbol typically, and that allows
- 20:26you to write the equivalent of a note to
- 20:28yourself, but in a way that's not going
- 20:30to break your code. The computer
- 20:31actually ignores your comment. It's just
- 20:33there for you. It's just there for your
- 20:35teacher. It's just there for your
- 20:36colleague with whom you're sharing
- 20:38ultimately that code. So if I go back to
- 20:40VS code here, and I just want to add
- 20:42some comments to this program to explain
- 20:44to my teacher, to myself, to my
- 20:47colleagues what this program is doing,
- 20:49well, let's go ahead and do that. I'm
- 20:50going to go at the very top of my
- 20:51program,
- 20:53and on line one now, I'm going to move
- 20:55that original line of code down a bit.
- 20:57I'm going to add a hash, and I'm going
- 20:58to say something like this, uh ask user
- 21:01for their name. Now, I don't have to use
- 21:04that language. I don't have to use that
- 21:05uh that text. I could use any human
- 21:07language whatsoever. It doesn't have to
- 21:09be English, but I'm going to now below
- 21:12that just say something like this. Say
- 21:14hello to user. And you'll notice that VS
- 21:16Code by default is kind of graying out
- 21:18my comments. They're no longer blue.
- 21:20There's no red. There's no color in
- 21:21them. And that's just because they're
- 21:22notes to myself and the computer
- 21:24ultimately is going to ignore them. But
- 21:26what we have now is two comments. Ask
- 21:29user for their name and then a second
- 21:30comment, say hello to user. And I've
- 21:32just kind of commented each chunk of
- 21:35code. Like each line or lines plural of
- 21:38code that are doing something
- 21:39noteworthy. Why? Well, tomorrow morning
- 21:41when I wake up having, you know,
- 21:44slept for quite some time, forgotten
- 21:46what it is I did the previous day, it's
- 21:47convenient with comments to just see in
- 21:49English or your own human language what
- 21:52it is this program is doing. So that you
- 21:54don't have to read the code itself. And
- 21:56better yet, if there's maybe a mistake
- 21:58down the road, you can read what your
- 22:00intention was and then you can look at
- 22:02the code and figure out if your code's
- 22:03now doing what you intended. So this
- 22:06isn't really necessary for a program
- 22:08this small. It's pretty obvious with
- 22:10just one or two or three lines what the
- 22:11program's doing. It's just as fast to
- 22:13read the code than the comment. But
- 22:14getting into this habit is generally a
- 22:16good thing to comment your code every
- 22:18one or few lines so as to remind
- 22:20yourself and others what it is your
- 22:22intent and your code is doing. What's
- 22:24nice about comments too is this.
- 22:26Comments can also serve to be sort of a
- 22:29to-do list for yourself. There's this
- 22:31notion in programming of pseudo code.
- 22:33Pseudo code isn't a formal thing. It's
- 22:35not one specific language. It's just
- 22:37using English or your own human language
- 22:40to express your thoughts succinctly,
- 22:42methodically, algorithmically so to
- 22:44speak. But pseudo code therefore,
- 22:46because it's not Python and it's not
- 22:48necessarily English, it just kind of
- 22:50allows you to outline your program even
- 22:52in advance. So for instance, if I wasn't
- 22:54sure today how I wanted to go about
- 22:57writing this program, but I did know
- 22:58what I want to do, I I have started
- 23:01today by just writing this in hello.py.
- 23:04No code. I could have written just a
- 23:06couple of comments to myself. Step one,
- 23:08ask user for their name. Step two, say
- 23:10hello to user. Then, once I've outlined
- 23:13my program in pseudo code, then I can go
- 23:15in there and say, all right, how do I
- 23:16ask the user for their name? Uh well, I
- 23:18can do input {quote} "What's your name?"
- 23:20{question mark} and then on the left
- 23:22here, I can maybe put a variable and
- 23:24assign it to that. Okay, how do I say
- 23:26hello to the user? Well, I know I can
- 23:28use print to say things on the screen.
- 23:30Let me say hello, {comma}
- 23:32uh and let me Okay, let me now print the
- 23:33person's name. So, again, pseudo code is
- 23:36a nice way of structuring your to-do
- 23:38list, especially if you have no idea how
- 23:40to write the code because it breaks a
- 23:42bigger program down into small bite-size
- 23:45tasks. All right, let me pause here to
- 23:47see if there are now any questions on
- 23:49comments, pseudo code, return values, or
- 23:53variables.
- 23:56Any questions we can clear up here?
- 23:59Yeah, my question is uh does the
- 24:00function input uh work for any type of
- 24:04of information or only for words?
- 24:07Yeah, good really good question. So,
- 24:08according to its documentation, and
- 24:10we'll look more at formal documentation
- 24:12soon, input is going to expect what's
- 24:13called a string. That is a sequence of
- 24:16text, be it in English or any other
- 24:18human language, but it's indeed going to
- 24:19be expecting text with which to prompt
- 24:22the user.
- 24:23A good question. How about another
- 24:24question from the group if we could?
- 24:26Uh I wanted to ask how I make several
- 24:29line comments.
- 24:31Oh, how do you do many lines of comments
- 24:32if I if I'm hearing you correctly? Sure,
- 24:35uh you would just keep doing them like
- 24:37this. You just prefix each of the lines
- 24:40with a hash symbol like I'm doing here.
- 24:43There is another technique for doing
- 24:45multi-line comments in Python that
- 24:46actually tend to have special meaning.
- 24:48You can do three double quotes like this
- 24:51and then anything in between here is a
- 24:54comment. That's That's technique or you
- 24:56can use single quotes as well, but more
- 24:57on those, I think, another time.
- 25:00All right. Well, if you don't mind, let
- 25:01me forge ahead here and see how we might
- 25:03improve this program further, and also
- 25:05introduce a few other features that we
- 25:07might want to take into account over
- 25:09time. So, it turns out that we can
- 25:11certainly improve on this program
- 25:13because it's a little disappointing that
- 25:16I'm cutting this corner and saying
- 25:17"Hello," {comma} and then on a new line
- 25:19printing out name. Like, we can do
- 25:21better. And most programs you use on
- 25:22your phone or your laptop certainly keep
- 25:24text together when people want. So, how
- 25:27can we go about doing that? Well,
- 25:28there's a few different ways. And, in
- 25:29fact, the goal here is not so much to
- 25:31solve this one problem, but to
- 25:33demonstrate and emphasize that in
- 25:34programming, Python, and other
- 25:36languages, there's so many ways,
- 25:37sometimes, to solve the same problem.
- 25:40And here's one way to solve this
- 25:42problem. Let me go in here, and let me
- 25:44go ahead now and say "Hello," {comma}
- 25:48and let me just add to the end of that
- 25:50the user's name. So, I'm using plus in
- 25:53kind of an interesting way. This is not
- 25:55addition, per se. I'm not adding
- 25:57numbers, obviously, but I do kind of
- 25:59want to add the person's name to the
- 26:02string of text "Hello," {comma}. Well,
- 26:05let me go now down to my terminal window
- 26:06and run python of hello.py again. Enter.
- 26:10What's your name? I'm going to type in
- 26:11David. Enter. Okay, it's better. It's
- 26:14better, but there's a minor bug, albeit
- 26:16aesthetic, here. There's missing space,
- 26:19but let's just use some intuition here.
- 26:20Well, if I'm missing the space after the
- 26:22comma, why don't I go ahead and just add
- 26:24it manually here? Let me now rerun the
- 26:26program, python of hello.py. Enter.
- 26:29David. Enter. And there we go. Now, we
- 26:31have something that looks a little
- 26:32prettier in terms of English grammar.
- 26:34Hello, {comma} {space} David. And now,
- 26:37if we rewind, you might have noticed
- 26:39before or wondered why I had this
- 26:42seemingly extra space after my question
- 26:44mark, namely here. There's a space after
- 26:46the question mark but before the double
- 26:48quote, and that was just for aesthetics,
- 26:49too. I wanted to move the user's cursor
- 26:51one space to the right so that when I
- 26:53type their name or they type their name,
- 26:55it's not immediately next to that same
- 26:58question mark there. But there's other
- 27:00ways we can do this. It turns out that
- 27:02some functions, print among them,
- 27:05actually take multiple arguments. And it
- 27:07turns out that if you separate the
- 27:10inputs to a function, the so-called
- 27:12arguments to a function with a comma,
- 27:14you can pass in not just one but two,
- 27:16three, four, five, onward. So let me go
- 27:19ahead and pass in not just hello,
- 27:21{comma} {space} but that
- 27:24followed by name. And this is a little
- 27:26confusing potentially at first glance
- 27:27cuz now I've got two commas, but it's
- 27:29important to note that the first comma
- 27:31is inside of my quotation marks, which
- 27:34is uh simply an English grammatical
- 27:36thing. The second comma here is outside
- 27:39of the quotes but between what are now
- 27:41two separate arguments to print. The
- 27:44first argument is hello, {comma}
- 27:45{space}. The second argument is the name
- 27:48variable itself. So let's see how this
- 27:50looks.
- 27:51Python of hello.py, enter. What's your
- 27:54name? David, enter. Okay, I've kind of
- 27:57overcorrected. Now I've got two spaces
- 27:59for some reason. Well, it turns out and
- 28:00this is subtle, when you pass multiple
- 28:03arguments to print, it automatically
- 28:06inserts a space for you. This was not
- 28:08relevant earlier because I was passing
- 28:10in one big argument to print all at once
- 28:14by using that plus operator. This time
- 28:16I'm passing in two because of the comma.
- 28:19So if I don't want that extra space, I
- 28:21don't need to pass in one myself. I can
- 28:23just do this and now notice if I run
- 28:25this program again, Python of hello.py,
- 28:28type in my name, David. Now it looks
- 28:30grammatically like I might want. Now
- 28:32which of these approaches is better?
- 28:34This approach uses a function print with
- 28:36two arguments, hello, {comma} and the
- 28:39name variable. The previous version,
- 28:41recall, technically used one argument
- 28:44even though it looked a little curious.
- 28:45It's one argument in the sense that the
- 28:47computer, just like mathematicians, are
- 28:49going to do what's inside of parentheses
- 28:51first. So, if inside of parentheses you
- 28:53have this string of text, "Hello,
- 28:55{comma} and a space", which I need to
- 28:57add back, then you have a plus, which
- 28:59means not addition per se, but
- 29:01concatenation, to join the thing on the
- 29:03left and the thing on the right. This
- 29:05ultimately becomes the English phrase
- 29:08"Hello, {comma} {space} David", and then
- 29:11what's being passed ultimately to the
- 29:13function is technically something like
- 29:16this, but it's doing it all dynamically.
- 29:18It's not me typing in David as I I
- 29:21secretly as I discreetly did earlier,
- 29:23it's figuring out dynamically what that
- 29:25value is after concatenating "Hello"
- 29:27with the value of name, and then passing
- 29:30that ultimately to print as the sole
- 29:32argument. Let me pause here to see if
- 29:34there's any questions on
- 29:36numbers of arguments now
- 29:39to functions.
- 29:41Can we use a function
- 29:43uh many times to solve a certain problem
- 29:45which we can encounter many times in our
- 29:47code?
- 29:48You can. You can use a function many
- 29:50different times to solve some problem.
- 29:52What we'll soon see though is if you
- 29:54find yourself as the programmer solving
- 29:56a problem the same way again and again
- 29:59and again, it turns out you'll be able
- 30:00to make your own function so that you
- 30:02don't have to keep reusing the basic
- 30:04ones that come with the language.
- 30:07I was curious about the {comma} and the
- 30:09plus sign. So, after plus sign, can we
- 30:12give just one variable and after
- 30:14{comma}, can we give multiple variables?
- 30:16Like, what is the difference?
- 30:18A good question. So, in the context of
- 30:20strings, and I keep using that term,
- 30:22string is a technical term in a
- 30:24programming language, and again it means
- 30:25a a sequence of text, a character, a
- 30:28word, a whole paragraph even. So, the
- 30:30plus operator is not just used, as we'll
- 30:33see, for addition of numbers in Python
- 30:36like we do on paper pencil, but it also
- 30:38is used for concatenation of strings on
- 30:40the left and the right. If you did want
- 30:42to combine not just two strings, left
- 30:44and right, but a third and a fourth, you
- 30:46can absolutely keep using plus, plus,
- 30:49plus, plus, and chain them together just
- 30:51like in math. Eventually, that's going
- 30:53to start to look a little ugly, I dare
- 30:55say, especially if your line of code
- 30:56gets long. So, there's better ways that
- 30:58we'll actually soon see. And a good
- 31:00question as well. Well, let me come back
- 31:02to the code here in question and see if
- 31:04we can't show you just a couple of other
- 31:06ways to solve the same problem, along
- 31:08the way emphasizing that what we're
- 31:09technically talking about here, yes, are
- 31:11strings, but there's even a technical
- 31:13term for these strings. In Python, it's
- 31:15just str, so to speak, str for short,
- 31:18for string. As you may know, if you
- 31:20program in other languages, people who
- 31:22invent programming languages like to be
- 31:24very uh succinct, to the point, so we
- 31:26tend to use fairly short phrases to
- 31:28describe things, not necessarily full
- 31:30words. So, while you might say string,
- 31:33technically in Python, what we're really
- 31:34talking about, these sequences of text,
- 31:36are technically strs. This is an actual
- 31:39type of data in a program. But, we'll
- 31:41soon see that there's other types of
- 31:43data in programs as well. In fact, let's
- 31:46see if we can't improve this in one
- 31:48other way. I like the progress we've
- 31:50made by keeping everything on the same
- 31:52line, "Hello, David."
- 31:54all on the same line. What more though
- 31:56could we do in terms of solving this
- 31:58problem? Well, it turns out that we
- 32:00didn't have to give up entirely with
- 32:02using print twice. Let me rewind a
- 32:04little bit and go back to that earlier
- 32:06version where I wasn't really sure how
- 32:08to solve this problem. So, I was using
- 32:10print once to print out just the hello
- 32:12and the space and the comma, and then I
- 32:14used print again to call uh to print
- 32:17name. That, strictly speaking, wasn't
- 32:19bad, but there was this visual side
- 32:21effect that I just didn't like. It just
- 32:23looked ugly to have these two lines of
- 32:25text separate from one another. But,
- 32:27there's another way to fix this.
- 32:29Clearly, it seems to be the case that
- 32:31the print function is automatically
- 32:34outputting a blank line. It's moving the
- 32:36cursor automatically for me to the next
- 32:38line because that's why I'm seeing hello
- 32:41on one line and David on the next and
- 32:43then my prompt, the dollar sign, on the
- 32:45line below that. So, print seems to be
- 32:47presuming automatically that you want it
- 32:49to move the cursor to the next line
- 32:51after you pass it some argument. But,
- 32:53you can override that behavior. Again,
- 32:55functions take arguments which influence
- 32:57their behavior. You just have to know
- 32:59what those arguments are. And it turns
- 33:01out that if we look at the documentation
- 33:04for Python's print function, we can
- 33:07actually look up at this URL here.
- 33:09docs.python.org is where all of Python's
- 33:12official documentation lies. If I poke
- 33:14around, I can find my way to more
- 33:16specifically this URL here where I can
- 33:18find all of the available functions in
- 33:20Python
- 33:22that and the documentation therefore.
- 33:24And if I go a little more precisely, I
- 33:26can even find specific documentation for
- 33:28the print function itself. And rather
- 33:30than pull that up in a browser, I'm
- 33:31going to go ahead and highlight just one
- 33:33line from that same URL
- 33:35which is this. And this is easily the
- 33:37most cryptic thing we've seen yet, but
- 33:40this is the official documentation for
- 33:42the print function. And one of the best
- 33:43things you can do when learning a
- 33:44programming language is honestly learn
- 33:46to read the documentation because truly
- 33:48all of the answers to your questions
- 33:50will in some way be there even though
- 33:53admittedly it's not always obvious. And
- 33:55I will say too, Python's documentation
- 33:57isn't necessarily the easiest thing
- 33:59especially for a first-time or novice
- 34:01programmer. It too just takes practice,
- 34:03so try not to get overwhelmed if you're
- 34:04not sure what you're looking at. But,
- 34:06let me walk you through this example.
- 34:08This again is a line of text from
- 34:09Python's official documentation for the
- 34:11print function. What this indicates as
- 34:13follows is this. The name of this
- 34:15function is of course print. Then
- 34:17there's a parenthesis over here and
- 34:18another close parenthesis way over
- 34:20there. Everything inside of those
- 34:22parenthesis are the arguments, the
- 34:24potential arguments to the function.
- 34:27However, when we're looking at these
- 34:29arguments in the documentation like
- 34:32this, there's technically a different
- 34:34term that we would use. These are
- 34:35technically the parameters to the
- 34:37function. So, when you're talking about
- 34:39what you can pass to a function and what
- 34:42those inputs are called, those are
- 34:44parameters. When you actually use the
- 34:46function and pass in values inside of
- 34:49those parentheses, those inputs, those
- 34:51values are arguments. So, we're talking
- 34:54about the exact same thing. Parameters
- 34:55and arguments are effectively the same
- 34:57thing, but the terms you use from
- 34:59looking at the problem from different
- 35:00directions. When we're looking at what
- 35:02the function can take versus what you're
- 35:04actually passing into the function. So,
- 35:06what does this imply? Well, this syntax
- 35:08is pretty cryptic, but at the moment,
- 35:10just know that an asterisk, a star, and
- 35:13then the word objects means that the
- 35:14print function can take any number of
- 35:16objects. You can pass in zero strings of
- 35:19text, one string like I did, two strings
- 35:21like I did, or technically infinitely
- 35:23many if you you really want, though that
- 35:25code's not going to look very good.
- 35:27After that, we see a comma, then we see
- 35:29another parameter here called sep, short
- 35:32for separator in English, and notice the
- 35:34equal sign and the single quote space
- 35:37single quote. So, quote unquote space. I
- 35:40don't know what that is yet, but I I I
- 35:42think we've seen a hint about it. Let's
- 35:44focus though for a moment on this. The
- 35:46print function takes another parameter
- 35:48called end, and the default value of
- 35:51that parameter is apparently, based on
- 35:53this equal sign and these quotes,
- 35:55backslash n.
- 35:57And what is backslash n? If you'd like
- 35:59to chime in in the chat. Anyone who's
- 36:00programmed before has probably seen
- 36:02this, though if you've never programmed
- 36:03before, this might look quite cryptic.
- 36:07Backslash n
- 36:09means new line, and it's a way textually
- 36:12of indicating if and when you want the
- 36:14computer effectively to move the cursor
- 36:16to the next line, create a new line of
- 36:18text. And so, technically, if we read
- 36:21into the documentation, we'll see more
- 36:22detail on this,
- 36:24the fact that there's a parameter called
- 36:26end in the documentation for the print
- 36:28function just means that by default this
- 36:30print function is going to end every
- 36:33line with backslash n. You don't
- 36:35literally see backslash n, you see a new
- 36:37line, you see the cursor moving to the
- 36:39next line. Now, by that logic, let's
- 36:41move backwards. Sep for separator. The
- 36:44default value of separator is apparently
- 36:47a single blank space. Well, where have
- 36:50we seen that? Well, recall in earlier
- 36:52example when I passed in not just one
- 36:54but two arguments to the print function,
- 36:57recall that they magically had a space
- 36:59between them. In fact, they had that
- 37:01space plus my own space and that's why I
- 37:02deleted my space because at that point
- 37:04it was extra. So, this just means that
- 37:06when you pass multiple arguments to
- 37:08print, by default they're going to be
- 37:09separated by a single space. By default
- 37:12when you pass arguments to print, it's
- 37:14the whole thing is going to be ended
- 37:16with a new line. Now, just by knowing
- 37:18this and let me literally wave my hand
- 37:20at the rest of the documentation for
- 37:22another day. There's more things that
- 37:24print can do, but we're going to focus
- 37:25just on sep and on end, let's see if we
- 37:28can't leverage this now to solve that
- 37:29original problem. The original problem
- 37:31was this, I don't like how hello {comma}
- 37:33David is on two different lines. Well,
- 37:35that's happening again because print is
- 37:37automatically printing out a new line,
- 37:39so let's tell it not to do that. Let's
- 37:42tell it by passing a second argument to
- 37:45the first use of print to say end equals
- 37:49{quote} unquote not backslash n, which
- 37:52is the default automatically, let's make
- 37:55it {quote} unquote nothing else. Let's
- 37:58override the default value, so there is
- 38:00no new line, there's literally nothing
- 38:02there. And let's see what happens. Let
- 38:05me now go down to my terminal window and
- 38:07clear it, and I'm going to run python of
- 38:09hello.py enter. I'm going to type in my
- 38:11name David and I think now everything's
- 38:14going to stay on the same line because,
- 38:16and it did,
- 38:17this line here five is is to print out
- 38:20hello {comma} space, but then nothing at
- 38:23the end of it because I changed it to be
- 38:25{quote unquote}. The second line is
- 38:27going to print the name, David, or
- 38:29whatever the human's name is, and it
- 38:31will move the cursor to the next line
- 38:32because I didn't override the value of
- 38:35end there. Just to see this more
- 38:37explicitly, if you do something cryptic
- 38:39like, "Well, I have no idea what's going
- 38:41on. Let me just put in temporarily three
- 38:42question marks here." We'll see the
- 38:44results of this, too. Let me go back
- 38:46down to my terminal window, run python
- 38:48of hello.py What's your name, David? And
- 38:50now you see literally really ugly
- 38:53output, but you see literally what's
- 38:55going on. Hello, {comma} space, then
- 38:57three question marks, and that print
- 39:00statement, and then you see d a v i d.
- 39:03So, not a good outcome, but it
- 39:06demonstrates just how much control we
- 39:07have here, too. And let me rewind
- 39:09further. Recall that in our other
- 39:11version of this,
- 39:13when I passed in hello, {comma} and
- 39:15name, they were separated by a single
- 39:18space. So, python of hello.py
- 39:20d a v i d {enter} That just worked.
- 39:23Well, what if we override the value of
- 39:26sep for separator instead of being one
- 39:29space, we could say something like uh
- 39:33{question mark} {question mark}
- 39:34{question mark} just to wrap our minds
- 39:35around what's going on there. Let me now
- 39:37do python of hello.py d a v i d {enter}
- 39:41and you see two. These two inputs,
- 39:43hello, {comma} and the name are now
- 39:46separated in an ugly way by three
- 39:48question marks because I've overridden
- 39:50the default behavior of sep. And even
- 39:52though the documentation uses single
- 39:54quotes, I've been in the habit of using
- 39:55double quotes. In Python, you can use
- 39:57either. Strictly speaking, it doesn't
- 39:59matter, but you should be consistent,
- 40:01and I generally always use double
- 40:02quotes. Python's documentation, though,
- 40:04always uses single quotes.
- 40:08Questions now on these types of
- 40:11parameters. And allow me to propose that
- 40:13we give these an official name. Up until
- 40:15now, when we've been passing values to
- 40:18print, those are called positional
- 40:20parameters. Positional in the sense that
- 40:22the first thing you pass to print gets
- 40:23printed first. The second thing you pass
- 40:25to print after a comma gets printed
- 40:27second, and so forth. But, there's also
- 40:29these things we've now seen called named
- 40:31parameters, named SEP, separator, or
- 40:35end, END, for the line ending. Those are
- 40:37named parameters because, one, they're
- 40:39optional and you can pass them in at the
- 40:42end of your print statement, but you can
- 40:44also call uh use them by name.
- 40:48This may be a weird question, but I was
- 40:49wondering uh what if someone wants to uh
- 40:52like add actually quote quotation marks
- 40:56within the quotation marks?
- 40:57Yeah, I like how you think. This is what
- 41:00we would call a corner case, right? Just
- 41:02when we've made Right, this is This is
- 41:03all sounding great, at least as as
- 41:05programming goes, but wait a minute,
- 41:06what if you want to print a quote?
- 41:08That's a really good question. Well,
- 41:09let's see if we can't figure this out.
- 41:11Suppose that I want to print out not
- 41:14just the user's name. Let me simplify
- 41:16this further. Let me go ahead and get
- 41:17rid of all of this, and let me just say
- 41:19something like hello, um
- 41:24maybe I'm being a little sarcastic here.
- 41:26Hello, friend, you know, in that kind of
- 41:29tone. Well, this is not going to work,
- 41:31actually, because you are trying to use
- 41:33quotes to be like friend in finger
- 41:35quotes, but you're also trying to end
- 41:37the sentence. And if I try running this,
- 41:38let's do this. python ifhello.py, you'll
- 41:41see that this is just invalid syntax.
- 41:43Perhaps you forgot a comma. And this is
- 41:44actually a bit annoying. Sometimes the
- 41:46error messages you see are misleading.
- 41:48Like, the computer the language doesn't
- 41:50really know what's going on, so it gives
- 41:52its best guess, but it's not necessarily
- 41:54correct. But, I can solve this problem
- 41:56in a couple of ways. I can do this. I
- 41:59can change my outermost quotes to single
- 42:01quotes, cuz recall a moment again I said
- 42:03you could use double quotes or single
- 42:05quotes so long as you're consistent. So,
- 42:07that's fine. If you use single quotes on
- 42:08the outside, you can then use double
- 42:10quotes on the inside, and you'll see
- 42:12them literally. So, for instance, if I
- 42:14run python of hello.py, there we go.
- 42:16Hello, friend. But, there's another way.
- 42:19If you insist on using double quotes, as
- 42:22you might want to just be consistent,
- 42:24you can also use that backslash
- 42:26character again. We saw the backslash n
- 42:29a moment ago, and that meant we don't
- 42:31want a literal n to be in the output. We
- 42:33wanted a new line. So, the backslash
- 42:35actually represents what's called an
- 42:36escape character. An escape character is
- 42:39one that you can't just type necessarily
- 42:41once on your keyboard. You need to
- 42:43express it with multiple characters. So,
- 42:45I can actually put backslashes in front
- 42:47of these inner
- 42:49double quotes, so that the computer
- 42:51realizes, "Oh, wait a minute. Those
- 42:52aren't literal Those aren't quotes that
- 42:54finish or start the thought. They're
- 42:56literal quotes." So, now let me go back
- 42:58to my terminal window, run python of
- 43:00hello.py, enter, and now it's working as
- 43:03well. So, escaping is a general
- 43:05technique that allows us to do that,
- 43:07too.
- 43:08And if I may, let me rewind now on these
- 43:11examples and go back to where we left
- 43:13off with my code. I'm just undoing all
- 43:15of that because I want to get back to
- 43:17the point, ultimately, of specifying now
- 43:20a a final way of solving this problem.
- 43:23Well, it turns out that we have yet
- 43:25another way we can solve this problem,
- 43:27which is perhaps the most frequently
- 43:30done now, or at least the most elegant
- 43:33when it comes to setting us up for
- 43:35longer and longer uh uses of strings.
- 43:38You can use a relatively new feature of
- 43:41Python that allows you to do this. You
- 43:43can literally put not the name of the
- 43:45variable like that in your string,
- 43:47because we already saw this is wrong,
- 43:48right? If you do this, you will
- 43:50literally see hello {comma} name. But,
- 43:52what if I do this? What if I put curly
- 43:54braces or curly brackets around the
- 43:57variable's name? Notice VS Code is
- 43:58actually very subtly changing the color
- 44:00of it. So, VS Code knows something
- 44:01interesting is going on here. Let me run
- 44:04this program, but I'm I'm done yet.
- 44:06Python of hello.py, enter. d a v i d,
- 44:08enter. Okay, obviously not what I want,
- 44:11but I need to tell Python that this is a
- 44:13special string. This is what we're going
- 44:15to call a format string or an f string,
- 44:18a relatively new feature of Python in
- 44:20the past few years that tells Python to
- 44:23actually format stuff in the string in a
- 44:25special way. And the symbol by which you
- 44:28do this, this is a little weird, but
- 44:29this is what the world chose, if you put
- 44:31a f at the beginning of the string right
- 44:35before the first quote mark, that's a
- 44:37clue to Python that ooh, this is a
- 44:40special string. Let me format this in a
- 44:42special way for you. Let me now rerun
- 44:44the program. Python hello.py, enter. d a
- 44:47v i d, enter. And now we see the goal
- 44:50this whole time, hello, {comma} David.
- 44:52We don't start with this way because I
- 44:54think if we did this the first way,
- 44:55you'd be like, why are we doing this?
- 44:57What are all these magical symbols? But
- 44:59this is just yet another way to solve
- 45:01the same problem. But let me propose
- 45:03that we consider now yet other things we
- 45:05can do with strings. And it turns out
- 45:07that even as we've been doing
- 45:09some relatively simple operations here,
- 45:11we've generally been trusting that the
- 45:13user's going to cooperate. And that is
- 45:15to say that they're going to actually
- 45:16type in what we want them to type. Now,
- 45:18just because they type a string though,
- 45:20doesn't mean it's going to look the way
- 45:21we want. You and I honestly as humans
- 45:23are actually in the habit on websites
- 45:25and apps of like accidentally hitting
- 45:27the space bar a lot, either at the
- 45:29beginning of our input or at the end,
- 45:31maybe because the space bar tends to be
- 45:32so big. It's pretty common to get
- 45:34accidental spaces before or after some
- 45:37user's input. You and I are definitely
- 45:39in the habit of not necessarily
- 45:40capitalizing words like we should. If
- 45:42we're sending text messages, we're
- 45:43probably being a little quick and just
- 45:45sending everything in lower case, for
- 45:46instance, if that's your style, if your
- 45:48phone's not fixing it for you. Maybe in
- 45:50a formal letter you would capitalize
- 45:51things properly, but you and I as
- 45:53humans, we can't really be trusted to
- 45:54type things in a nice way necessarily
- 45:56when using some piece of software, be it
- 45:58an app or a website or something else.
- 46:00But it turns out that strings themselves
- 46:04come with a lot of built-in
- 46:05functionality, and you can see all of
- 46:07that in Python's own documentation here.
- 46:09The string data type that we've been
- 46:11talking about comes with a lot of
- 46:13functionality built in. That means that
- 46:15we can manipulate the user's input to do
- 46:17more than just join it with something
- 46:19else like hello. We can actually clean
- 46:21it up or reformat it in a way that
- 46:24hopefully looks a little better for us.
- 46:26So, let me go back to my code here, and
- 46:28let me just demonstrate what might
- 46:30happen if a user doesn't cooperate. If I
- 46:32go ahead here and run python of
- 46:33hello.py, enter, let me just sloppily
- 46:36hit the spacebar a few too many times.
- 46:38Why? I just wasn't paying attention. And
- 46:41now I'm going to type in my name d a v i
- 46:42d, and I don't know. I hit the spacebar
- 46:44a couple more times. Like it's kind of a
- 46:46mess. It's all lowercase. That's not
- 46:48going to necessarily look grammatically
- 46:49right. It's got spaces here and here.
- 46:51The program is going to print exactly
- 46:53that, and that looks really bad. At
- 46:55least if we're prioritizing aesthetics
- 46:57and grammar, like why are there so many
- 46:59spaces after the comma? This is not a
- 47:00very nice way to greet your users. But,
- 47:03we can clean this up. It turns out that
- 47:05built into strings, which again is this
- 47:09data type, so to speak, this type of
- 47:11data in Python, is the ability to
- 47:13actually do things to that string. So,
- 47:15let me do this. I can actually go ahead
- 47:18and do something like this.
- 47:19Uh name equals name.strip.
- 47:23And what does this do? Remove white
- 47:26space from string.
- 47:30And what do I mean by this? Well, on the
- 47:32right-hand side, notice I've written the
- 47:34variable name called name.
- 47:36I've then used a period or a dot, and
- 47:39then I seem to be doing what's a
- 47:41function, right? Anytime we've seen this
- 47:43function thus far, we see it's the the
- 47:45function's name, print or input, then we
- 47:47see a parenthesis, then another
- 47:48parenthesis, and that's exactly what I
- 47:49see here. But, I'm using this function a
- 47:51little differently. Technically, this
- 47:53function is in this context called a
- 47:55method. And what do I mean by that?
- 47:57Well, if name is a string, aka stir,
- 48:01well, it turns out according to the
- 48:02documentation, there's a lot of
- 48:03functions that come with strings in
- 48:07Python. And you can access that
- 48:08functionality by using the name of a
- 48:10string, like literally name here, then a
- 48:13period, then the name of the function,
- 48:15and then an open parenthesis and a close
- 48:17parenthesis. Maybe some arguments inside
- 48:19of those parentheses, but in this case,
- 48:21it doesn't need any arguments. I just
- 48:23want to strip the space from the left
- 48:25and the space from the right of the
- 48:27user's input. But, that's not enough. I
- 48:29want to remember that I've stripped off
- 48:31that white space on the left and the
- 48:32right. So, I'm going to use the equal
- 48:34sign again here. And notice that just as
- 48:36before, this doesn't mean equality, this
- 48:38means assignment from right to left. So,
- 48:41when this line of code here, name.strip,
- 48:44returns to me,
- 48:45aka a return value, it will return the
- 48:48same thing that the user typed in, but
- 48:50with no more white space to the left or
- 48:51to the white
- 48:53to the right. So, then the equal sign
- 48:55assignment is going to copy that value
- 48:57from the right to the left, thereby
- 49:00updating the value inside of my name
- 49:03variable. So, you can not only assign
- 49:06values to variables, you can absolutely
- 49:08change the value of variables by just
- 49:10using the assignment operator, the equal
- 49:12sign again and again and again. And it
- 49:14will just keep copying from right to
- 49:15left whatever the new value should be.
- 49:17So, now, if I rerun this program, python
- 49:20of hello.py,
- 49:22enter,
- 49:23I have d a v i Whoop, let's do it again.
- 49:25Space space space space space d a v i d
- 49:28in all lower case, space space, enter,
- 49:30it's better. It hasn't fixed my
- 49:32capitalization, so I'm still being a
- 49:34little sloppy with the first d, but it
- 49:36has stripped off all of that extra
- 49:38space. Super minor detail, right? Like,
- 49:41this isn't all that exciting, but it
- 49:43just speaks to the power of what you can
- 49:45do with with just a single line of code.
- 49:47Now, what else can I do here? Well, I
- 49:49could capitalize the user's input. Let
- 49:51me go ahead and try this. It turns out
- 49:54that I could also
- 49:55uh do this. name.capitalize.
- 49:59So, let me go ahead and capitalize
- 50:02uh user's name. And again, I'm making
- 50:04comments, and there's no one right way
- 50:05to write the comments. I'm just using
- 50:07some short English phrases here to
- 50:08remind myself of what I'm doing. What's
- 50:10now going on here? Well, let me go ahead
- 50:12and run python of hello.py. Enter. Uh
- 50:15space space space space space d a v i d
- 50:17space space enter.
- 50:19Okay. Now it's looking prettier, right?
- 50:21No matter how the user typed in their
- 50:22name, even a little sloppily, I'm now
- 50:25fixing that. But, let's let's try
- 50:27something. I'm getting a little curious
- 50:28here. How about this? Uh space space
- 50:30space space space d a v i d space
- 50:33mailin. I'll use my last name now.
- 50:35Enter.
- 50:36Okay. So, ironically, capitalize is not
- 50:39really capitalizing everything we want.
- 50:42It's clearly capitalizing what? Just the
- 50:44very first letter. So, it turns out that
- 50:46again, there's other functions in Python
- 50:48that come with strings. And if we poke
- 50:50around the documentation, scrolling
- 50:52through a URL like that, I bet we'll
- 50:55find another solution, one of which is
- 50:57actually this.
- 50:58Let's actually change this to title.
- 51:01There's yet another function that come
- 51:03with strings called title that do
- 51:05title-based capitalization, just like a
- 51:07book or a person's name, capitalizing
- 51:09the first letter of each word. And this
- 51:12is just going to do a little more work
- 51:13for us. So, let's go ahead and run this.
- 51:15And as an aside, I'm kind of tired now
- 51:17at this point of typing python python
- 51:20python all the time. It turns out that
- 51:22when using a command line interface like
- 51:25this, you can actually go back through
- 51:26all of your old commands. What I just
- 51:28did a moment ago was I hit the up arrow.
- 51:30That immediately goes back through my
- 51:32history of all of the commands I've ever
- 51:34typed. So, this is just a faster way now
- 51:36for me to repeat myself than typing
- 51:38everything manually. Let me go ahead and
- 51:40hit enter. Uh space space space space
- 51:42space d a v i d mailin space space all
- 51:45lower case. Enter. Now it's it's looking
- 51:49better. Now I've capitalized things and
- 51:51cleaned things up. But what about my
- 51:52code? I've got like eight lines of code
- 51:54now, four of which are comments, four of
- 51:56which are actual code. Do I really need
- 51:58this much? Well, not necessarily. Watch
- 52:01what I can also do in Python. Let me not
- 52:04bother capitalizing the user's name
- 52:05separately. Let me say this, and capital
- 52:10capitalize user's name. I can chain
- 52:13these functions together. I can add
- 52:15title to the end of this. And now what's
- 52:18happening? Well, again, with a line of
- 52:20code like this, you first focus on
- 52:22what's to the right of the equal sign.
- 52:24Then we'll get to the left of the equal
- 52:26sign. What's on the right of the equal
- 52:27sign? This line here. Well, what does
- 52:29this mean? Get the value of the name
- 52:32variable, like d a v i d space m a l a
- 52:34n,
- 52:35then strip off the white space on the
- 52:37left and the right. That is going to
- 52:39return a value. It's going to return d a
- 52:41v i d space m a l a n without any white
- 52:44space to the left or right. What do you
- 52:46want to do with that return value? You
- 52:48want Python to title case it. That is go
- 52:50through every word in that resulting
- 52:53string and fix the first letter of the
- 52:55first word, the first letter of the
- 52:57second word, and so forth. And then, now
- 52:59we can finish our thought, copy the
- 53:00whole thing
- 53:02from right to left into that same name
- 53:05variable. And you know what? I can take
- 53:06this even one step further. Why don't we
- 53:09go ahead and do this if we want? Let me
- 53:11get rid of all that, and let me just do
- 53:14strip and title all on that first line.
- 53:18And now we've gone from like eight lines
- 53:19of code to four. It's a lot tighter,
- 53:22it's a lot neater, and even though
- 53:24reasonable people might disagree, it's
- 53:26arguably better
- 53:28because it's just easier to read. Fewer
- 53:30lines of code, fewer opportunities for
- 53:31mistakes. It just allows me to move on
- 53:34with my next problem to solve.
- 53:37All right, let me pause here and see if
- 53:38there's any questions on these methods.
- 53:41A method is a function that's built in
- 53:44to a a type of value like these
- 53:46functions are
- 53:47or on F strings, which we saw a moment
- 53:49ago.
- 53:50Yes, hi. Thanks, David. Um so, is there
- 53:52a way to remove the spaces between
- 53:55the spaces that I might have added?
- 53:57Short answer, no. If you read the
- 53:59documentation at that same URL earlier,
- 54:00you'll see that strip removes from the
- 54:02left and the right, but not in between.
- 54:04In fact, there's two other functions
- 54:06that come with strings. One's called L
- 54:08strip, the other's called R strip that
- 54:10allow you to do one or the other. If we
- 54:12want to start getting rid of space in
- 54:14the middle, we're going to have to do a
- 54:15different trick altogether.
- 54:17How many functions can we combine like
- 54:20this? {dot} strip {dot} title you have
- 54:22combined. So, how many we can combine?
- 54:24Yeah, a really good question.
- 54:27Technically, as many as you want, but at
- 54:29some point your code's going to start to
- 54:31look really, really bad, right? Because
- 54:32the line of code's going to get really,
- 54:34really long. It's eventually going to
- 54:35maybe wrap around again and again. So,
- 54:38at some point you just kind of say like,
- 54:40"Uh-uh, that's too many." and you start
- 54:41breaking it up into multiple lines like
- 54:43I did, maybe reassigning the value to
- 54:46the variable as needed. And this is
- 54:48actually a good question. If I can pivot
- 54:49Divya off your question, I mean, what do
- 54:52people think? If we could go ahead and
- 54:53put everyone's hands down for a moment,
- 54:55let me ask this. Is the way I've done
- 54:59this now with strip and title and input
- 55:03all in the same line better than my
- 55:06previous approach? In Zoom, you can use
- 55:08the yes icon or the no icon. If you
- 55:11think this version is better, say yes.
- 55:14If you think this previous version was
- 55:16better,
- 55:18for instance, this one here, where we
- 55:20had everything broken out, say no.
- 55:23And then we'll see why in just a moment.
- 55:26I proposed earlier that reasonable
- 55:28people can disagree, and that's
- 55:30absolutely the case.
- 55:32Doing it one way or the other isn't
- 55:34necessarily best, at least if you can
- 55:36justify it. Let me go back to the most
- 55:38recent version here.
- 55:41All right. So, we're seeing a lot of
- 55:43yeses and a lot of no's. Why don't we go
- 55:45ahead and and call on one of the yeses
- 55:47if we could? Someone who's voting yes,
- 55:50why do you think the current version of
- 55:52this code is indeed better than the
- 55:54previous longer version of the code?
- 55:57Uh I think it's more readable, so I can
- 55:59say, "Hey, this is the name fun This is
- 56:01the name variable.
- 56:03It gets some input and then remove the
- 56:05space and give it a title and there you
- 56:07go. You have a hello name."
- 56:09Yeah, I think that's pretty reasonable.
- 56:10It's very readable, at least if you're
- 56:12in the habit, as you are in English, of
- 56:13reading left to right. It just kind of
- 56:15flows very naturally as a result. The
- 56:17line is not really that long. It's
- 56:19certainly fitting nicely onto the
- 56:20screen, so I think that's a good
- 56:21argument. How about a counterpoint
- 56:22though? Someone who voted no, if we
- 56:24could call on someone who thinks this is
- 56:26worse.
- 56:27Um because it's not uh readable at all.
- 56:31I seems like
- 56:32uh
- 56:33uh
- 56:34It's a very long line, so I think it's
- 56:37better to separate.
- 56:39Yeah, I I think that's persuasive, too,
- 56:41right? It's getting a little longer and
- 56:43even though my sentence here, "What's
- 56:45your name?" is relatively short, you
- 56:47could imagine that this could get even
- 56:48uglier quickly. If I were asking a
- 56:50longer question of the user, that's
- 56:52going to make this line of code even
- 56:53longer and therefore less readable. It
- 56:56might be less obvious to me or my
- 56:57colleagues that I am calling strip or
- 57:00that I am calling title. It might be
- 57:01kind of a unexpected surprise. So, I
- 57:04think that's reasonable, too. In short,
- 57:05there is no right answer here and in
- 57:07fact, part of the process of getting
- 57:09better at programming is getting your
- 57:11own sense of style or working for a
- 57:13company where they might prescribe which
- 57:15way is better than the other because
- 57:16they just want everyone doing the same
- 57:18thing, even though reasonable people
- 57:20might uh
- 57:21disagree. Ultimately though, so long as
- 57:23you have what's a pretty good argument
- 57:25in favor of one way or the other. Like
- 57:28ultimately, that's what's important. If
- 57:29you're just doing things cuz you don't
- 57:30really know which one's better, that's
- 57:32not great. But if if and when you start
- 57:34to acquire opinions and if your boss, if
- 57:36your teacher, if your colleague, your
- 57:38friend can challenge you and say, "Wait,
- 57:39why did you do it like this?" They might
- 57:41not agree with you, but at least have an
- 57:43answer and that should be sufficiently
- 57:45persuasive in general. Now, strings come
- 57:47with a whole bunch of other methods as
- 57:49well, among which is one called split,
- 57:51which can, as the name suggests, split a
- 57:53string into multiple smaller substrings,
- 57:56so to speak. For instance, if the human
- 57:58here is in the habit of typing in their
- 57:59first name, then a space, and then their
- 58:01last name, and you want to go ahead and
- 58:03greet them only by first name, well, we
- 58:05could actually leverage that single
- 58:06space between the first name and last
- 58:08name and split that string into two
- 58:10smaller substrings. How can we do this?
- 58:12Well, let me go ahead and in between
- 58:14these lines proactively comment that
- 58:15we're about to split user's name into
- 58:19first name and last name, and then let's
- 58:23go ahead and take that name variable,
- 58:24which currently contains something like,
- 58:26presumably, David space Maylan, and let
- 58:28me go ahead and call split and pass in
- 58:31as the argument to split a single white
- 58:33space, thereby indicating that I indeed
- 58:35want to split on that character. Now, it
- 58:37turns out split's going to return a
- 58:39sequence of values, ideally a first name
- 58:41and then a last name, and we can
- 58:43actually in Python assign both of those
- 58:44values from that sequence at once to
- 58:47some variables. For instance, first
- 58:49{comma} last equals, and that's going to
- 58:51have the effect from right to left of
- 58:53putting the first such value in the
- 58:54first variable, the second such value in
- 58:56the second variable. So, now on my last
- 58:59line of code, I can go in and say hello
- 59:01not to the full name, something like
- 59:03David Maylan, I can just say hello
- 59:05{comma} first. All right, let's go ahead
- 59:07and clear my terminal window, run Python
- 59:09of hello.py and hit enter. I won't
- 59:11bother with any leading white space this
- 59:12time, but let me go ahead and type in
- 59:14David space Maylan, and crossing my
- 59:17fingers as usual, hello David is what we
- 59:20now see.
- 59:21All right. So, we've seen so much so
- 59:23many examples thus far involving
- 59:25strings, but certainly programs uh and
- 59:28programming languages can manipulate
- 59:30other types of data as well. Let's go
- 59:32ahead and transition then to another
- 59:34very common type of data in Python and
- 59:36programming more generally, namely
- 59:38integers, otherwise known in Python as
- 59:40int, i n t. So, just as str, s t r is
- 59:43short for string, so is int in Python
- 59:46short for integer. Well, what's an
- 59:47integer? Well, just like in math, it's a
- 59:50number like -2, -1, 0, 1, 2 and all the
- 59:54way toward negative infinity, all the
- 59:55way toward positive infinity, but
- 59:56there's no decimal point in an integer.
- 59:59It's just a number like -2, -1, 0, 1,
- 1:00:02and 2 onward. That's an int. Of course,
- 1:00:05in the world of mathematics, there's
- 1:00:08lots of symbols that we use and we've
- 1:00:09seen plus before, although we used it
- 1:00:11for a different purpose, but Python
- 1:00:13supports these symbols and more. And
- 1:00:15Python allows you to add numbers
- 1:00:17together, plus, subtract numbers,
- 1:00:19multiply numbers, divide numbers, and
- 1:00:21the only one here that might look a
- 1:00:22little strange to people or unfamiliar
- 1:00:25is this percent sign, but it doesn't
- 1:00:27mean percent in this context. If you use
- 1:00:29a single percent sign in a Python
- 1:00:31program, that's actually the so-called
- 1:00:33modulo operator, the operator that
- 1:00:35allows you to take the remainder after
- 1:00:37dividing one number by another. So,
- 1:00:40we'll see examples of that before long,
- 1:00:41but the first four of these are perhaps
- 1:00:43quite quite familiar. Well, it turns out
- 1:00:46that in Python, you cannot necessarily
- 1:00:49you don't necessarily have to keep
- 1:00:50writing code in a file like hello.py and
- 1:00:54then running it in a terminal window.
- 1:00:56One of the features that many people
- 1:00:58like about Python is that it supports
- 1:01:00this so-called interactive mode. Like,
- 1:01:02you can start writing Python code and
- 1:01:04immediately execute each of those lines
- 1:01:07interactively. If especially if you
- 1:01:09don't care about saving all of your
- 1:01:10lines of code, you just want to execute
- 1:01:12code and get back some answers. So, for
- 1:01:14instance, let me go back to VS Code here
- 1:01:16and let me close hello.py and let me
- 1:01:18click on the little triangle over here
- 1:01:20in my terminal window just to make it
- 1:01:22much bigger just temporarily for a
- 1:01:24moment. So, I'm not creating any dot py
- 1:01:26file now. I'm just going to run Python
- 1:01:29by itself at my prompt. And you'll see
- 1:01:32when I do this, I get some cryptic
- 1:01:34looking output and the date and time at
- 1:01:36which the program was last updated and
- 1:01:38so forth, but I ultimately get three
- 1:01:40triple uh brackets like this. This is
- 1:01:43the interactive mode for Python. So, I'm
- 1:01:47running the Python interpreter and
- 1:01:49anytime I type
- 1:01:51uh a a line of code in the interpreter,
- 1:01:52it's going to execute it immediately. I
- 1:01:54don't have to keep running Python again
- 1:01:56and again. It's as though in the human
- 1:01:57world, if you were standing next to a
- 1:01:59human who speaks some other language and
- 1:02:01you're just having a conversation with
- 1:02:03them back and forth, it's all happening
- 1:02:05the translation immediately. So, what
- 1:02:07might I do in interactive mode? Well, I
- 1:02:09could do something like 1 + 1, enter.
- 1:02:11That's actually code. All right, you
- 1:02:13might not think of it as code, but if
- 1:02:15you know a bit of arithmetic and you
- 1:02:16know uh numbers and you know plus,
- 1:02:19that's valid Python code. And you can
- 1:02:21use Python really as a fancy calculator.
- 1:02:23But I could do other things too. If I
- 1:02:24want to print to myself, "Hello, {comma}
- 1:02:27world." I can also print out that line
- 1:02:29of code there too. Hello, world. So,
- 1:02:31it's interactive in the sense that the
- 1:02:33moment you execute a line of code, boom,
- 1:02:35you see the result. We're generally not
- 1:02:37going to do that because at least when
- 1:02:39teaching the language, we tend to want
- 1:02:41to do things incrementally and we want
- 1:02:43you to be able to see where it is we
- 1:02:45came from and we want to be able to try
- 1:02:46things again and again, especially if we
- 1:02:48make mistakes. But know that this is
- 1:02:50indeed a feature of Python, this
- 1:02:51so-called interactive mode. But let's
- 1:02:54focus for a moment now not just on that
- 1:02:56interactivity, but really on the fact
- 1:02:57that Python apparently supports integers
- 1:03:00and mathematics and some of those basic
- 1:03:02operations. And let's see if we can't
- 1:03:03make maybe our our own little
- 1:03:05calculator. So, let me go ahead and open
- 1:03:07up VS Code again and I'm going to shrink
- 1:03:09down my terminal window and I'm going to
- 1:03:11create a new file called calculator.py.
- 1:03:14So, to do that recall, I can type code
- 1:03:16down here and the name of the file I
- 1:03:19want to create {dot} py. Enter. That
- 1:03:21gives me a new tab up top, so I have
- 1:03:24already closed hello.py. I'm now in
- 1:03:25calculator.py and let's just make a
- 1:03:27simple calculator that does some
- 1:03:29addition for me. But, I'm going to do it
- 1:03:31in a file so that we can iterate on this
- 1:03:33and make changes for better or for worse
- 1:03:35over time. Let me go ahead and first
- 1:03:37declare a couple of variables. I'm going
- 1:03:39to do the mathematical thing of calling
- 1:03:41my first variable X,
- 1:03:42my second variable Y, and then I'm going
- 1:03:45to give myself a third variable Z equals
- 1:03:47X plus Y, and then I'm going to go ahead
- 1:03:49and print out Z. Now, this program,
- 1:03:52admittedly, not very exciting or
- 1:03:54interesting. In fact, it's a little less
- 1:03:56interesting than printing stuff on the
- 1:03:57screen like before with strings, but
- 1:04:00we'll build on this and see what other
- 1:04:01features exist in Python that we can
- 1:04:03leverage. So, hopefully, if Python knows
- 1:04:05its math as well as I do, when I run
- 1:04:07Python of calculator.py,
- 1:04:10I should see, hopefully, that 1 + 2
- 1:04:13equals
- 1:04:14indeed three. All right. So, not that
- 1:04:16surprising and not that interesting. And
- 1:04:18honestly, this isn't the most useful
- 1:04:20program because it's always going to
- 1:04:21calculate 1 + 2 = 3. Let's at least make
- 1:04:25this program say a little more
- 1:04:27interactive, right? We already know from
- 1:04:28previous examples how we can get input
- 1:04:30from the user. Let's bring back that
- 1:04:32input function. And let's do this. Let
- 1:04:34me go ahead now and at the top of my
- 1:04:36code, let's change X to not be the
- 1:04:39number one always. Let's change it to be
- 1:04:41whatever the return value is of asking
- 1:04:43the user for X. And I can use any
- 1:04:46English or human language I want here.
- 1:04:48I'm going to say, "What's X?" Just like
- 1:04:50I asked before, "What's your name?" And
- 1:04:52I'm going to do the same thing for Y.
- 1:04:54I'm going to use input again, but this
- 1:04:56time change the question to be, "What's
- 1:04:57Y?"
- 1:04:58All right. At this point, I think I'm
- 1:05:00going to leave the rest of the code the
- 1:05:01same. Z equals X plus Y, and then print
- 1:05:04Z. But, what's nice now is that I think
- 1:05:07I have a nice interactive calculator.
- 1:05:10Right now, it's not going to do 1 + 2
- 1:05:11all the time. It's going to do whatever
- 1:05:13the user types plus whatever the user
- 1:05:15types. So, let's try this. Let me go
- 1:05:17ahead and run the program. All right,
- 1:05:19let's do it. 1 is going to be X, 2 is
- 1:05:22going to be Y, and of course, everyone
- 1:05:25in agreement, 1 + 2 equals 3.
- 1:05:31Huh, what's going on there?
- 1:05:35Either your math class misled you or I
- 1:05:38have misled you.
- 1:05:40Why don't we call on someone here to see
- 1:05:42if you can't help us reason through what
- 1:05:44the bug is. What's the mistake? Uh
- 1:05:46Anjali, if I'm saying it right?
- 1:05:48I think the issue is is that it's
- 1:05:51concatenating strings because you use
- 1:05:53the plus operator instead of adding.
- 1:05:55Perfect. So, perfect intuition. We've
- 1:05:57seen that plus is used a little
- 1:05:59differently in the context of strings
- 1:06:00because it concatenates, that is it
- 1:06:02joins the two strings, and that seems to
- 1:06:04indeed be what's happening here even
- 1:06:06though the user typed a number. But, the
- 1:06:08interesting thing here is that when you
- 1:06:10get user input, because they're using a
- 1:06:12keyboard on their Mac or PC or their
- 1:06:14phone, it is always going to be text. It
- 1:06:16might look like a number, but by
- 1:06:18default, it's coming from the keyboard
- 1:06:20as a string. That is, as text. And so,
- 1:06:24how do we go about re- resolving this if
- 1:06:27ultimately we don't want to treat those
- 1:06:28inputs as strings, we want to treat them
- 1:06:31as actual numbers? Well, we need another
- 1:06:32function. And it turns out in Python
- 1:06:34that you can convert sometimes from one
- 1:06:37type of data to another type of data,
- 1:06:39for instance, from string to int by
- 1:06:42doing something like this. Let me go
- 1:06:44back into my code, and let me change X
- 1:06:47before adding it to Y to be whatever the
- 1:06:50integer version of X is plus whatever
- 1:06:53the integer version of Y is. So, it
- 1:06:56turns out that int is not only a type of
- 1:06:58data in Python, it's also a function.
- 1:07:01And it's a function that if you pass in
- 1:07:03an input like a string, so long as that
- 1:07:06string looks like a number like one or
- 1:07:08like two, it will convert it to an
- 1:07:11actual number that you can perform
- 1:07:12mathematics on instead. So if I now go
- 1:07:15back to my terminal window and run
- 1:07:17Python and let me show you another
- 1:07:19trick. Calculator's kind of a long word,
- 1:07:21it's a little tedious to type. Notice
- 1:07:23what I can do in my terminal window and
- 1:07:25a command line interface in general. If
- 1:07:27I start typing c a l for calculator, I
- 1:07:30can actually hit tab to finish my
- 1:07:33thought. So autocomplete is possible in
- 1:07:35a terminal window like this. Type the
- 1:07:36first letter or few letters and then
- 1:07:38boom, with tab it'll finish your thought
- 1:07:40for you. Or you can go back in your
- 1:07:42history like I did with the up and down
- 1:07:44arrows. Let me go ahead and execute
- 1:07:45this. What's x1? What's x2? And there we
- 1:07:49go. Now we have a general purpose
- 1:07:51calculator that's going to support not
- 1:07:53just
- 1:07:54uh addition of one and two, but now any
- 1:07:56two integers that the user types. And
- 1:07:59let me now improve this, right? We've
- 1:08:00seen how we can make improvements to
- 1:08:02code and I I don't know if it's going to
- 1:08:04necessarily be better, but let's try
- 1:08:06this.
- 1:08:07Do I really need the z variable?
- 1:08:09It's worth noting that I'm creating a
- 1:08:11variable called c z and then I'm
- 1:08:14immediately using it on the next line of
- 1:08:16code. Now, that's not that compelling
- 1:08:19because if you're creating a variable
- 1:08:20and then immediately using it but never
- 1:08:22again using it, did you really need to
- 1:08:24take the time to introduce another
- 1:08:26symbol and another variable just to use
- 1:08:28it once and only once? Well, maybe not.
- 1:08:31Maybe we don't really need z in this
- 1:08:33way. Maybe I should go and do something
- 1:08:36uh like this. Maybe I should get rid of
- 1:08:39z here.
- 1:08:41Maybe I should change this to be int up
- 1:08:44here, change this to be int up here.
- 1:08:47Doing something that's pretty
- 1:08:49interesting now. Even though it's a bit
- 1:08:50of new syntax, notice that you can nest
- 1:08:54functions so to speak. You can put one
- 1:08:57function call, that is the use of a
- 1:09:00function, inside of the use of another
- 1:09:02function so that the return value of the
- 1:09:05inner function
- 1:09:07becomes the argument to or the input to
- 1:09:10the outer function. So, just like in
- 1:09:11math, if you have parentheses,
- 1:09:13parentheses, parentheses, your teacher
- 1:09:14probably taught you to focus on what's
- 1:09:16inside the innermost parentheses first
- 1:09:18and then work your way out. Same thing
- 1:09:20with programming. That's what Python's
- 1:09:21going to do. It's going to look at
- 1:09:22what's inside of the parentheses first,
- 1:09:24it's going to get the answer, and then
- 1:09:26it's going to pass the return value to
- 1:09:29the outermost function. So, what happens
- 1:09:31on line one now is that the input
- 1:09:32function gets called first, then the
- 1:09:35result of that, quote unquote one,
- 1:09:38becomes the input to the int function,
- 1:09:41and same on line two. The output of
- 1:09:43what's Y becomes the input to this int
- 1:09:46function. And now, there is no Z. I
- 1:09:49could just do print X + Y. And because
- 1:09:52I've taken the time to convert each of
- 1:09:55those strings to an integer, I think
- 1:09:58we're okay. So, let me try this. Python
- 1:10:00calculator.py, enter. One and two, and
- 1:10:03we're still getting three. Not 12 or not
- 1:10:061212,
- 1:10:08we're indeed getting three. And we've
- 1:10:09additionally gotten rid of the variable
- 1:10:11because we didn't necessarily need it,
- 1:10:13it seems, especially if only using it
- 1:10:14once. Well, here too, let me put
- 1:10:16everyone's hands down for just a moment
- 1:10:18and let me ask, as before, this version
- 1:10:20now, which uses int around the
- 1:10:24invocations of input, and does not use
- 1:10:27Z, is this better than the previous
- 1:10:30version? If you want to vote yes, go
- 1:10:31ahead, or if you prefer the old way,
- 1:10:34vote no. The old way, I'll undo all of
- 1:10:36this as we vote, instead looked like
- 1:10:40this.
- 1:10:43All right, and let me go back to now the
- 1:10:44newest version. Let's take a hand of the
- 1:10:46yeses. Someone who thinks this latest
- 1:10:49version is better.
- 1:10:51I think this way is a better because it
- 1:10:54uh allows us to immediately see what the
- 1:10:57X and Y variables are with integers and
- 1:11:02so we know what to expect from them. And
- 1:11:05also the print argument is more
- 1:11:08intuitive. We avoid too much clutter in
- 1:11:11the code.
- 1:11:12I think those are all good reasons. It's
- 1:11:14nice and succinct. The lines of code are
- 1:11:16not very long.
- 1:11:17I don't need to know what Z is cuz it
- 1:11:19doesn't exist. It just see print X plus
- 1:11:21Y. I like that. But someone who prefers
- 1:11:23the older way where we did have Z and we
- 1:11:26more explicitly passed individual
- 1:11:29variables to the int function.
- 1:11:30Yeah, hi.
- 1:11:32I think the earlier version is better
- 1:11:34because when
- 1:11:36I mean if user input something else
- 1:11:38other than in let's say I mean let's say
- 1:11:40data type one and two like
- 1:11:43so will be it will be easier to debug.
- 1:11:46This version or this version here or the
- 1:11:48old version?
- 1:11:50The old version.
- 1:11:50Okay, that's fair. And in fact I'm I'm
- 1:11:52being very careful today as best I can
- 1:11:55not to mess up. I have thus far only
- 1:11:58inputted integers when I'm expecting
- 1:12:00integers and Rose actually pointed to
- 1:12:02something we'll come back to in the
- 1:12:03coming weeks. How do we actually handle
- 1:12:05errors? What if the user doesn't type in
- 1:12:07the number one or the number two or a
- 1:12:09number at all? What if they type in a
- 1:12:10word like cat c a t? That's not a number
- 1:12:14and I bet I can't convert it to an
- 1:12:15integer. But for today I'm not going to
- 1:12:17focus on that. I'm just going to hope
- 1:12:18that the user cooperates but that's not
- 1:12:20going to be the case and so perhaps one
- 1:12:22way would set us up for more success
- 1:12:24when it comes to handling those errors.
- 1:12:26Now for today's purposes which is
- 1:12:27better?
- 1:12:28I mean I like both and I think both of
- 1:12:30you made very valid arguments in there
- 1:12:32too. So long as you have a justification
- 1:12:34that feels pretty reasonable. I mean
- 1:12:36that's what ultimately matters. But
- 1:12:38acquiring again a sense of the
- 1:12:40trade-offs here. Well, is this way
- 1:12:42better? If so, why or why not? Just
- 1:12:45understanding what those trade-offs are.
- 1:12:46But generally speaking prioritizing
- 1:12:49readability is a very good thing. Making
- 1:12:51your code readable for someone else is a
- 1:12:53very good thing and very good for you,
- 1:12:56too. So, that when you wake up the next
- 1:12:57morning or you come back the next week
- 1:12:58or the next year, you too can read your
- 1:13:00own code without having to waste time
- 1:13:02trying to remember what you did. And
- 1:13:04simplicity tends to be a good thing,
- 1:13:06too. Keeping your code simple. So, is as
- 1:13:09you get more comfortable with
- 1:13:10programming, you might be tempted to try
- 1:13:11to like combine an entire program into
- 1:13:14one long line. For instance, let me do
- 1:13:16right just that. Don't Technically
- 1:13:18speaking, we don't really need X in a
- 1:13:19variable. We don't really need Y in a
- 1:13:21variable. We could also do this. I could
- 1:13:24just get rid of X and Y altogether. I
- 1:13:28could then now eliminate that and make
- 1:13:30it just one line of code. Okay, so on
- 1:13:32some sense you might be inclined to
- 1:13:33think, "Wow, that's really nice. You
- 1:13:35made it one simple line of code." I
- 1:13:38would argue this actually isn't that
- 1:13:39simple. Now, I think I'm starting to
- 1:13:41nest too many things. I have to think
- 1:13:43about print and int and input. I then
- 1:13:46have to notice that, "Okay, I've opened
- 1:13:48two parentheses. I've closed two of
- 1:13:49them. There's a plus." You're making me
- 1:13:51think too much. And anytime you make me
- 1:13:53think, you're wasting time. And anytime
- 1:13:55you complicate the look of the code like
- 1:13:57this, you're just going to increase the
- 1:13:59probability of mistakes, syntactical
- 1:14:01mistakes, or logical errors in your
- 1:14:03code. So, of all the things we've done,
- 1:14:05this is the only one that I would argue,
- 1:14:07"Yes, it's one line and it's nice and
- 1:14:10compact. It's just not readable enough."
- 1:14:12I would shy away from doing this,
- 1:14:13especially since two of those function
- 1:14:15calls are getting input from the user.
- 1:14:17But there, too, reasonable people might
- 1:14:19disagree. But that's the kind of like
- 1:14:21visceral reaction you should have
- 1:14:22sometimes when code starts getting a
- 1:14:24little too complicated, a little too
- 1:14:27clever perhaps for its own good.
- 1:14:31All right. Well, it's not just integers
- 1:14:33we have access to. Let me propose that
- 1:14:35we transition from integers to one more
- 1:14:38data type here, namely a float. So,
- 1:14:41again, a string is a sequence of text.
- 1:14:43An int is an integer like -1, 0, and 1.
- 1:14:46A float is a number with a decimal
- 1:14:48point, properly called a floating point
- 1:14:50value, and you can think of the floating
- 1:14:52point as being the decimal that might be
- 1:14:54over here or over here with some number
- 1:14:56of digits to the left or the right.
- 1:14:57Mathematically, it's a real number, a
- 1:14:59number that has a decimal point in it.
- 1:15:01So, that's a third type of data that
- 1:15:03Python supports. Right now, our
- 1:15:05calculator is somewhat naively assuming
- 1:15:07that the user's only going to type in
- 1:15:08integers, but if I want to support
- 1:15:10floating point values, too, I think I
- 1:15:13can just make a couple of tweaks. So,
- 1:15:15I'm going to go back to VS Code here,
- 1:15:17and instead of just converting the
- 1:15:18user's input X and Y to integers on line
- 1:15:21one and two, let's just make a simple
- 1:15:23change. Let's actually convert it to a
- 1:15:25float on the first line and a float on
- 1:15:29the second line here. Now, I think if I
- 1:15:31go down to my terminal window and run
- 1:15:33python of calculator.py, let's type in a
- 1:15:36number like 1.2 with a decimal point and
- 1:15:393.4 with a decimal point, and there we
- 1:15:41go. We have 4.6 as the final answer. So,
- 1:15:44that wouldn't have worked before if I
- 1:15:46was only expecting integers from the
- 1:15:48user, but now that I'm support expecting
- 1:15:50floating point values and accommodating
- 1:15:52it, I can actually now do floating point
- 1:15:54arithmetic as well. But, suppose that I
- 1:15:56don't really want the final answer to be
- 1:16:00a floating point value like 4.6. I would
- 1:16:03be happy if we just round to the nearest
- 1:16:05integer. So, I want to support the user
- 1:16:07typing in floating point values with
- 1:16:09decimal points, but at the end of the
- 1:16:10day, I just want to round the result to
- 1:16:12the nearest possible integer for
- 1:16:15instance. Well, it turns out that here,
- 1:16:17too, Python comes with some
- 1:16:19functionality built in. And in fact, if
- 1:16:21we return to this URL from earlier,
- 1:16:23wherein all of the Python built-in
- 1:16:25functions are listed, there's one called
- 1:16:27round, which does exactly as we would
- 1:16:29expect. It takes as input a number and
- 1:16:31then rounds it for us, for instance, to
- 1:16:34the nearest digit to the nearest
- 1:16:35integer.
- 1:16:36But, if we look a little closer at that
- 1:16:38documentation, as we can here, I'll
- 1:16:40provide an excerpt, this is what the
- 1:16:42function looks like in the
- 1:16:44documentation. And recall that earlier
- 1:16:46we looked at the documentation for
- 1:16:47print, and this is similar in spirit
- 1:16:50that this shows us not just the name of
- 1:16:51the function, but it's available
- 1:16:53parameters. That is inputs that we can
- 1:16:55provide when using this function. But
- 1:16:57this is a little cryptic too, just like
- 1:16:59print was, and it adds some syntax. So
- 1:17:01let's see. The name of this function
- 1:17:03here is of course round, and its first
- 1:17:05argument is a number. Notice this times
- 1:17:08there's no star. There's no star objects
- 1:17:10like there was for print. The round
- 1:17:12function takes just one number as its
- 1:17:14first argument, period. That's its
- 1:17:16positional parameter. But notice this
- 1:17:19syntax, and this is a convention in
- 1:17:21programming or technology more
- 1:17:22generally. Generally speaking, when you
- 1:17:24see square brackets in documentation
- 1:17:27like this, this means that you're about
- 1:17:28to see something optional. And so what
- 1:17:30this means is that if you want to
- 1:17:32specify more precisely the number of
- 1:17:35digits that you want the round function
- 1:17:37to round to, you can specify it here by
- 1:17:41adding a comma and then that number. So
- 1:17:43if we read the documentation, if you
- 1:17:45don't specify a number of digits, you
- 1:17:48just specify the number to round, it
- 1:17:50rounds to the nearest integer. But
- 1:17:52suppose you want to round to the tenths
- 1:17:54place or the hundredths place, that is
- 1:17:56one or two digits after the decimal
- 1:17:57point, you could additionally pass in
- 1:17:59{comma} one or {comma} two to be more
- 1:18:03precise. So that's what the
- 1:18:04documentation there is saying. Let's see
- 1:18:06if we can't then translate this to
- 1:18:08some actual code for us. So if I go back
- 1:18:11now to VS Code, and I consider that I
- 1:18:13want to go ahead and round X and Y, I
- 1:18:16can do this in a couple of ways. I could
- 1:18:18do round X + Y, but you know, I'd
- 1:18:22actually kind of prefer to break this
- 1:18:23now out into two lines. I don't have to,
- 1:18:26and reasonable people here might
- 1:18:27disagree, but I'd like to revert to a
- 1:18:29scenario where I'm printing Z so that I
- 1:18:31can just a little more clearly to
- 1:18:33myself, to others say Z equals the
- 1:18:35rounded result of X plus Y. It's not
- 1:18:39necessarily the better way to do it, but
- 1:18:41I'm a little more comfortable with
- 1:18:42breaking out my thoughts one at a time,
- 1:18:44especially if I want to start commenting
- 1:18:46each of these chunks of code. All right,
- 1:18:48let me go down to my terminal window now
- 1:18:50and run python of calculator.py. What's
- 1:18:52X? Let's do 1.2 again. Then let's do
- 1:18:553.4. And now it was previously 4.6, but
- 1:18:58now it's been rounded up to the nearest
- 1:19:01integer, which of course is going to be
- 1:19:03five. All right, what if I wanted to
- 1:19:06change this a little further? What if I
- 1:19:09wanted to support maybe really big
- 1:19:11numbers? Big numbers irrespective of
- 1:19:13rounding. Let's just do something like
- 1:19:14this.
- 1:19:15Let me go ahead and run python of
- 1:19:16calculator.py again. And let me just add
- 1:19:19999
- 1:19:20plus one. And notice I don't have to
- 1:19:23type decimal points, even though I'm
- 1:19:25converting to float. My program will
- 1:19:27just allow me to type decimal points,
- 1:19:29but I don't need to oblige. The answer
- 1:19:31of course here it should be and is in
- 1:19:33fact 1,000, whether or not we round. So
- 1:19:35that's just arithmetic with integers
- 1:19:37here. But in
- 1:19:39the US, we tend to format long numbers
- 1:19:42by putting commas
- 1:19:44uh after or before every triple of
- 1:19:46digits. Other countries flip it and they
- 1:19:48use periods and commas instead. That's a
- 1:19:50system setting. You can change that on
- 1:19:52your own Mac or PC or device for python
- 1:19:55or any language. But for me, I'm using
- 1:19:57the uh US approach here, which is
- 1:19:59periods for decimal points and commas
- 1:20:01for separators. What if I wanted this to
- 1:20:03be outputted as 1 comma 000?
- 1:20:07Just to make it a little more clear that
- 1:20:09it's 1,000 and not something like 100.
- 1:20:12That's even more useful when it's like 1
- 1:20:14million. 1 comma 000 comma 000. Wouldn't
- 1:20:18it be nice if we could automatically
- 1:20:19output those numbers as well? Well, it
- 1:20:21turns out that we can. There is a way
- 1:20:24using python
- 1:20:26to actually specify that we want to
- 1:20:28include commas like this. And here we
- 1:20:31have an opportunity to bring back our
- 1:20:32old friend, the F string.
- 1:20:35First, let me do something that's not
- 1:20:36that productive. First, let me do this.
- 1:20:38Let me print out the value of Z, but
- 1:20:41wait a minute, I can't just say {quote}
- 1:20:42Z cuz that's literally going to print Z
- 1:20:44on the screen. So, let me wrap it with
- 1:20:46those curly braces like I did before,
- 1:20:48but that too was not enough. I literally
- 1:20:51needed to add an F at the beginning of
- 1:20:53my string to tell Python that this is an
- 1:20:55F string, a format string. That now is
- 1:20:57going to print out, not very
- 1:20:59interestingly, just the value of Z
- 1:21:01itself. So, that I'm going to great
- 1:21:03lengths just to print Z when really I
- 1:21:05could have just passed Z as the sole
- 1:21:07argument. But, just to ensure that I
- 1:21:09haven't broken it, let's do this again.
- 1:21:11999 plus 1 enter. Okay, it's still a
- 1:21:14thousand. So, I didn't make anything
- 1:21:16worse. But, notice and this syntax is
- 1:21:18unfortunately a bit cryptic. Notice that
- 1:21:21I can actually do this. I can put a
- 1:21:22colon after the Z and I can put a comma
- 1:21:26thereafter. This looks very cryptic
- 1:21:28admittedly and even I have to constantly
- 1:21:30look things like this up in the
- 1:21:31documentation to remember the syntax.
- 1:21:33But, here let me run it again. Python of
- 1:21:35calculator.py
- 1:21:37999
- 1:21:391 and now notice that the number's been
- 1:21:42automatically formatted for me. If I
- 1:21:44were in a different country or locale, I
- 1:21:45could absolutely override this to use
- 1:21:47periods instead of commas or vice versa.
- 1:21:50But, in this case here, it's just
- 1:21:51happening for me automatically. So,
- 1:21:52there too, we see a hint of what it
- 1:21:54means to really format a string. There's
- 1:21:56even more power, more powerful
- 1:21:58capabilities built into that.
- 1:22:01All right, let me pause here to see if
- 1:22:02there's any questions now on floats,
- 1:22:05on rounding,
- 1:22:06or on this use of F strings.
- 1:22:10Yes, so I have a question. So, when
- 1:22:12using floats, um is there like a cap you
- 1:22:14have on the decimal points you can have?
- 1:22:16Really good question. So, floats, yes,
- 1:22:18and this is a problem we'll revisit
- 1:22:20before long. Floats cannot represent
- 1:22:23numbers infinitely precisely in a
- 1:22:25nutshell because computers only have so
- 1:22:27much memory. They only have a finite
- 1:22:29amount of memory. You and I only have a
- 1:22:30finite amount of hardware inside of the
- 1:22:33computer. So, at some point they're
- 1:22:35going to have to round. Right now, I'm
- 1:22:37rounding automatically. Effectively,
- 1:22:38computers will eventually have to do
- 1:22:39that for us, but we'll see that as a
- 1:22:41fundamental problem before long.
- 1:22:44Allow me to turn back just for a few
- 1:22:45final examples on float before we
- 1:22:47introduce a few final examples that
- 1:22:49allow us not just to use functions, but
- 1:22:51to make our own. Let me propose that we
- 1:22:53also try our hand at a bit of division
- 1:22:56here. Let me propose that we modify this
- 1:22:58calculator now to still take a couple of
- 1:23:00floats, but let's now just do something
- 1:23:02a little simpler than
- 1:23:03a little different from this, just doing
- 1:23:05X divided by Y. And let me go ahead and
- 1:23:07get rid of my format string and just
- 1:23:09keep it simple for now, printing out Z
- 1:23:11instead. And what are we going to see
- 1:23:13here? Well, just some simple division.
- 1:23:14So, Python of calculator.py. Let's do
- 1:23:17something like 2 divided by 3. And of
- 1:23:20course, I get .66666. And to Ethan's
- 1:23:23question a moment ago, it does seem to
- 1:23:24be finite. It's not rounding in a weird
- 1:23:26way here, but I only seem to see so many
- 1:23:29digits. That's a
- 1:23:31inevitability of using a float in this
- 1:23:33way. By contrast, just so you know,
- 1:23:35integers nowadays in Python can be as
- 1:23:38big as you want them to be. Unlike other
- 1:23:40languages, there is no upper bound on
- 1:23:41how big an int can be now in Python, but
- 1:23:44there is a bound on just how precise a
- 1:23:46floating point value can be.
- 1:23:48All right. Now that I've got some simple
- 1:23:49division working here, let's go ahead
- 1:23:51and round this. It would be nice to
- 1:23:53round this really long number .666666
- 1:23:56and so forth to maybe just two decimal
- 1:23:58places. We've seen how to do this with
- 1:24:00round though, at least in its
- 1:24:01documentation. Let's just round this not
- 1:24:03to the nearest int by passing in just X
- 1:24:06divided by Y, which is one argument once
- 1:24:09the math is done inside of the
- 1:24:10parentheses. I don't want to pass in
- 1:24:12just one argument. I want to pass in two
- 1:24:14so that I can specify in digits number
- 1:24:17of digits, which recall was the second
- 1:24:19parameter for round. Let me go ahead and
- 1:24:21run pythonofcalculator.py. I'll do the
- 1:24:23same thing, two and then three, 0.67.
- 1:24:27So, here too, we see a way of rounding
- 1:24:29now, not just to a nearest integer, but
- 1:24:31to a nearest number of digits. But,
- 1:24:34there's another way to do this here. And
- 1:24:36in fact, this evokes our our F F-string
- 1:24:39example again. Let me go ahead and
- 1:24:41change this. Suppose that you didn't
- 1:24:43remember the round function or for some
- 1:24:45reason you didn't want to use it, you
- 1:24:47instead want to just use a format
- 1:24:48string. Well, let's go there. Let me do
- 1:24:51{quote} Z, but let me surround it with
- 1:24:54those curly braces. Let me add the F at
- 1:24:56the beginning. And again, this is not
- 1:24:57interesting yet. This is just going to
- 1:24:59print out Z, but I'm adding a lot more
- 1:25:01complexity to turn it into an F-string.
- 1:25:04But, notice I can do something else
- 1:25:05after my variable name, after the colon,
- 1:25:08if this were going to be a big integer,
- 1:25:11I might want to use a comma, like
- 1:25:12before, to separate each triple of
- 1:25:14numbers with commas, but I don't. I'm
- 1:25:16going to use a different sequence
- 1:25:18uh of characters. I'm going to say
- 1:25:20{quote} 2F. And this too is one of these
- 1:25:23very cryptic things I have to constantly
- 1:25:25look up cuz I forget if I don't use it
- 1:25:27that often. So, don't be intimidated if
- 1:25:29this looks especially weird, but this
- 1:25:31is, according to the documentation, the
- 1:25:33way you specify using an F-string how
- 1:25:36many digits you want to print. So, let
- 1:25:38me run this version of the calculator,
- 1:25:39type in two and then three, we get the
- 1:25:41exact same thing. But again, this is
- 1:25:44just consistent with my claim that in
- 1:25:45programming we can so very often solve
- 1:25:48the same problem in multiple ways. This
- 1:25:51is just now the F-string approach to
- 1:25:53that very same problem.
- 1:25:56All right, which one is better? It
- 1:25:57depends. In this case, they're pretty
- 1:25:59equivalent. You could imagine though it
- 1:26:01being useful to use a function sometimes
- 1:26:03so that you can pass in an argument like
- 1:26:05N digits as that second argument, or you
- 1:26:07can imagine just deciding in advance
- 1:26:09that you want {quote} 2 and then writing
- 1:26:11it like this. Let's transition now from
- 1:26:14focusing on strings and on integers and
- 1:26:17on floats to focusing now on functions
- 1:26:19themselves. We began today by focusing
- 1:26:21on how you can use functions that come
- 1:26:23with Python. But, wouldn't it be nice if
- 1:26:25you could invent your own functions,
- 1:26:26especially if, to our point earlier, you
- 1:26:29find yourself solving the same kind of
- 1:26:30problem again and again? It's nice that
- 1:26:33Python comes with the print function cuz
- 1:26:34it's really useful to be able to print
- 1:26:36things on the screen, but wouldn't it be
- 1:26:37nice if you could print specific things
- 1:26:39on the screen by just calling your own
- 1:26:41function? Well, let me propose that we
- 1:26:43do this. Let me go back to VS Code here,
- 1:26:46and let me propose that we go back to
- 1:26:48hello.py. I'm going to reopen hello.py
- 1:26:51where we left it before, and I'm going
- 1:26:53to go ahead now and propose that we
- 1:26:54consider how we can start improving this
- 1:26:57further by making our own function. I
- 1:26:59have written so many programs today that
- 1:27:02just say hello, and each time I'm using
- 1:27:04print, but wouldn't it have been nice if
- 1:27:07from the beginning of today we could
- 1:27:08just call a function called hello that
- 1:27:11just says hello for us. Now, the authors
- 1:27:13of Python years ago didn't think that we
- 1:27:15need a special function just to say
- 1:27:17hello, but I would like that to exist.
- 1:27:19I'm saying hello so many times, I just
- 1:27:21want to be able to call a function
- 1:27:22hello. So, I'm going to start from
- 1:27:24scratch here. I'm going to delete all of
- 1:27:26my code from earlier, and I'm going to
- 1:27:28pretend for the moment that a function
- 1:27:30called hello exists. And I'm going to do
- 1:27:33just as I did before, I'm going to get
- 1:27:34the user's name with the input function
- 1:27:36asking, "What's your name?" {question
- 1:27:38mark} And now, I'm going to call a
- 1:27:40function hello, and then I'm going to
- 1:27:43print out the user's name.
- 1:27:45Now, I will admit hello doesn't exist,
- 1:27:48so bad things are about to happen, but
- 1:27:50let's see what. Let me go down to my
- 1:27:52terminal window. Let me run python of
- 1:27:54hello.py.
- 1:27:55I think the first line's going to be
- 1:27:57okay cuz that worked before, and indeed
- 1:27:59it's prompting me for my name. So, let
- 1:28:01me type in David. The second line of
- 1:28:03code is apparently calling a function
- 1:28:05that looks like it's called hello
- 1:28:07because why is it a function? It has a
- 1:28:09parenthesis and a close parenthesis
- 1:28:10immediately after it, and that's what
- 1:28:12every function we've used has looked
- 1:28:13like, but Python's not going to
- 1:28:15recognize this one. When I hit enter
- 1:28:16now, I get a name error. Name hello is
- 1:28:19not defined. Did you mean help? I
- 1:28:22didn't, although it's opportune. That's
- 1:28:24what I need at this point is some help,
- 1:28:26but I am encountering this error because
- 1:28:28why? The function just doesn't exist.
- 1:28:31So, how do I make this function exist?
- 1:28:33Well, I need to create it myself using
- 1:28:35this keyword def. Def for define. So,
- 1:28:39here too, just as str is short for
- 1:28:41string and int is short for integer, def
- 1:28:44is short for define. If and when you
- 1:28:46want to define, create, invent your own
- 1:28:49functions, you can do so using now this
- 1:28:52keyword in Python. So, let me go back to
- 1:28:54my code here, and let me propose that we
- 1:28:57define this perhaps in this way.
- 1:28:59At the very top of my file, I'm going to
- 1:29:01first take a moment to define a function
- 1:29:04called hello using def
- 1:29:06hello open parenthesis close parenthesis
- 1:29:09colon. What this means now is that
- 1:29:11Python is going to treat every line of
- 1:29:14code that I indent underneath this one
- 1:29:17as the meaning of this new function
- 1:29:19hello. So, def is important, as is the
- 1:29:22space. I get to choose the name of the
- 1:29:24function, and I'm choosing to call it
- 1:29:26hello. The parentheses with nothing
- 1:29:27inside means that this function at the
- 1:29:29moment it's not going to take any
- 1:29:30inputs, no arguments there, too. The
- 1:29:32colon means stay tuned for some
- 1:29:34indentation. Everything that's indented
- 1:29:37beneath this line of code is going to be
- 1:29:39part of this function. It's going to be
- 1:29:40a super short function, one line of
- 1:29:42code. It's just going to print out
- 1:29:44{quote} hello.
- 1:29:46But now, on lines one and two, I have
- 1:29:48invented my own function hello. Notice
- 1:29:52these dots that have now magically
- 1:29:53appeared here. This is just a setting of
- 1:29:55my text editor, VS Code in this case,
- 1:29:57that's just making super explicit to me
- 1:30:00that I've hit the space bar four times,
- 1:30:02or equivalently the tab key once, which
- 1:30:04is converted automatically to four
- 1:30:06spaces. Generally speaking, I'm going to
- 1:30:08need to make sure that all of my
- 1:30:09indented code lines up now so that
- 1:30:11Python knows that it's all part of the
- 1:30:13same thing. But it's easy in this case
- 1:30:15cuz it's just a single line. But now,
- 1:30:17thanks to lines one and two, the
- 1:30:19function hello will absolutely exist
- 1:30:22when I'm ready to use it on line six. So
- 1:30:25let me go down to my terminal window and
- 1:30:26run python of hello.py enter.
- 1:30:29Here comes my name again. And now, when
- 1:30:31I hit enter, I now see hello David. All
- 1:30:35right, we've kind of regressed though,
- 1:30:36right? This is not nearly as pretty as
- 1:30:38it once was. I think we can probably do
- 1:30:41better than this by improving things
- 1:30:44further. Why don't we consider though
- 1:30:46how we might, say, parameterize the same
- 1:30:49function? That is to say, can we
- 1:30:51customize hello to maybe take the user's
- 1:30:53name as input so that we can say not
- 1:30:55only hello, but the person's name all on
- 1:30:58one line, all in one breath. Well, I
- 1:31:01think we can do this. Let me propose
- 1:31:03that we do this as follows. Let me go
- 1:31:05ahead and up in my code, let me inside
- 1:31:08of these parentheses, let me come up
- 1:31:10with my own parameter name. I have
- 1:31:12complete choice here, and I'm going to
- 1:31:14say that the name of my parameter will
- 1:31:16be the word to. Why? Because I want my
- 1:31:19function to sound like the verb I it
- 1:31:22represents, hello, but who do you want
- 1:31:24to say hello to? Well, I'm going to call
- 1:31:26my parameter for this function to, just
- 1:31:28cuz in English it kind of sounds nice to
- 1:31:30me. Hello to. Who do you want to say
- 1:31:32hello to? That's why I'm calling this
- 1:31:33parameter to instead of something
- 1:31:35simpler like X or Y or Z.
- 1:31:38All right, well what do I want to do
- 1:31:39with the word to? Well, I can do a
- 1:31:41couple of different things. We've seen
- 1:31:42like so many different ways to implement
- 1:31:44hello. Let me just add a comma there for
- 1:31:46grammar's sake, and then let me put the
- 1:31:48word to after that as the second
- 1:31:51argument to the function hello. There's
- 1:31:54other ways we can do this, and we've
- 1:31:55seen so many, but this one looks a
- 1:31:56little clearer to me, I'll say. What's
- 1:31:59going to happen next? Well, I don't
- 1:32:00think I need this extra print line here,
- 1:32:03I think what I'm going to do is this.
- 1:32:05I'm going to go ahead here and print out
- 1:32:08not the person's name manually, I'm
- 1:32:11going to instead instead say hello
- 1:32:13parentheses name. So, what am I now
- 1:32:16doing? On lines one and two, I'm
- 1:32:18defining my very own function called
- 1:32:20hello, but this time that function has
- 1:32:22been designed to take a parameter, a
- 1:32:24single parameter as input, and I'm using
- 1:32:27the value of that parameter which I
- 1:32:29called two to plug into print so that I
- 1:32:32see not only hello, but also that
- 1:32:34person's name. What am I doing on line
- 1:32:36five? Same as always, I'm just getting
- 1:32:38the user's name. Line six, I'm not only
- 1:32:40calling hello, I'm passing as input the
- 1:32:43name variable as an argument so that
- 1:32:47that's what gets passed into hello. And
- 1:32:49what's happening here is essentially
- 1:32:50this. Even though the variable is called
- 1:32:52name here, when the function itself is
- 1:32:55called, the computer assumes that that
- 1:32:58same value is now called two. So, name
- 1:33:01is essentially copied to another
- 1:33:03variable called two, so that in the
- 1:33:06context of hello, I can say hello to
- 1:33:09that variable instead. And we'll see in
- 1:33:11a moment what happens if we don't keep
- 1:33:13those uh straight. Let me go ahead and
- 1:33:15run python if hello.py, enter. What's
- 1:33:17your name? And now I'm crossing my
- 1:33:19fingers. Enter. There we go. We're back
- 1:33:22in business, but now I have my own
- 1:33:23custom function called hello that's
- 1:33:25allowing me to say hello to a specific
- 1:33:27person. And here's where now things can
- 1:33:29get really fancy. What if you wanted
- 1:33:31your hello function to say hello to
- 1:33:33someone specific, but you know what, if
- 1:33:34you don't know who you want to say hello
- 1:33:36to, you want to say hello to the whole
- 1:33:38world? You can give parameters default
- 1:33:40values. We've seen that. Recall that
- 1:33:42with print, there was a default value
- 1:33:44for sep, for the separator. There was a
- 1:33:46default value for end, the line ending.
- 1:33:49We can do that, too, and here's the
- 1:33:51syntax. If you want the value of this
- 1:33:54parameter by default, if not provided by
- 1:33:57the programmer to be equal to {quote}
- 1:33:59world, you literally do that in the same
- 1:34:03line you're defining the function. And
- 1:34:05I'll admit it's starting to look more
- 1:34:06cryptic, but I'm still just defining a
- 1:34:08function called hello. It takes a
- 1:34:10parameter called two, but I'm assigning
- 1:34:13it with the equal sign a default value
- 1:34:16of {quote} world just in case the
- 1:34:18programmer doesn't call hello with an
- 1:34:21argument. And we can see this here. Let
- 1:34:23me change my code to use hello in two
- 1:34:26ways. On line five, I'm going to very
- 1:34:28simply call hello no arguments. Then on
- 1:34:32line six, I'm going to get the name.
- 1:34:33Line seven, I'm going to call hello with
- 1:34:35an argument. So, you'll see hello now
- 1:34:37being used in two ways. Let me go ahead
- 1:34:39and run python of hello.py.
- 1:34:41I'll type in my name. Oh, interesting.
- 1:34:44Notice, I already see hello world, but
- 1:34:46that's expected cuz line five happens
- 1:34:49before line six. But once I type my
- 1:34:51name, now the program's going to be a
- 1:34:53little more polite and say hello to me
- 1:34:56personally. So, there too, we see with
- 1:34:59relatively simple but new syntax how you
- 1:35:01can implement functionality very similar
- 1:35:04in spirit to what the print function
- 1:35:06gave us automatically. Now you have
- 1:35:08control over doing that yourself. But
- 1:35:11let me now make this point too. One of
- 1:35:13the whole points of defining your own
- 1:35:14functions is one, just to avoid having
- 1:35:16to repeat yourself again and again. You
- 1:35:18don't have to actually keep reinventing
- 1:35:21the wheel and keep using the print
- 1:35:23function again and again and again if
- 1:35:24you just want to say hello. Wouldn't it
- 1:35:26be nice now if I could kind of move this
- 1:35:28code that I wrote for defining the hello
- 1:35:31function and just to be dramatic, I'm
- 1:35:34going to hit enter a whole lot of times,
- 1:35:3650 lines down, and put my definition of
- 1:35:39hello way further down in this file.
- 1:35:41Why? Well, just for in the spirit of out
- 1:35:43of sight, out of mind. Because if I now
- 1:35:45rewind to the start of my program, now
- 1:35:48you can sort of take for granted that
- 1:35:50oh, hello is a function. Why? Because
- 1:35:51it's there on line one and it has an
- 1:35:53open parenthesis and a close
- 1:35:54parenthesis, which up until now has
- 1:35:56meant call this function. And then on
- 1:35:58line two, we're getting a variable from
- 1:35:59the user by typing in their name and
- 1:36:01then we're calling hello, passing in
- 1:36:03that value. Well, at this point, I can
- 1:36:05just take for granted that hello exists,
- 1:36:07even if it's way down further in the
- 1:36:09file or as we'll see in future weeks,
- 1:36:11even if it's in a different file
- 1:36:12altogether. But there's a problem here,
- 1:36:15and let me go ahead and run this version
- 1:36:17of hello.py.
- 1:36:19Notice that as soon as I run the
- 1:36:21interpreter, python of hello.py, I see a
- 1:36:23name error, name hello is not defined.
- 1:36:26Again, did you mean help? Well, again
- 1:36:29fitting, I do need some help here, but I
- 1:36:30didn't mean to call the function help.
- 1:36:33The problem here though is that python
- 1:36:35is just taking me literally. I have
- 1:36:36defined my function hello all the way
- 1:36:39down here, but I'm trying to use it way
- 1:36:42up here, and that's not allowed.
- 1:36:43Python's interpreter's going to take you
- 1:36:45literally, and if you use a function, it
- 1:36:46must already exist by the time you are
- 1:36:49calling it. So, how do I fix this? Well,
- 1:36:51apparently, I can't do that. I have to
- 1:36:53define any functions I want at the very
- 1:36:56top of my file, but that too could get
- 1:36:58me into a bit of trouble eventually
- 1:37:00because if I constantly have to define a
- 1:37:03function above where I want to use it,
- 1:37:05you're kind of writing code in reverse.
- 1:37:06You're constantly writing functions up
- 1:37:08here, up here, up here as opposed to
- 1:37:09like writing your code logically, top to
- 1:37:11bottom. So, let me fix this in a more
- 1:37:13standard way, which is to do this.
- 1:37:16Generally speaking, you do want to put
- 1:37:18the main part of your code at the top of
- 1:37:21your file. And in fact, I'm going to go
- 1:37:23so far as to define my function called
- 1:37:26main. It's not a requirement, but it's
- 1:37:28indeed a convention, and this just
- 1:37:30connotes to the reader that this is the
- 1:37:32main part of my program. I'm going to
- 1:37:33get rid of my empty hello call now and
- 1:37:36only pass in one version with hello
- 1:37:38name, and then down here, a couple lines
- 1:37:41further down, I'll actually define my
- 1:37:43hello function. Unfortunately, now that
- 1:37:45I've reordered the functions in this way
- 1:37:47by putting the main part of my code at
- 1:37:49the top and hello at the bottom so that
- 1:37:52my logic kind of flows top to bottom, if
- 1:37:54I go ahead and run python of hello.py
- 1:37:56enter,
- 1:37:57nothing whatsoever happens. If I do it
- 1:38:00again, nothing whatsoever happens. Well,
- 1:38:02why in the world is this? Well, just
- 1:38:05because I've defined a function called
- 1:38:06main and I've defined a function called
- 1:38:08hello doesn't mean that I've actually
- 1:38:10called, that is used either of them.
- 1:38:13Yes, I'm using hello inside of main, but
- 1:38:16no one is telling python to actually use
- 1:38:18or call main. So, in order to tidy this
- 1:38:22up, the last thing I need to do in this
- 1:38:24file it seems is actually call my main
- 1:38:27function. And in fact, by calling my
- 1:38:29main function in this way, it gets me
- 1:38:31out of trouble because now I'm defining
- 1:38:33main first, but I'm not calling hello
- 1:38:34yet. I'm defining hello next, but I'm
- 1:38:37not calling hello next. I only at the
- 1:38:39very end of this file call main, which
- 1:38:42has the effect of running this code up
- 1:38:44here, which has the effect of running
- 1:38:46this code down here, and it allows me
- 1:38:48therefore to organize my file and order
- 1:38:51my functions in any way I want including
- 1:38:53main at the very top, and solving
- 1:38:56ultimately that problem of python not
- 1:38:58knowing what's going on. Now, it's
- 1:39:00important to note that I defined my
- 1:39:02function hello as taking an argument
- 1:39:04two, and then I passed into that
- 1:39:06function the value of the variable that
- 1:39:08I wanted to say hello to, that is the
- 1:39:10variable called name. Because suppose I
- 1:39:12had done something a little bit
- 1:39:13differently. Suppose that I hadn't hello
- 1:39:16as taking an argument, so I just remove
- 1:39:18mention of two and its default value
- 1:39:20world, and I go back up to my main
- 1:39:23function and I just call hello itself
- 1:39:26without passing in any argument. And
- 1:39:28now, let me go ahead and make one more
- 1:39:29change, one more mistake technically.
- 1:39:32Let me go ahead and just try to naively
- 1:39:34print out the value of name in the hello
- 1:39:37function. So, now to be clear, in my
- 1:39:38main function on line two, I'm defining
- 1:39:40my variable called name and assigning it
- 1:39:42the return value of the input function
- 1:39:44from the user. I'm then just calling
- 1:39:46hello. In my hello function, which now
- 1:39:48no longer takes any arguments, I am
- 1:39:51calling print, passing in hello,
- 1:39:53{comma}, and then immediately passing in
- 1:39:55name, the variable into which I got the
- 1:39:57user's input. But the catch is that name
- 1:40:00exists now only in main. And so watch
- 1:40:02what happens when I try to run this
- 1:40:04version of the program with Python
- 1:40:05hello.py. I hit enter. I'm prompted for
- 1:40:08my name, d a v i d, enter, and ah, a
- 1:40:11name error. Name name, {quote} is not
- 1:40:14defined. So it turns out that this is
- 1:40:16actually an issue of what's called
- 1:40:18scope. Scope refers to a variable only
- 1:40:21existing in the context in which you
- 1:40:23defined it. So in so far as I define
- 1:40:25this variable name in my main function,
- 1:40:28I can only use that variable in my main
- 1:40:30function. I can't use it, as I've tried
- 1:40:32to here, in my hello function. It
- 1:40:34doesn't exist in that so-called scope.
- 1:40:36And so this is why now, if I rewind and
- 1:40:39undo all of those changes, you'll see
- 1:40:42that I'm deliberately passing name from
- 1:40:44my main function into my hello function.
- 1:40:47And now in the hello function, it
- 1:40:48technically has a different name. It's
- 1:40:50called two in that context, but that's
- 1:40:51fine. It's completely up to each
- 1:40:53individual function to name its own
- 1:40:55variables or name its own arguments. But
- 1:40:57this is a way now that I'm handing to
- 1:40:59the hello function the value of that
- 1:41:01variable so it can be printed by hello
- 1:41:03as well.
- 1:41:04And there's one final flourish we can
- 1:41:06add here. Now that we've implemented
- 1:41:08hello, you'll notice that hello only has
- 1:41:10a so-called side effect. It only prints
- 1:41:12out something to the screen. Well, what
- 1:41:15if I also want my function
- 1:41:17to not have a side effect per se, but
- 1:41:20actually hand me back a value. Recall
- 1:41:22that the input function returns a value,
- 1:41:25the string that the user typed in.
- 1:41:27Recall that the int function returns a
- 1:41:29value, the float function returns a
- 1:41:31value that was passed into it. Well, you
- 1:41:34can use one final keyword here,
- 1:41:36literally return to return a value
- 1:41:39explicitly yourself. In fact, let me go
- 1:41:41back to VS Code here, and I think we'll
- 1:41:44return our attention to calculator.py
- 1:41:47and see if we can't implement one other
- 1:41:49version of calculate. calculator.py that
- 1:41:52actually has our own function that even
- 1:41:54returns a value. So, I'm going to go
- 1:41:56ahead and open up calculator.py,
- 1:41:59and I think this time I'm going to throw
- 1:42:01everything away as before, and I'm just
- 1:42:03going to start practicing what we're
- 1:42:05preaching here. Define a function called
- 1:42:07main, which is now going to be the main
- 1:42:09part of my function. Let's go ahead and
- 1:42:11now declare a variable called X and
- 1:42:14assign it to the converted version of
- 1:42:17the user's input after asking them
- 1:42:19what's X. So, again, a line of code
- 1:42:21quite like we've done before. And
- 1:42:23suppose now that what I want to do is
- 1:42:25square this value. I want to take the
- 1:42:27number that the user's typed in and
- 1:42:29raise it to the power of two. So, 2
- 1:42:31squared would be 4, 3 squared would be
- 1:42:339, 4 squared would be 16, and so forth.
- 1:42:36Well, how do I go about implementing a
- 1:42:38function literally called square, which
- 1:42:40actually doesn't come with Python built
- 1:42:42in? Well, let me assume for the moment
- 1:42:44that it does exist, and let me say
- 1:42:45something like this.
- 1:42:47Let me go ahead and say that printing,
- 1:42:50how about X squared is
- 1:42:54comma
- 1:42:55square of X. So, what have I done? I've
- 1:42:59defined a function called main, and I've
- 1:43:01implemented two lines. The first of
- 1:43:03these lines prompts the user for a value
- 1:43:05X and converts it to an int and stores
- 1:43:07it in a variable called X. On line
- 1:43:09three, I then say X squared is and then
- 1:43:12I pass a second argument to the print
- 1:43:14function, whatever the return value is
- 1:43:17of a square function. But square doesn't
- 1:43:20exist, and I'll show you this here. If I
- 1:43:22now call main at the bottom, and I run
- 1:43:25python of calculator.py,
- 1:43:30I'll see that X is two and then I see a
- 1:43:33whole bunch of errors. A name error,
- 1:43:35name square is not defined. So this
- 1:43:37isn't a typo here, it's just the
- 1:43:38function doesn't exist, but I think I
- 1:43:40can make it exist here. Let me go ahead
- 1:43:42and define another function called
- 1:43:44square. This one's going to take in a
- 1:43:46number and I'm going to call it
- 1:43:47generically N as many a programmer would
- 1:43:50just to represent any old number and
- 1:43:52then what do I want to do in order to
- 1:43:54square N? Well, a number squared is
- 1:43:57really just itself times itself, so I'm
- 1:43:59going to do this N times N. But it's not
- 1:44:03enough just to do the math yourself, N
- 1:44:05times N. You're going to have to return
- 1:44:07the actual value N times N and that's
- 1:44:10our new keyword here. When I now do
- 1:44:12this, watch what happens. Python of
- 1:44:14calculator.py enter, X say shall be two,
- 1:44:18X squared is four. Let me go ahead now
- 1:44:21and say X is now three, X squared is now
- 1:44:25nine. So I've implemented my very own
- 1:44:27function that returns the square of a
- 1:44:29value and because I'm using the return
- 1:44:31keyword, that ensures that I can pass
- 1:44:34the return value of this, just like the
- 1:44:36return value of input or int or float to
- 1:44:39another function like print instead. And
- 1:44:41here too, there's going to be so many
- 1:44:42ways to solve the same problem. I can
- 1:44:44actually raise N to the power of two.
- 1:44:47We've not seen this syntax before, but
- 1:44:49if you use two asterisks like this, two
- 1:44:51stars, that raises the thing on the left
- 1:44:54to the power on the right. Or it turns
- 1:44:56out there is in Python a function called
- 1:44:58pow for raising something to the power
- 1:45:00that takes two arguments, the first of
- 1:45:02which is the number, the second of which
- 1:45:04is the exponent. So there too, there's
- 1:45:05just so many ways to actually solve that
- 1:45:08same problem as well.
- 1:45:11So ultimately, what have we done here?
- 1:45:12We first introduced functions, these
- 1:45:14actions or verbs, many of which come
- 1:45:16built into Python that you can just use
- 1:45:17in your own code. We then introduced
- 1:45:19variables by which you could store those
- 1:45:21return values and then maybe do
- 1:45:22something more with it. At the end of
- 1:45:24the day too, you now have the ability to
- 1:45:26create to invent your own functions to
- 1:45:28solve simple problems like hello or in
- 1:45:30the weeks to come much more
- 1:45:32sophisticated, more challenging, more
- 1:45:33fun problems as well.
About this transcript
This page contains the full transcript of CS50P - Lecture 0 - Functions, Variables by CS50, generated from the public captions YouTube serves with the video. The transcript has 21,070 words across 2,982 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to translate the transcript into 100+ languages.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, translation to 100+ languages, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.