Copyright Anne Dawson 2005 - 2019

This file is: http://www.annedawson.net/Python_Spaces_Indentation.html

Last updated: Wednesday 8th August 2018, 8:05 PT, AD

Spaces and Indentation in Python programs

by Dr Anne Dawson

************************************************************************************


Beware of spaces! 
Python program statements are written one above the other, as shown in the following example:


print("Anne was here")
print("again at work")
print("having fun with Python 3")


The 3 line program above is correct and will run producing the expected output.



The program below will generate a syntax error 
because of the space character inserted at the start of the second line.


print("Anne was here")
 print("again at work")
print("having fun with Python 3")





The if statement
----------------

The if statement starts with the keyword "if" 
followed by a Boolean expression (e.g. x == 10), followed by a colon (:).


x = 10
if x == 10:
    print('x has a value of 10')
else:
    print('x does NOT have a value of 10')



Beneath the if line, the statements to be run if the condition is true are entered 
after typing a few space characters. In the example above, four space characters are used.
Any statements to be run after the else: part of the program
also have to be spaced out to the ***same level*** 
by typing 4 space characters before the statement.

Spacing out a program in this way is known as indenting. 

Indentation is part of the syntax of the Python language.


All of the Python 3 example programs
are shown correctly indented and run successfully.

Click here for the example Python 3 programs in text format

All of the Python 2 example programs
are shown correctly indented and run successfully.

Click here for the example Python 2 programs in text format

If you use the Tab key instead of using spaces for the purpose of indenting, 
then you must always use the Tab key. 

If you use four spaces, always use four spaces. 

******The Python organisation recommends using four spaces.******



It is highly recommended to use only spaces, or to set the tab level to 4 on the IDLE editor options. 

NOTE! Mixing Tabs and spaces is a syntax error, even if the program looks correct on the screen! 
You will get an "incorrect indent" error. You have been warned!






Use lots of space to make your programs more readable
----------------------------------------------------

Compare the following two Python programs
which perform exactly the same tasks when they're run.

Which of these two programs do you find easier to read and understand?


Program 1


x=10
y=20
z=30
v=x*y*z
print("Volume is: "+str(v))




Program 2


height  = 10
width   = 20
breadth = 30

volume = height * width * breadth

print("Volume is: " + str(volume))


Which of the two programs above do you like better?
I prefer Program 2. I can understand it a glance.
Not so for Program 1.

To help understandability, 
you can put as many blank lines as you wish
***between*** Python statements.



Click here for a Python programming style guide
straight from the horse's mouth
- a few words from Guido van Rossum,
the inventor of the Python programming language...
************************************************************************************


Valid HTML5!

Valid CSS!

Anne Dawson © 1995-2019 | Anne Dawson All Rights Reserved.