From the course: Programming Foundations: Fundamentals

Properly using whitespace - Python Tutorial

From the course: Programming Foundations: Fundamentals

Properly using whitespace

- Python doesn't care about extra empty lines in your programs, just like most languages. In fact, it's common for developers to add extra empty lines, also known as white space, You can think of it kind of like paragraphs in a book. Let's take a look at the 03_05_begin file. Things are really squished together and we even have a new function thrown in. You might find it especially challenging to figure out what this code is going to do, and that's totally understandable. Let's add a few blank lines so that it can be easier to read. We'll put a little bit of space here, some here after line number five. Much easier to read. Although in movies, programmers are often depicted as just endlessly typing on the keyboard, we actually spend much more time reading and understanding code than we do writing it. That's why we add white space. between the different elements of our statements. So right here on line number two, we're going to add some white space after name and right after this equals sign. We'll do the same thing again here on line number five. Just adding more white space, it makes it easier to read. If we save this file and run the code, Let's give it a name. And then when we hit Enter, it says, "It's nice to meet you, Jill. "Are you enjoying the course?" If we type, "Yes," then it says, "That's good to hear." Hopefully, you were able to pick up on what the input function does. It takes some input from the user, and then it stores it in a variable, just like you see here on line number two, we get some input and then we store it in the variable name. We do this same thing here on line number five, where we ask the user if they were enjoying the course. Then we store it in our variable answer. You'll be seeing it again and again, so now you know what it does. Now let's add a bit more space. This time we're going to add space here on line number seven, after our answer variable, and once again, after this equals sign. Let's go ahead and save it and make sure we get the same output. and choose Run Python File in Terminal. Once again, we can enter a name, hit Enter, and enter yes for if we're enjoying the course. where white space really matters to Python, and that's when it relates to its special key words. If you recall, there are over 30 key words Let's take a look here on line number seven. If is one of those key words. If is one of those key words. If we delete the space between if and the answer variable, If we delete the space between if and the answer variable, let's click Save and run this code. So we'll right click once again So we'll right click once again and choose Run Python File in Terminal. and choose Run Python File in Terminal. Notice that we get a syntax error. That's because Python expects us That's because Python expects us to have a space after the if key word. to have a space after the if key word. So let's go back and add a space once again after if. So let's go back and add a space once again after if. Don't worry if you don't understand all the ins and outs Don't worry if you don't understand all the ins and outs of the current code, just keep in mind that most of the current code, just keep in mind that most of the time, Python is very laid back about white space. In fact, you're encouraged to add it in wherever it helps to improve readability. wherever it helps to improve readability. But there are circumstances where Python takes white space very seriously, and your code will fail takes white space very seriously, and your code will fail if you don't respect the rules. if you don't respect the rules. The good thing is that the more you practice, The good thing is that the more you practice, the more you follow the rules without even thinking. the more you follow the rules without even thinking.

Contents