20

I've been searching google and this website for some time now, but I just can't seem to find a straight answer on the subject.

What is whitespace in Python? I know it's something to do with indenting with each line, but I'm not sure exactly how to use it. How does it work?

user1892304
  • 617
  • 1
  • 6
  • 11
  • check out http://stackoverflow.com/questions/637295/are-there-any-pitfalls-with-using-whitespace-in-python?answertab=votes#tab-top and http://www.python.org/dev/peps/pep-0008/#code-lay-out – Andbdrew Dec 14 '12 at 18:48
  • 17
    What are "these sorts" of questions? "Easy" ones? There's nothing wrong with this question. – FogleBird Dec 14 '12 at 19:03
  • 1
    @FogleBird basic language questions are usually best addressed elsewhere. – Marcin Dec 14 '12 at 19:04
  • 6
    I don't see any such rule in the FAQ. http://stackoverflow.com/faq – FogleBird Dec 14 '12 at 19:08
  • 1
    Other languages mostly ignore whitespaces, Python doesn't. Read [indentaion](http://docs.python.org/2/reference/lexical_analysis.html#indentation) and [Whitespaces between tokens](http://docs.python.org/2/reference/lexical_analysis.html#whitespace-between-tokens) of the official docs. – Paolo Dec 14 '12 at 19:10

5 Answers5

11

Whitespace is used to denote blocks. In other languages curly brackets ({ and }) are common. When you indent, it becomes a child of the previous line. In addition to the indentation, the parent also has a colon following it.

im_a_parent:
    im_a_child:
        im_a_grandchild
    im_another_child:
        im_another_grand_child

Off the top of my head, def, if, elif, else, try, except, finally, with, for, while, and class all start blocks. To end a block, you simple outdent, and you will have siblings. In the above im_a_child and im_another_child are siblings.

Brigand
  • 84,529
  • 20
  • 165
  • 173
  • -1 this is backwards. The specific constructs introduce the block, and must be followed by an INDENT token as a result (unless the whole body is on the same line). – Marcin Dec 14 '12 at 19:06
  • 1
    Don't forget `try`,`except`,`finally` – mgilson Dec 14 '12 at 19:09
  • You must not have read mgilson's comment entirely. It is `except`, not `catch`. – zondo May 11 '16 at 10:45
9

Whitespace just means characters which are used for spacing, and have an "empty" representation. In the context of python, it means tabs and spaces (it probably also includes exotic unicode spaces, but don't use them). The definitive reference is here: http://docs.python.org/2/reference/lexical_analysis.html#indentation

I'm not sure exactly how to use it.

Put it at the front of the line you want to indent. If you mix spaces and tabs, you'll likely see funky results, so stick with one or the other. (The python community usually follows PEP8 style, which prescribes indentation of four spaces).

You need to create a new indent level after each colon:

for x in range(0, 50):
    print x
    print 2*x

print x

In this code, the first two print statements are "inside" the body of the for statement because they are indented more than the line containing the for. The third print is outside because it is indented less than the previous (nonblank) line.

If you don't indent/unindent consistently, you will get indentation errors. In addition, all compound statements (i.e. those with a colon) can have the body supplied on the same line, so no indentation is required, but the body must be composed of a single statement.

Finally, certain statements, like lambda feature a colon, but cannot have a multiline block as the body.

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • 3
    At the risk of starting a flame war, stick with spaces -- 4 of them as outlined in PEP-8 ... (If you have an issue with this comment because you love tabs, please take it up with the authors of PEP-8 and the python community who endorse the affore-mentioned document -- They're the ones who created this convention for python) – mgilson Dec 14 '12 at 19:08
  • 1
    @mgilson It's not that I love tabs, but using all tabs is not going to cause heartache, except to people who can't bear to see any code not rigidly in conformity with pep-8. – Marcin Dec 14 '12 at 19:11
  • 2
    I agree with that point. The problem comes when trying to share with other people who have text editors set up to conform to PEP-8. Then they try to add to your code and get all the funky errors you referenced in your post because hitting TAB inserts 4-spaces automagically. But, as you say, this won't cause any problems if your file is truly all tabs and it most certainly is valid python either way (as long as you're consistent). – mgilson Dec 14 '12 at 19:20
4

It acts as curly bracket. We have to keep the number of white spaces consistent through out the program.

Example 1:

def main():
     print "we are in main function"
     print "print 2nd line"

main()

Result:

We are in main function
print 2nd line

Example 2:

def main():
    print "we are in main function"
print "print 2nd line"

main()

Result:

print 2nd line
We are in main function

Here, in the 1st program, both the statement comes under the main function since both have equal number of white spaces while in the 2nd program, the 1st line is printed later because the main function is called after the 2nd line Note - The 2nd line has no white space, so it is independent of the main function.

Dmitry
  • 6,716
  • 14
  • 37
  • 39
3
something
{
 something1
 something2
}
something3

In Python

Something
    something1
    something2
something3
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
SjayKH
  • 39
  • 3
0

Every programming language has its own way of structuring the code.
whenever you write a block of code, it has to be organised in a way to be understood by everyone.

Usually used in conditional and classes and defining the definition.
It represents the parent, child and grandchild and further.

Example:

def example()
    print "name"
    print "my name"
example()

Here you can say example() is a parent and others are children.

Jérémie Bertrand
  • 3,025
  • 3
  • 44
  • 53
Roshan
  • 77
  • 9