I am just now learning about indentation in python. How do I know what needs indentation and by how much? This is confusing to me.
I use Geany as my editor. Can it be setup to help me?
You have to use the same number of spaces in the same block of code, otherwise Python will give you an error:
Example
Syntax Error:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
for x in range(10):
if x > 2:
pass # a place holder
Or…
if true:
pass # a place holder
elif x > 42:
pass # another place holder
else:
pass # guess what :)
… but the minimum is one space, for indents.
Your script should be:
if 5 > 2:
print("Five is greater than two!")
print("Five is greater than two!")
Also, indents need to be consistent; you can’t have two, then three of four, in the same block. The indents are a part of the syntax and not simply a style choice.
To add…
If you browse though the threads here and study the posted code, you’ll soon pick it up, as to what needs to be indented.
The amount of indentation is easier to answer: as much as you want, so long as each block is consistent.
Each block must be indented by one level relative to the previous block, but the amount of indentation in a level is not specified by the language, and can vary from one block to another. So long each block is consistent, the interpreter is satisfied.
By convention, we typically use four spaces per level. Other conventions are eight spaces (too big, in my opinion), two spaces (too little) or a single tab (people have very strong opinions either pro- or anti-tabs).
So this is legal:
for x in sequence:
# indent by four spaces = 1 level
if condition:
# Indent by eight spaces for this block
print("a big indent here")
else:
# Indent by only two spaces for this block
print("a smaller indent")
print("outdent back to four spaces")
but I recommend you stick to a nice simple, consistent 4 spaces.
Geany can help you here. Go to:
Edit menu
Preferences
Editor tab on the side bar
Indentation tab on the top of the dialog
Put in your preferences there, and pressing the TAB key will indent, and Shift-TAB will outdent.
As far as where you need to indent, you have to memorise the rules:
Any statement which is followed by a block requires the block to be indented.
The statement is followed by a colon “:” which helps you recognise that it needs a new block.
The spacing is extremely significant in Python. This defines the organisation of your code blocks. This error occurs when your code structure is messed up, such as this:
def test_function():
if 5 > 3:
print "hello"
Your file may possibly have a combination of tabs and spaces. I recommend using a Python syntax aware editor such as PyScripter or NetBeans.
Also, in whatever editor you’re using, enable visible whitespace and replace tabs with spaces. To learn more about Indentation, check out this post from scaler topics.
While tabs can be used in Python, combining tabs and spaces frequently results in the error you’re seeing. The preferred method for creating Python code is to replace tabs with four spaces.
While I did not like the Microsoft’s culture and policies in the past. I think that they currently offer very good tools for multiplatform development. Namely I use these: