The Role of Whitespace in Python


show more Understanding whitespace in Python provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training show less
please wait ...

Understanding whitespace in Python

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 fo...

Understanding whitespace in Python
Video duration: 4m 8s 6h 36m Beginner

Viewers:

Understanding whitespace in Python provides you with in-depth training on Developer. Taught by Bill Weinman as part of the Python 3 Essential Training

Subjects:
Developer Web
Software:
Python
Author:
please wait ...