15 hours 57 minutes 47 seconds
🇬🇧 English
Speaker 1
00:00
This course from Harvard University is an introduction to programming using the Python programming language. You will learn how to read and write code as well as how to test and debug it. This course is taught by Dr. David Malan.
Speaker 1
00:15
And it's designed for students with or without prior programming experience who'd like to learn Python
Speaker 2
00:45
Hello, world. My name is David Malan, and this is CS50's introduction to programming with Python. Whereas CS50 itself is an introduction to the intellectual enterprises of computer science and the art of programming, this course is specifically focused on programming in Python itself.
Speaker 2
01:01
At the beginning of the course, we'll be focused on a topic in programming known as functions and variables, mechanisms via which you can write code that solve smaller problems, but you can compose those smaller solutions into solutions to larger problems still. We'll then transition to a look at conditionals, a way in code of expressing yourself logically, to maybe do something if some question has an answer of true or not do something if the answer is false. We'll transition thereafter to introducing you to loops, the ability in code to do something again and again and again some number of times. We'll then transition to something a little more technical known as exceptions.
Speaker 2
01:36
Unfortunately, a lot can go wrong when you're writing code. Some of it your fault. Some of it perhaps someone else's fault. But you can write code defensively, so to speak, and actually catch those kinds of exceptions, those errors, and handle them properly so that the users you're writing code for don't actually see the same.
Speaker 2
01:52
Thereafter, we'll take a look at libraries, third-party code written by other people often, or perhaps yourself in the past, that you can use and reuse in your own project so as to avoid reinventing the wheel again and again. We'll look thereafter at something called unit tests. It turns out you'll actually write code to test your own code, but you won't have to write tests for your tests. Indeed, this is a best practice in industry, writing tests for your code so that, 1, you can be sure that your code today is, hopefully, if your tests are correct, correct itself.
Speaker 2
02:21
But moreover, if you or someone else modifies your code tomorrow or down the line, you can rerun those same tests to ensure that those new changes have not broken anything about your own code. We'll then take a look at something called FileIO, I-O for input and output, the ability to not just store information inside of a computer's memory, but rather save it persistently to disk, so to speak, to files and folders. We'll then take a look at another technique known as regular expressions, whereby in Python you can define patterns and you can validate data to make sure the human types something in as you expect. You can use regular expressions to extract data, perhaps from some data set you're trying to analyze.
Speaker 2
03:00
We'll then take a look, ultimately, at object-oriented programming, a paradigm, a way of writing code whereby you can represent, in code, real-world entities. And this is in addition to other paradigms of programming that we'll also explore, among them procedural programming, where you write lots of those functions, procedures, really, top to bottom to solve problems step by step, and even something known as functional programming as well. And then at the very end of the course, we equip you with all the more tools for your toolkit, an additional building blocks, additional vocabulary via which after the same course you can go off on your own and either take other courses or solve projects of your own using all of these mechanisms. Now, this course itself assumes no prior programming background, so you don't have to have written a single line of code in Python or any language yet.
Speaker 2
03:46
But this is also a course that you can take before, during, or even after CS50 itself if you'd like to get all the more versed with Python. Each week via the course's lectures will we introduce you to any number of concepts that we'll then drill down more deeply into in the form of problem sets each week, that is, programming projects that will enable you to apply some of those lessons learned to problems of your very own. And by the end of the course, you'll have solved so many problems that ideally are representative of problems you'll eventually encounter in the real world. Whether you aspire to solve code in the technical world, or perhaps in the arts, the humanities, the social sciences, the natural sciences, or beyond, you'll have ultimately the vocabulary and the technical skills via which to approach the same.
Speaker 2
04:30
This, then, is CS50. And this is CS50's introduction to programming with Python. My name is David Malan, and this is our week on functions and variables. But odds are, many of you, most of you, have never actually programmed before.
Speaker 2
05:13
So let's start by doing just that. Let me go ahead here and open up my computer, and on it a program called Visual Studio Code or VS Code, which is just a very popular program nowadays for actually writing code. Now, you don't have to write code using this particular tool. In fact, all we need at the end of the day is a so-called text editor, a program for writing text.
Speaker 2
05:33
And heck, if you really want, you could even use something like Google Docs or Microsoft Word. You'd have to save it in the right format. But really, at the end of the day, all you need is a program for writing text because that's what code is, text. Now, within this particular program, I'm going to have the ability to create 1 or more files via this top portion of the screen.
Speaker 2
05:50
And I'm going to do so by diving right in and doing this at the bottom of my screen. At the bottom of my screen is a so-called terminal window. And this is a command line interface, or CLI, interface to the underlying computer, be it your Mac or your PC or even some server in the cloud. And what I'm going to do here is literally write code and then the name of the file that I want to code, for instance, hello.py.
Speaker 2
06:14
As we'll soon see, any program that you write in Python generally has a file name that ends in .py to indicate to the computer that it's indeed a program written in Python. Now, you'll see here at the top of my screen, I have a blinking cursor, a line 1, which is where the very first line of my code is going to go, and then just a tab that reminds me of the name of this file, hello.py. And without even knowing much Python, I'm going to write my very first program here as follows. Print, open parenthesis, quote, hello, comma, world, close quote, and close parenthesis.
Speaker 2
06:46
And you'll see that at my keyboard, some of my thoughts were finished for me. I only had to type 1 parenthesis, and the other 1 automatically appeared. And that's just a feature that we'll see of tools like this tool here. Now, even if you've never programmed before, odds are you can guess, infer what this simple program is going to do.
Speaker 2
07:03
And it's only 1 line. Print, open parenthesis, quote, hello world, close quote, close parenthesis. Indeed, when I run this program, ultimately, it's just going to say, hello to the world. And in fact, this is a very famous, perhaps the most canonical, program that you can write as your very first program in Python or any other language.
Speaker 2
07:20
And so that's what I've done here. But on my Mac, my PC, even my phone, I'm generally in the habit, like you, of running programs by double clicking an icon or just tapping on the screen. But I see no such icons here. And in fact, that's because my interface to at least my current Mac or PC or some server in the cloud is, again, only a CLI, Command Line Interface, which even though it might feel like it's a step back from the menus and buttons and icons that you and I take for granted every day, you'll find, we think, that it's ultimately a much more powerful interface and incredibly popular to use among programmers in the real world.
Speaker 2
07:54
So to run this program, I'm going to have to use a command. And I'm going to move my cursor back down to the bottom of the screen here, where previously I already ran 1 command, the command code, which has the effect of opening VS Code in my computer. And then I passed in the name of the file that I wanted to code up. Now I have a chance to type a second command.
Speaker 2
08:13
And you'll see I see a second dollar sign. Now, the dollar sign here doesn't indicate any kind of currency or money. It just is the symbol that's generally used to indicate your prompt, where the command line interface wants you to put those commands. Now, the command I can run here is going to be this.
Speaker 2
08:29
I'm going to run Python of hello.py. Now, why is that? Well, it turns out that when I actually write code in a language like Python, it's, of course, stored in that file, hello.py. But I need to interpret the code, top to bottom, left to right, so that the computer knows what to do.
Speaker 2
08:47
Indeed, at the end of the day, even if you don't really know much about computers, you've probably heard that computers only understand zeros and ones, the so-called binary system. Well, if that's the case, then something that says print and parentheses and quote unquote, hello world, is not surely zeros and ones. We have to somehow translate it into the zeros and ones that the computer understands. Now fortunately, so long as you've installed such a program in advance, there's a program as well as a language called Python.
Speaker 2
09:14
So Python is not only a language in which we're going to write code, it's also a program, otherwise known as an interpreter, that you install for free on your own Mac or PC or some server in the cloud. And you can then run that program, that interpreter, passing to it as input the name of your file, like mine here, hello.py. And then that program, that interpreter, will handle the process of reading it top to bottom, left to right, and translating it effectively into those 0's and 1's that the computer can understand. So let's do just that.
Speaker 2
09:43
Let me go back to VS Code here. I already typed out Python of hello.py, but I didn't yet hit Enter. And that's what's now going to kick off this command. And hopefully, if I didn't mess any of this up, I should see my very first program's output to the screen.
Speaker 2
09:58
And voila, hello world. So if you, too, have typed exactly that same code and have executed exactly that same command, you will have written your very first program, in this case, in Python. Well, now let's take a step back and consider what is it that we actually just did, and What is it we're looking here on the screen? Well, first and foremost, in most any programming language, you tend to have access to what are called functions.
Speaker 2
10:21
A function is like an action or a verb that lets you do something in the program. And generally speaking, any language comes with some predetermined set of functions, some very basic actions or verbs that the computer will already know how to do for you, that the language really will know how to do for you. And you, the programmer, the human, can use those functions at will to get the computer to do those things. Now, the program in question here, hello.py, is using 1 function.
Speaker 2
10:49
And you can perhaps guess what it is. That function is, of course, going to be this function print. And that print function, of course, doesn't print some preordained string of text. That is to say, it prints whatever it is you want it to print.
Speaker 2
11:04
And here, too, do we have another piece of terminology in the world of programming, namely arguments. An argument is an input to a function that somehow influences its behavior. The people who invented Python, of course, didn't necessarily know what it is you and I are going to want to print to the screen. So they designed this print function, using these parentheses, with the ability to take as input some string of text, be it in English or any other human language, that is what you want this function ultimately to print onto the screen.
Speaker 2
11:34
And what is it that the program's ultimately doing on the screen? Well, it's printing, of course. It's showing us, hello world, on the screen. And that's generally in programming known as a side effect.
Speaker 2
11:43
It can be visual. It can be audio. In this case, it's something that appears on the screen. And functions, therefore, can indeed have these side effects.
Speaker 2
11:50
1 of the things they can do as this verb or action is to display on the screen as a side effect something like those words that we wanted, hello world. So that's my first program. And I'm feeling pretty good. Everything worked as planned.
Speaker 2
12:04
I didn't make any mistakes. But honestly, when you're learning how to program, and even once you've learned how to program years later, you're going to make mistakes. And those mistakes, of course, are referred to a term you might already know, which is that of a bug. A bug is a mistake in a program.
Speaker 2
12:18
And they can take so many forms and take comfort, perhaps, in knowing that over the coming weeks, you're going to make so many mistakes. You're going to have so many bugs in your code, just like I did and just as I still do. And those bugs themselves are just mistakes that are problems for you to solve. And over the weeks to come, we're going to give you a lot of tools, both mental and technical, via which you can solve those problems.
Speaker 2
12:41
But just don't get discouraged. If when writing your program for the first time it doesn't even work that first time, it will, with time, with practice, and with experience. So let me deliberately now make a mistake that there was a non-zero chance I might have done accidentally already, but I got lucky. Let me go ahead and just suppose I forgot to include something like the closing parenthesis at the end of this line of code.
Speaker 2
13:04
The code is almost correct. It's like 99% of the way there. But now that I've pointed it out, it's pretty obvious that it's missing that closed parenthesis. But even little, seemingly minor details like that, that you and I as humans wouldn't really care about.
Speaker 2
13:18
And if you're sending an email or a text message, whatever, it's just a typo. It's not that big a deal. It is going to be a big deal to the computer. A computer is going to take you literally.
Speaker 2
13:27
And if you don't finish your thought in the way the language expects, it's not going to necessarily run at all. So let's do this. I'm going to go ahead here and clear my screen down at the bottom just so I can start fresh. And I'm going to go ahead and run this version of my program after having made that change by deleting the parenthesis.
Speaker 2
13:44
I'm going to go ahead and type Python again of hello.py. And this time when I hit Enter, I'm hoping I'm going to see hello world. But here we have an error on the screen, a so-called syntax error, which refers to my having made a mistake at my keyboard. And This 1, fortunately, is pretty straightforward.
Speaker 2
14:01
It indeed says that this open parenthesis was never closed. And so that's probably pretty intuitive now what I need to do. I need, of course, to close it. Unfortunately, sometimes the error messages we'll see in the coming weeks are not going to be nearly that user friendly.
Speaker 2
14:16
But there, too, again, with experience, with practice, will you get better at debugging such programs. Let me now make sure that I indeed fixed it correctly. Let me go ahead, run now hello.py, and hit Enter, and voila, we're back in business. Well, let me pause here and see if we have any questions now about Python itself, writing, or running even the simplest of these programs.
Speaker 3
14:39
CLAUDIO CHERUBINO-SAMPONI-LEZAMALLAO
Speaker 2
14:41
Could I write code inside a Word or, for example, Microsoft Excel? And what's the barrier to doing that? A really good question.
Speaker 2
14:49
And allow me to very explicitly say to the entire internet that you should not write code with Microsoft Word. I mentioned that only because it's a tool via which you can write text. And code is, At the end of the day, just text, but it's not the right tool for the job. We don't need boldfacing, underlining, paragraphs, and the like.
Speaker 2
15:06
We generally want something much simpler than Microsoft Word or Google Docs. And so VS Code is an example of just a more general purpose text editor. Its purpose in life is to allow you, the human, to edit text. Nowadays, these text editors come with many more features.
Speaker 2
15:20
In fact, you'll notice that even in my code here, even though it's just 1 line, there's a bit of color to it. The word print, for me, is appearing in blue. The parentheses are black. And we'll see, as we might write more lines of code, more and more of the lines will come to life in various colors.
Speaker 2
15:34
Now, that's just 1 feature of a text editor. We'll see, too, that it has features like this built-in terminal window. It's going to have a built-in tool for debugging or finding problems with code. And it's just a very popular tool nowadays.
Speaker 2
15:45
But there are many, many others out there. You're welcome to use them for this course and beyond. We just happen to use this 1 in large part, too, because you can also use VS Code nowadays for free in the cloud. How about 1 other question here on programming with Python or Hello World or syntax more generally?
Speaker 2
16:02
AUDIENCE 2
Speaker 4
16:02
That was time to ask if it is not possible to run the code without using the terminal window. DAVID MALAN
Speaker 2
16:09
GALPINKIN I think I heard, is it not if it's possible to run the program without the terminal window? OK, You froze for me again. But let me infer what the question is.
Speaker 2
16:19
So in this environment, as I've configured my computer, I can only run these Python programs via the terminal window. Now, that's good for me, the programmer, or the person who's trying to learn how to program. But it's not very good if you want to ship this software and have other people use your actual code. You can absolutely write programs and then allow other people to use not a command line interface, but a graphical user interface or GUI, G-U-I.
Speaker 2
16:43
This is just 1 mechanism, and perhaps I think the best 1 with which to start writing code, because eventually, it's going to give us a lot more control. Allow me to forge ahead here, but please feel free to continue asking questions along the way, if only via the chat. And let's consider now how we might go about improving this program. Let's go about improving this program to make it a little more interactive and not just assume that everyone is going to want to be greeted more generically as Hello World.
Speaker 2
17:10
Let's see if I can't get this program to say something like Hello David, or Hello Jeremiah, or Hello Horatio, or whatever the actual user's name is. Well, to do this, I'm going to go back up to hello.py, and I'm going to add another line of code at the very top that simply says, for instance, what's your name, quote unquote, with an extra space at the end. So I'm printing to the user, asking them a question for some input. But now I need another function to actually get input from the user.
Speaker 2
17:37
And perfectly, enough Python comes with a function named input. So here I'm going to go ahead and call a function, input, open paren, close paren. And that's going to prompt the user with just a blinking cursor waiting for them to type something in. Now, it turns out if I read the documentation for the input function, it actually takes an argument itself.
Speaker 2
17:56
I don't need to use print separately and then prompt the user for input. So I can actually simplify this code before we even use it. I'm going to go ahead here and take that same string from print, put it as an argument to the input function, and get rid of the print altogether. And in fact, that print would have added a new line anyway.
Speaker 2
18:12
So now I've just got a prompt where the user's cursor is going to end up blinking at the end of the line asking them, what's your name? In my terminal window, I'm going to run Python of hello.py, enter. OK, we're making progress. It seems that this new function input is indeed prompting me, the human, for input.
Speaker 2
18:29
So I'm going to type in my name, David, and hit Enter. Unfortunately, it doesn't really do anything with my name. It just outputs it immediately. All right, well, I could fix this, right?
Speaker 2
18:39
I could go up to line 2, and I could change world to David. And then back in my terminal window here, I can do Python of hello.py, Enter. What's your name, David? Enter.
Speaker 2
18:50
And there we go. All right, now I'm up and running. Now my program is working as intended. Of course, this isn't really working as intended here.
Speaker 2
18:59
Let me go ahead and try pretending to be my colleague Carter here. Well, Carter's name is this. I'm going to go ahead and hit Enter, and I'll see, of course, hello, Carter. Well, obviously not, because I've hardcoded, so to speak.
Speaker 2
19:10
I've written literally my name inside of the string. So we need some way now of actually getting back what the user's input is and doing something with it, ultimately. And for this, we're going to leverage another feature of programming, specifically a feature of some functions, which is that they can have return values as well. If you think of input as being, again, this action, this verb, you can actually personify it as maybe a person, like a friend of yours that you've asked a question of.
Speaker 2
19:36
And you've asked your friend to go get input from someone else. Go ask that person their name. And if your friend comes back knowing that person's name, well, wouldn't it be nice if they handed that name back to you? That's kind of what we need metaphorically the function to do, is get the user's input and then hand it back to me so that I, the programmer, can do something with it.
Speaker 2
19:54
But if it's going to be handed back to me, I kind of want to put it somewhere so that I can then print it back on the screen. I need to do the equivalent of take out a piece of paper or a Post-it note, write down on this piece of paper what it is the human has said, so that I can then feed it into as input that print function. And to do that, we're going to need 1 more feature of programming, namely variables. And odds are, most everyone's familiar with variables from math class way back when, x and y and z and the like.
Speaker 2
20:23
Well, programming has that same capability, this ability to create a variable, in this case, in the computer's memory, not just on a piece of paper. And that variable can store a value, a number, some text, even an image or video or more. A variable is just a container for some value inside of a computer or inside of your own program. So how do I go about expressing myself in this way?
Speaker 2
20:49
Well, I think what I'm going to do is introduce a variable that's a little more interestingly named than x or y. I could just say this, x equals input. But I'm going to use a better name than a typical mathematical variable here. And I'm going to literally call my variable name.
Speaker 2
21:03
Why? Well, in programming, because I have a whole keyboard in front of me, I can use more descriptive terms to describe what it is I'm writing. And now, though, there's an opportunity to consider a specific piece of syntax. We've seen parentheses.
Speaker 2
21:15
We've seen quotes, all of which are necessary when passing inputs to a function. But this equal sign here that's in between input on the right and name on the left is actually important. And it's technically not an equal sign per se. It doesn't mean equality as much as it means assignment.
Speaker 2
21:34
So in Python and many programming languages, a single equal sign is the assignment operator. And what that means specifically is that you want to assign from right to left whatever the user's input is. So the equal sign copies from the right to the left whatever the return value of the function on the right is. So again, the input function clearly gets input from the user.
Speaker 2
21:58
That's why I was able to type my name or Carter's. But it also sort of behind the scenes hands that value, that return value, back to me, the programmer. And if I use an equal sign and a variable, no matter what I call it, I can store that input in that variable so as to reuse it later. So now sitting in the computer's memory somewhere is a container containing David, quote unquote, or Carter, quote unquote, or whatever the human has typed in.
Speaker 2
22:25
But here it's easy to make a mistake. Suppose I decide to try to print that name. And so I kind of, on a hunch, type in this, hello, comma, name, just kind of plugging in the name of the variable. Well, let me go ahead here and run Python of hello.py and hit Enter.
Speaker 2
22:43
That's going to prompt me for my name. And let me type in my name, D-A-V-I-D, but I haven't hit Enter yet. And perhaps via the chat, what's going to happen here when I now hit Enter? I'm hoping it says, hello, David.
Speaker 2
22:55
I'd be OK if it says, hello, world. But I don't want it to say what it's actually going to say. And yep, what we're seeing in the chat is, well, it's probably going to say literally, hello, comma, name. So that's not quite right.
Speaker 2
23:07
So we need another way of printing out the value inside of that variable rather than just this word, name. Well, let me try this in a couple of different ways. Let me try this as follows. Let me go ahead and maybe undo this, because I've gotten pretty good already at saying hello.
Speaker 2
23:23
So let's draw that line in the sand and just say, all right, let's at least get hello, comma, out the door. Let's now print name. And just on a hunch, I'm going to try this. I'm going to use print again, because you can use these functions as many times as you need.
Speaker 2
23:35
And I'm going to pass to the print function the variable called name. But notice, I'm being a little clever now. I'm not putting it in double quotes, because we've seen already that double quotes means literally print out N-A-M-E. I'm getting rid of the quotes this time in hopes that now by passing the variable called name to the function called print, it will, in fact, go about printing the contents of that variable, that is, its so-called value.
Speaker 2
24:02
All right, let's go ahead and do this here. Python of hello.py, Enter. What's your name? David.
Speaker 2
24:08
And now, crossing my finger still, I see hello, David. All right, so it's not the best program. I'm kind of cutting some corners here, so to speak. I'm saying, hello, David, on 2 separate lines.
Speaker 2
24:21
So it's not as elegant. It's not as pretty. It's not as grammatically appropriate in English as just saying it all in 1 breath on 1 line. But at least I've solved the problem, just not very well yet.
Speaker 2
24:32
But let me take a step back now and perhaps introduce a couple of other concepts with which we should be familiar, which is as our programs get longer and they're no longer just 1 line, or 2, or even 3, eventually our programs are going to become dozens of lines, maybe even hundreds of lines long, let's set the stage for success moving forward. It turns out that Python, and a lot of programming languages, also support something called comments. Comments are notes to yourself in your code. And you include comments by way of a special symbol.
Speaker 2
25:01
In Python, it's going to be the hash symbol, typically. And that allows you to write the equivalent of a note to yourself, but in a way that's not going to break your code. The computer actually ignores your comments. It's just there for you.
Speaker 2
25:12
It's just there for your teacher. It's just there for your colleague with whom you're sharing, ultimately, that code. So if I go back to VS Code here, and I just want to add some comments to this program to explain to my teacher, to myself, to my colleagues what this program is doing, well, let's go ahead and do that. I'm going to go at the very top of my program.
Speaker 2
25:31
And on line 1 now, I'm going to move that original line of code down a bit. I'm going to add a hash. And I'm going to say something like this, ask user for their name. Now, I don't have to use that language.
Speaker 2
25:42
I don't have to use that text. I could use any human language whatsoever. It doesn't have to be English. But I'm going to now, below that, just say something like this, say hello to user.
Speaker 2
25:53
And you'll notice that VS Code, by default, is kind of graying out my comments. They're no longer blue. There's no red. There's no color in them.
Speaker 2
26:00
And that's just because they're notes to myself, and the computer ultimately is going to ignore them. But what we have now is 2 comments, ask user for their name, and then a second comment, say hello to user. And I've just kind of commented each chunk of code, like each line or lines plural of code, that are doing something noteworthy. Why?
Speaker 2
26:18
Well, tomorrow morning when I wake up having slept for quite some time, forgotten what it is I did the previous day, it's convenient with comments to just see in English or your own human language what it is this program is doing so that you don't have to read the code itself. And better yet, if there's maybe a mistake down the road, you can read what your intention was. And then you can look at the code and figure out if your code's now doing what you intended. So this isn't really necessary for a program this small.
Speaker 2
26:46
It's pretty obvious with just 1 or 2 or 3 lines what the program's doing. It's just as fast to read the code than the comments. But getting into this habit is generally a good thing, to comment your code every 1 or few lines so as to remind yourself and others what it is your intent and your code is doing. What's nice about comments, too, is this.
Speaker 2
27:04
Comments can also serve to be sort of a to-do list for yourself. There's this notion in programming of pseudocode. Pseudocode isn't a formal thing. It's not 1 specific language.
Speaker 2
27:15
It's just using English or your own human language to express your thoughts succinctly, methodically, algorithmically, so to speak. But pseudocode, therefore, because it's not Python and it's not necessarily English, it just kind of allows you to outline your program even in advance. So for instance, if I wasn't sure today how I wanted to go about writing this program, but I did know what I want to do, I could have started today by just writing this in hello.py. No code.
Speaker 2
27:43
I could have written just a couple of comments to myself. Step 1, ask user for their name. Step 2, say hello to user. Then, once I've outlined my program in pseudocode, then I can go in there and say, all right, how do I ask the user for their name?
Speaker 2
27:56
Well, I can do input, quote unquote, what's your name, question mark. And then on the left here, I can maybe put a variable and assign it to that. OK, how do I say hello to the user? Well, I know I can use print to say things on the screen.
Speaker 2
28:08
Let me say hello, comma. And let me now print the person's name. So again, pseudocode is a nice way of structuring your to-do list, especially if you have no idea how to write the code, because it breaks a bigger program down into small, bite-sized tasks. All right, let me pause here to see if there are now any questions on comments, pseudocode, return values, or variables.
Speaker 2
28:34
Any questions we can clear up here? AUDIENCE 1. Yeah, my question is, does the function input work for any type of information or only for words? DAVID MALAN.
Speaker 2
28:45
Yeah, really good question. So according to its documentation, and we'll look more at formal documentation soon, input is going to expect what's called a string. That is a sequence of text, be it in English or any other human language. But it's indeed going to be expecting text with which to prompt the user.
Speaker 2
29:01
Good question. How about another question from the group, if we could? AUDIENCE 2
Speaker 3
29:04
I want to ask how I make several line comments. DAVID MALAN
Speaker 2
29:08
Oh, how do you do many lines of comments, if I'm hearing you correctly? Sure. You would just keep doing them like this.
Speaker 2
29:16
You just prefix each of the lines with a hash symbol, like I'm doing here. There is another technique for doing multi-line comments in Python that actually tend to have special meaning. You can do 3 double quotes like this, and then anything in between here is a comment. That's another technique, or you can use single quotes as well.
Speaker 2
29:35
But more on those, I think, another time. All right, well, if you don't mind, let me forge ahead here and see how we might improve this program further and also introduce a few other features that we might want to take into account over time. So it turns out that we can certainly improve on this program because it's a little disappointing that I'm cutting this corner and saying, hello, comma, and then on a new line, printing out name. Like, we can do better.
Speaker 2
29:59
And Most programs you use on your phone or your laptop certainly keep text together when people want. So how can we go about doing that? Well, there's a few different ways. And in fact, the goal here is not so much to solve this 1 problem, but to demonstrate and emphasize that in programming, Python and other languages, There's so many ways sometimes to solve the same problem.
Speaker 2
30:18
And here's 1 way to solve this problem. Let me go in here, and let me go ahead now and say, hello, comma. And let me just add to the end of that the user's name. So I'm using plus in kind of an interesting way.
Speaker 2
30:32
This is not addition, per se. I'm not adding numbers, obviously. But I do kind of want to add the person's name to the string of text, hello, comma. Well, let me go now down to my terminal window and run Python, if hello.py again, Enter.
Speaker 2
30:47
What's your name? I'm going to type in David. Enter. OK, it's better.
Speaker 2
30:52
It's better, but there's a minor bug, albeit aesthetic here. There's missing space, but let's just use some intuition here. Well, if I'm missing the space after the comma, why don't I go ahead and just add it manually here? Let me now rerun the program, Python of hello.py, Enter, David, Enter.
Speaker 2
31:08
And there we go. Now we have something that looks a little prettier in terms of English grammar, hello, comma, space, David. And Now if we rewind, you might have noticed before or wondered why I had this seemingly extra space after my question mark, namely here. There's a space after the question mark before the double quote.
Speaker 2
31:26
And that was just for aesthetics, too. I wanted to move the user's cursor 1 space to the right so that when I type their name, or they type their name, it's not immediately next to that same question mark there. But there's other ways we can do this. It turns out that some functions, print among them, actually take multiple arguments.
Speaker 2
31:45
And it turns out that if you separate the inputs to a function, the so-called arguments to a function, with a comma, you can pass in not just 1, but 2345, onward. So let me go ahead and pass in not just hello comma space, but that followed by name. And this is a little confusing, potentially, at first glance, because now I've got 2 commas. But it's important to note that the first comma is inside of my quotation marks, which is simply an English grammatical thing.
Speaker 2
32:15
The second comma here is outside of the quotes, but between what are now 2 separate arguments to print. The first argument is hello comma space. The second argument is the name variable itself. So let's see how this looks.
Speaker 2
32:29
Python of hello.py, Enter. What's your name? David, Enter. OK, I've kind of overcorrected.
Speaker 2
32:35
Now I've got 2 spaces for some reason. Well, it turns out, and this is subtle, when you pass multiple arguments to print, it automatically inserts a space for you. This was not relevant earlier because I was passing in 1 big argument to print all at once by using that plus operator. This time, I'm passing in 2 because of the comma.
Speaker 2
32:57
So if I don't want that extra space, I don't need to pass in 1 myself. I can just do this. And now notice, if I run this program again, python if hello.py, type in my name David, now it looks grammatically like I might want. Now, which of these approaches is better?
Speaker 2
33:12
This approach uses a function print with 2 arguments, hello, and the name variable. The previous version, recall, technically used 1 argument, even though it looked a little curious. It's 1 argument in the sense that the computer, just like mathematicians, are going to do what's inside of parentheses first. So if inside of parentheses, you have this string of text, hello, comma, and a space, which I need to add back, then you have a plus, which means not addition per se, but concatenation, to join the thing on the left and the thing on the right.
Speaker 2
33:43
This ultimately becomes the English phrase, hello, comma, space, David. And then what's being passed ultimately to the function is technically something like this. But it's doing it all dynamically. It's not me typing in David as I discreetly did earlier.
Speaker 2
34:01
It's figuring out dynamically what that value is after concatenating hello with the value of name and then passing that ultimately to print as the sole argument. Let me pause here to see if there's any questions on numbers of arguments now to functions.
Speaker 3
34:19
AUDIENCE 2 Can we use a function many times to solve a certain problem, which we can encounter many times in our code? DAVID MALAN
Speaker 2
34:26
You can. You can use a function many different times to solve some problem. What we'll soon see, though, is if you find yourself as the programmer solving a problem the same way again and again and again, it turns out you'll be able to make your own function so that you don't have to keep reusing the basic ones that come with the language.
Speaker 3
34:45
I was curious about the comma and the plus sign. So after plus sign, can we give just 1 variable? And after comma, can we give multiple variables?
Speaker 3
34:55
What is the difference? DAVID MALAN ANDERSON
Speaker 2
34:56
A good question. So in the context of strings, and I keep Using that term, string is a technical term in a programming language. And again, it means a sequence of text, a character, a word, a whole paragraph even.
Speaker 2
35:08
So the plus operator is not just used, as we'll see, for addition of numbers in Python like we do on paper pencil, but it also is used for concatenation of strings on the left and the right. If you did want to combine not just 2 strings, left and right, but a third and a fourth, you can absolutely keep using plus, plus, plus, plus, and chain them together, just like in math. Eventually, that's going to start to look a little ugly, I dare say, especially if your line of code gets long. So there's better ways that we'll actually soon see.
Speaker 2
35:38
And a good question as well. Well, let me come back to the code here in question and see if we can show you just a couple of other ways to solve the same problem, along the way emphasizing that what we're technically talking about here, yes, are strings. But there's even a technical term for these strings. In Python, it's just str, so to speak, S-T-R for short, for string.
Speaker 2
35:57
As you may know, if you programmed in other languages, People who invent programming languages like to be very succinct to the point. So we tend to use fairly short phrases to describe things, not necessarily full words. So while you might say string, technically in Python what we're really talking about, these sequences of text, are technically STRs. This is an actual type of data in a program.
Speaker 2
36:19
But we'll soon see that there's other types of data in programs as well. In fact, let's see if we can't improve this in 1 other way. I like the progress we've made by keeping everything on the same line, hello, David, all on the same line. What more, though, could we do in terms of solving this problem?
Speaker 2
36:37
Well, it turns out that we didn't have to give up entirely with using print twice. Let me rewind a little bit and go back to that earlier version where I wasn't really sure how to solve this problem. So I was using print once to print out just the hello and the space and the comma. And then I used print again to print name.
Speaker 2
36:55
That, strictly speaking, wasn't bad. But there was this visual side effect that I just didn't like. It just looked ugly to have these 2 lines of text separate from 1 another. But there's another way to fix this.
Speaker 2
37:07
Clearly, it seems to be the case that the print function is automatically outputting a blank line. It's moving the cursor automatically for me to the next line, because that's why I'm seeing hello on 1 line and David on the next, and then my prompt, the dollar sign, on the line below that. So print seems to be presuming automatically that you want it to move the cursor to the next line after you pass it some argument. But you can override that behavior.
Speaker 2
37:32
Again, functions take arguments, which influence their behavior. You just have to know what those arguments are. And it turns out that if we look at the documentation for Python's print function, we can actually look up at this URL here. Docs.python.org is where all of Python's official documentation lies.
Speaker 2
37:52
If I poke around, I can find my way to, more specifically, this URL here, where I can find all of the available functions in Python and the documentation therefore. And if I go a little more precisely, I can even find specific documentation for the print function itself. And rather than pull that up in a browser, I'm going to go ahead and highlight just 1 line from that same URL, which is this. And This is easily the most cryptic thing we've seen yet.
Speaker 2
38:17
But this is the official documentation for the print function. And 1 of the best things you can do when learning a programming language is honestly learn to read the documentation. Because truly, all of the answers to your questions will in some way be there, even though admittedly, it's not always obvious. And I will say, too, Python's documentation isn't necessarily the easiest thing, especially for a first time or a novice programmer.
Speaker 2
38:40
It, too, just takes practice. So try not to get overwhelmed if you're not sure what you're looking at. But let me walk you through this example. This, again, is a line of text from Python's official documentation for the print function.
Speaker 2
38:50
What this indicates as follows is this. The name of this function is, of course, print. Then there's a parenthesis over here and another closed parenthesis way over there. Everything inside of those parentheses are the arguments, the potential arguments, to the function.
Speaker 2
39:05
However, when we're looking at these arguments in the documentation like this, there's technically a different term that we would use. These are technically the parameters to the function. So when you're talking about what you can pass to a function and what those inputs are called, those are parameters. When you actually use the function and pass in values inside of those parentheses, those inputs, those values, are arguments.
Speaker 2
39:31
So we're talking about the exact same thing. Parameters and arguments are effectively the same thing, but the terms you use from looking at the problem from different directions when we're looking at what the function can take versus what you're actually passing into the function. So what does this imply? Well, this syntax is pretty cryptic.
Speaker 2
39:47
But at the moment, just know that an asterisk, a star, and then the word objects means that the print function can take any number of objects. You can pass in 0 strings of text, 1 string like I did, 2 strings like I did, or technically infinitely many if you really want, though that code's not going to look very good. After that, we see a comma. Then we see another parameter here called sep, short for separator in English.
Speaker 2
40:12
And notice the equal sign and the single quote, space, single quote. So quote, unquote, space. I don't know what that is yet, but I think we've seen a hint about it. Let's focus, though, for a moment on this.
Speaker 2
40:24
The print function takes another parameter called end, and the default value of that parameter is apparently, based on this equal sign and these quotes, backslash n. And what is backslash n, if you'd like to chime in the chat? Anyone who's programmed before has probably seen this. Though if you've never programmed before, this might look quite cryptic.
Speaker 2
40:45
Backslash n means new line. And it's a way textually of indicating if and when you want the computer effectively to move the cursor to the next line, create a new line of text. And so technically, if we read into the documentation, We'll see more detail on this. The fact that there's a parameter called end and the documentation for the print function just means that by default, this print function is going to end every line with backslash n.
Speaker 2
41:13
You don't literally see backslash n. You see a new line. You see the cursor moving to the next line. Now by that logic, let's move backwards.
Speaker 2
41:20
Sep for separator. The default value of separator is apparently a single blank space. Well, where have we seen that? Well, recall in an earlier example when I passed in not just 1, but 2 arguments to the print function, recall that they magically had a space between them.
Speaker 2
41:38
In fact, they had that space plus my own space. And that's why I deleted my space, because at that point, it was extra. So this just means that when you pass multiple arguments to print, by default, they're going to be separated by a single space. By default, when you pass arguments to print, the whole thing is going to be ended with a new line.
Speaker 2
41:55
Now, just by knowing this, and let me literally wave my hand at the rest of the documentation for another day. There's more things that print can do. But we're going to focus just on sep and on end. Let's see if we can't leverage this now to solve that original problem.
Speaker 2
42:08
The original problem was this. I don't like how hello, David is on 2 different lines. Well, that's happening again because print is automatically printing out a new line. So let's tell it not to do that.
Speaker 2
42:19
Let's tell it, by passing a second argument to the first use of print, to say end equals quote unquote not backslash n, which is the default automatically. Let's make it quote unquote nothing else. Let's override the default value so there is no new line. There's literally nothing there.
Speaker 2
42:41
And let's see what happens. Let me now go down to my terminal window and clear it. And I'm going to run Python of hello.py enter. I'm going to type in my name David.
Speaker 2
42:50
And I think now everything's going to stay on the same line because, and it did, this line here, 5, is going to print out hello, comma, space, but then nothing at the end of it, because I change it to be quote, unquote. The second line is going to print the name, David, or whatever the human's name is. And it will move the cursor to the next line, because I didn't override the value of end there. Just to see this more explicitly, if you do something cryptic like, well, I have no idea what's going on.
Speaker 2
43:19
Let me just put in temporarily 3 question marks here. We'll see the results of this too. Let me go back down to my terminal window, run Python of hello.py. What's your name?
Speaker 2
43:28
David. And now You see literally really ugly output, but you see literally what's going on. Hello comma space, then 3 question marks, end that print statement, and then you see DAV ID. So not a good outcome, but it demonstrates just how much control we have here, too.
Speaker 2
43:47
And let me rewind further. Recall that in our other version of this, when I passed in hello, comma, and name, they were separated by a single space. So Python of hello.py, D-A-V-I-D, Enter. That just worked.
Speaker 2
44:01
Well, what if we override the value of sep for separator? Instead of being 1 space, we could say something like question mark, question mark, question mark, just to wrap our minds around what's going on there. Let me now do Python of hello.py, david, Enter, and you see 2. These 2 inputs, hello, comma, and the name, are now separated in an ugly way by 3 question marks because I've overridden the default behavior of sep.
Speaker 2
44:30
And even though the documentation uses single quotes, I've been in the habit of using double quotes. In Python, you can use either. Strictly speaking, it doesn't matter, but you should be consistent. And I generally always use double quotes.
Speaker 2
44:41
Python's documentation, though, always uses single quotes. Questions now on these types of parameters. And allow me to propose that we give these an official name. Up until now, when we've been passing values to print, those are called positional parameters.
Speaker 2
44:59
Positional in the sense that the first thing you pass to print gets printed first. The second thing you pass to print after a comma gets printed second and so forth. But there's also these things we've now seen called named parameters, named SEP, separator, or end, E-N-D, for the line ending. Those are named parameters because, 1, they're optional, and you can pass them in at the end of your print statement, but you can also use them by name.
Speaker 2
45:26
This may be a weird question, but I was wondering, what if someone wants to add actually quotation marks within the quotation marks. DAVID MALAN Yeah, I like how you think. This is what we would call a corner case, right? Just when we've made, right, this is all sounding great, at least as programming goes.
Speaker 2
45:44
But wait a minute, What if you want to print a quote? That's a really good question. Well, let's see if we can't figure this out. Suppose that I want to print out not just the user's name.
Speaker 2
45:53
Let me simplify this further. Let me go ahead and get rid of a lot of this. And let me just say something like, hello, maybe I'm being a little sarcastic here. Hello, friend, in that kind of tone.
Speaker 2
46:08
Well, this is not going to work, actually, because you are trying to use quotes to be like friend in finger quotes, but you're also trying to end the sentence. And if I try running this, let's do this. Python f hello.py, you'll see that this is just invalid syntax. Perhaps you forgot a comma.
Speaker 2
46:22
And this is actually a bit annoying. Sometimes the error messages you see are misleading. Like the computer, the language doesn't really know what's going on, so it gives its best guess, but it's not necessarily correct. But I can solve this problem in a couple of ways.
Speaker 2
46:35
I can do this. I can change my outermost quotes to single quotes, because recall a moment ago I said you could use double quotes or single quotes so long as you're consistent. So that's fine. If you use single quotes on the outside, you can then use double quotes on the inside, and you'll see them literally.
Speaker 2
46:51
So for instance, if I run Python if hello.py, there we go. Hello, friend. But there's another way. If you insist on using double quotes, As you might want to, just to be consistent, you can also use that backslash character again.
Speaker 2
47:05
We saw the backslash in a moment ago. And that meant we don't want a literal n to be in the output. We wanted a new line. So the backslash actually represents what's called an escape character.
Speaker 2
47:16
An escape character is 1 that you can't just type necessarily once on your keyboard. You need to express it with multiple characters. So I can actually put backslashes in front of these inner double quotes so that the computer realizes, Oh, wait a minute. Those aren't quotes that finish or start the thought.
Speaker 2
47:34
They're literal quotes. So now let me go back to my terminal window, run python of hello.py, Enter, and now it's working as well. So escaping is a general technique that allows us to do that too. And if I may, let me rewind now on these examples and go back to where we left off with my code.
Speaker 2
47:52
I'm just undoing all of that because I want to get back to the point, ultimately, of specifying now a final way of solving this problem. Well, it turns out that we have yet another way we can solve this problem, which is perhaps the most frequently done now, or at least the most elegant when it comes to setting us up for longer and longer uses of strings. You can use a relatively new feature of Python that allows you to do this. You can literally put not the name of the variable like that in your string, because we already saw this is wrong, right?
Speaker 2
48:27
If you do this, you will literally see hello, comma, name. But what if I do this? What if I put curly braces or curly brackets around the variable's name? Notice VS Code is actually very subtly changing the color of it.
Speaker 2
48:38
So VS Code knows something interesting is going on here. Let me run this program, but I'm not done yet. Python of hello.py, Enter. D-A-V-I-D, Enter.
Speaker 2
48:47
OK, obviously not what I want. But I need to tell Python that this is a special string. This is what we're going to call a format string or an f string, a relatively new feature of Python in the past few years that tells Python to actually format stuff in the string in a special way. And the symbol via which you do this is a little weird, but this is what the world chose.
Speaker 2
49:09
If you put an F at the beginning of the string, right before the first quote mark, that's a clue to Python that, ooh, this is a special string. Let me format this in a special way for you. Let me now rerun the program. Python hello.py, Enter.
Speaker 2
49:25
D-A-V-I-D, Enter. And now we see the goal this whole time, hello, David. We don't start with this way, because I think if we did this the first way, you'd be like, why are we doing this? What are all these magical symbols?
Speaker 2
49:36
But this is just yet another way to solve the same problem. But let me propose that we consider now yet other things we can do with strings. And it turns out that even as we've been doing some relatively simple operations here, we've generally been trusting that the user is going to cooperate. And that is to say that they're going to actually type in what we want them to type.
Speaker 2
49:56
Now, just because they type a string, though, doesn't mean it's going to look the way we want. You and I, honestly, as humans, are actually in the habit on websites and apps of accidentally hitting the space bar a lot, either at the beginning of our input or at the end. Maybe because the space bar tends to be so big, it's pretty common to get accidental spaces before or after some user's input. You and I are definitely in the habit of not necessarily capitalizing words like we should.
Speaker 2
50:20
If we're sending text messages, we're probably being a little quick and just sending everything in lowercase, for instance, if that's your style. If your phone's not fixing it for you, maybe in a formal letter you would capitalize things properly. But you and I, as humans, can't really be trusted to type things in a nice way, necessarily, when using some piece of software, be it an app or a website or something else. But it turns out that strings themselves come with a lot of built-in functionality.
Speaker 2
50:44
And you can see all of that in Python's own documentation here, the string data type that we've been talking about comes with a lot of functionality built in that means that we can manipulate the user's input to do more than just join it with something else, like hello. We can actually clean it up or reformat it in a way that hopefully looks a little better for us. So let me go back to my code here. And let me just demonstrate what might happen if a user doesn't cooperate.
Speaker 2
51:10
If I go ahead here and run Python of hello.py, Enter, let me just sloppily hit the space bar a few too many times. Why? I just wasn't paying attention. And I'm going to type in my name, D-A-V-I-D, and I don't know, I hit the spacebar a couple more times.
Speaker 2
51:23
Like, it's kind of a mess. It's all lowercase. That's not going to necessarily look grammatically right. It's got spaces here and here.
Speaker 2
51:30
The program is going to print exactly that. And that looks really bad, at least if we're prioritizing aesthetics and grammar. Like, why are there so many spaces after the comma? This is not a very nice way to greet your users.
Speaker 2
51:41
But we can clean this up. It turns out that built into strings, which, again, is this data type, so to speak, this type of data in Python, is the ability to actually do things to that string. So let me do this. I can actually go ahead and do something like this, name equals name.strip.
Speaker 2
52:01
And what does this do? Remove whitespace from string. And what do I mean by this? Well, on the right-hand side, notice I've written the variable name called name.
Speaker 2
52:15
I've then used a period or a dot. And then I seem to be doing what's a function, right? Anytime we've seen a function thus far, we see it's the function's name, print, or input. Then we see a parenthesis, then another parenthesis.
Speaker 2
52:26
And that's exactly what I see here. But I'm using this function a little differently. Technically, this function is, in this context, called a method. And what do I mean by that?
Speaker 2
52:35
Well, if name is a string, a.k.a. Str, well, it turns out, according to the documentation, there's a lot of functions that come with strings in Python. And you can access that functionality by using the name of a string, like literally name here, then a period, then the name of the function, and then an open parenthesis and a close parenthesis. Maybe some arguments inside of those parentheses, but in this case, it doesn't need any arguments.
Speaker 2
53:00
I just want to strip the space from the left and the space from the right of the user's input. But that's not enough. I want to remember that I've stripped off that white space on the left and the right. So I'm going to use the equal sign again here.
Speaker 2
53:13
And notice that just as before, this doesn't mean equality. This means assignment from right to left. So when this line of code here, name.strip, returns to me, a.k.a. A return value, it will return the same thing that the user typed in, but with no more whitespace to the right.
Speaker 2
53:32
So then the equal sign assignment is going to copy that value from the right to the left, thereby updating the value inside of my name variable. So you can not only assign values to variables, you can absolutely change the value of variables by just using the assignment operator, the equal sign, again and again and again. And it will just keep copying from right to left whatever the new value should be. So now, if I rerun this program, Python of hello.py, Enter, I have DAVID.
Speaker 2
54:03
Let's do it again. Space, space, space, space, space, d-a-v-i-d in all lowercase, space, space, Enter. It's better. It hasn't fixed my capitalization, so I'm still being a little sloppy with the first d, but it has stripped off all of that extra space.
Speaker 2
54:17
Super minor detail, right? Like, this isn't all that exciting, but it just speaks to the power of what you can do with just a single line of code. Now, what else can I do here? Well, I could capitalize the user's input.
Speaker 2
54:29
Let me go ahead and try this. It turns out that I could also do this, name.capitalize. So let me go ahead and capitalize user's name. And again, I'm making comments, and there's no 1 right way to write the comments.
Speaker 2
54:44
I'm just using some short English phrases here to remind myself of what I'm doing. What's now going on here? Well, let me go ahead and run Python of hello.py, Enter. Space, space, space, space, space, D-A-V-I-D, space, space, Enter.
Speaker 2
54:57
OK. Now it's looking prettier, right? No matter how the user typed in their name, even a little sloppily, I'm now fixing that. But let's try something.
Speaker 2
55:05
I'm getting a little curious here. How about this? Space, space, space, space, space, d-a-v-i-d, space, malen. I'll use my last name now.
Speaker 2
55:13
Enter. OK. So ironically, capitalize is not really capitalizing everything we want. It's clearly capitalizing what?
Speaker 2
55:22
Just the very first letter. So it turns out that, again, there's other functions in Python that come with strings. And if we poke around the documentation, scrolling through a URL like that, I bet we'll find another solution, 1 of which is actually this. Let's actually change this to title.
Speaker 2
55:39
There's yet another function that come with strings called title that do title-based capitalization, just like a book or a person's name, capitalizing the first letter of each word. And this is just going to do a little more work for us. So let's go ahead and run this. And as an aside, I'm kind of tired now at this point of typing Python, Python, Python all the time.
Speaker 2
56:00
It turns out that when using a command line interface like this, you can actually go back through all of your old commands. What I just did a moment ago was I hit the up arrow. That immediately goes back through my history of all of the commands I've ever typed. So this is just a faster way now for me to repeat myself than typing everything manually.
Speaker 2
56:17
Let me go ahead and hit Enter. Space, space, space, space, space, D-A-V-I-D, mail in, space, space, all lowercase, Enter. Now it's looking better. Now I've capitalized things and cleaned things up.
Speaker 2
56:30
But what about my code? I've got like 8 lines of code now, 4 of which are comments, 4 of which are actual code. Do I really need this much? Well, not necessarily.
Speaker 2
56:38
Watch what I can also do in Python. Let me not bother capitalizing the user's name separately. Let me say this, and capitalize user's name. I can chain these functions together.
Speaker 2
56:53
I can add title to the end of this. And now what's happening? Well, again, with a line of code like this, you first focus on what's to the right of the equal sign. Then we'll get to the left of the equal sign.
Speaker 2
57:04
What's on the right of the equal sign? This line here. Well, what does this mean? Get the value of the name variable, like david space malan.
Speaker 2
57:13
Then strip off the white space on the left and the right, that is going to return a value. It's going to return D-A-V-I-D space M-A-L-A-N without any white space to the left or right. What do you want to do with that return value? You want Python to title case it.
Speaker 2
57:28
That is, go through every word in that resulting string and fix the first letter of the first word, the first letter of the second word, and so forth. And then, now we can finish our thought, copy the whole thing from right to left into that same name variable. And you know what? I can take this even 1 step further.
Speaker 2
57:46
Why don't we go ahead and do this if we want? Let me get rid of all that. And let me just do strip and title all on that first line. And now we've gone from like 8 lines of code to 4.
Speaker 2
58:00
It's a lot tighter. It's a lot neater. And even though reasonable people might disagree, it's arguably better because it's just easier to read. Fewer lines of code, fewer opportunities for mistakes.
Speaker 2
58:10
It just allows me to move on with my next problem to solve. All right, let me pause here and see if there's any questions on these methods. A method is a function that's built in to a type of value, like these functions are, or on f strings, which we saw a moment ago.
Speaker 3
58:28
Yes, hi. Thanks, David. So is there a way to remove the spaces between the spaces that I might have added?
Speaker 3
58:35
DAVID MALANYIKA WRIGHT JR.:
Speaker 2
58:35
Short answer, no. If you read the documentation at that same URL earlier, you'll see that strip removes from the left and the right, but not in between. In fact, there's 2 other functions that come with strings.
Speaker 2
58:45
One's called lstrip. The other is called rstrip that allow you to do 1 or the other. If we want to start getting rid of space in the middle, we're going to have to do a different trick altogether.
Speaker 5
58:55
How many functions can we combine like this, dot strip, dot title here combined? So how many we can combine? DAVID MALAN HANSON-KUZNICKIUK
Speaker 2
59:03
Yeah, a really good question. Technically, as many as you want. But at some point, your code's going to start to look really, really bad, right?
Speaker 2
59:10
Because the line of code is going to get really, really long. It's eventually going to maybe wrap around again and again. So at some point, you just kind of say, like, uh-uh, that's too many. And you start breaking it up into multiple lines like I did, maybe reassigning the value to the variable as needed.
Speaker 2
59:25
And this is actually a good question. If I can pivot, Daviya, off your question, I mean, what do people think? If we could go ahead and put everyone's hands down for a moment, let me ask this. Is the way I've done this now with strip and title and input all in the same line better than my previous approach?
Speaker 2
59:45
In Zoom, you can use the Yes icon or the No icon. If you think this version is better, say Yes. If you think this previous version was better, for instance, this 1 here, where we had everything broken out, say Yes.
Omnivision Solutions Ltd