Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Whitespace is significant in Python in ways that you may not be accustomed to, if you are familiar with other scripting languages. Let's go ahead and make a working copy of syntax.py, and we can look at whitespace in Python. Call it syntax-whitespace.py. Go ahead and open that up, and you will notice that this main function consists of one statement, a print statement, and that print statement is indented four spaces, ands that actually tells Python that this print statement is associated with this main function.
It's actually the body of the main function. Four spaces are traditional in Python. You can use two. You can use one. It will work just as well. It does have to be consistent. In other words, if I have more than one line here, and if that other line is indented, say three spaces instead of four, if I save that and run it, you will see that I get a syntax error, and it says unindent does not match any outer indentation level.
So Python got confused. We indented this one four spaces, we indented this one three spaces and it just got confused. It didn't know what to do with that. So the indention has to be the same amount. Save that and run it, and we will see that that works. Now if I take this second line and I indent it back at the same level as the function definition, then it is no longer part of the body of that function. So what will happen when I run this is it will actually see this line, this is another line, before the syntax file and the reason for that is because that will get executed on the first pass through here before we call main.
main gets called at the end of the file. So this line will be printed first and then this line. So we will save that, and we will run it, and you see this is another line, and then this is the syntax file. So, by bringing the indentation out to the same level as the definition, it is now no longer part of the body of that function. We will go ahead and indent that again and save it and run it, and we see that it works as we expect with this second line after the first line.
So indentation as whitespace is significant in Python because Python doesn't use curly braces, or any other characters, to indicate the body of a function, or what they a call suite - what we might call a block of code that's - associated with a particular control structure. These indents are used for conditionals. They are used for classes. They are used for any place where you might need to associate a block of text with a particular control structure. Now there is one circumstance where the indentation and the whitespace is not actually significant.
If you have just one line of code in a particular structure - and that's all there is, there is not more than one line of code - that one line of code can be on the same line as the control structure itself. So if I put this print here - and I can put a space, or not put a space, or put several spaces - it will work exactly the same. I save this and run it, you see that it still works. Just like this 'if' statement down here has one line of code, which is the function call for main, now this function definition for main has just one line of code, the print, and in this case the whitespace is not significant.
I can have five spaces, I can have one space, and it still runs just the same. That's a special case for whether its just one line of code in a particular structure. Under most circumstances, where you have a function, or a class, or something like that, and you have more than one line of code, you will see the indentation used, and four spaces is traditional, and the indentation has to be consistent all the way through the block or the suite of code.
Get unlimited access to all courses for just $25/month.Become a member