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