One aspect in which Python is different from other
languages is that within Python code, whitespace
matters. This can seem really weird and frustrating if you
are coming from another language, such as Java or C,
that uses curly braces or start/stop keywords; instead,
Python uses indentation to separate blocks of code. This
whitespace is not used just to make your code readable;
rather, Python will not work without it. Here is an
example of a simple loop that highlights why whitespace
is so important:
Click here to view code image
>>> for kids in ["Caleb", "Sydney", "Savannah"]:
... print("Clean your room,", kids, "!")
File "<stdin>", line 2
print("Clean your room,", kids, "!")
^
IndentationError: expected an indented block
This code will generate a syntax error the minute you try
to run it. Python is expecting to see indentation on the
line after the :. If you insert four spaces before the
print() statement, the code works:
Click here to view code image
>>> for kids in ["Caleb", "Sydney", "Savannah"]:
... print("Clean your room,", kids, "!")
Clean your room, Caleb!
Clean your room, Sydney!
Clean your room, Savannah!
Python allows you to use spaces or tabs. You can use
both spaces and tabs in Python 2, but Python 3 will
return a syntax error; however, if you use both tabs and
spaces, you might end up with really weird issues that
you need to troubleshoot. The standard for Python from