Indentation is important

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!")

The convention is four spaces:

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.

Thanks Rob.

1 Like

Hi @fixit7

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 relevent statements include:

  • class
  • def
  • if, elif and else
  • for and while
  • try, except and finally
  • with
  • And starting in Python 3.10, match and case.

Thanks, I made a note of your recommendations.

Read through one of the *.py files in /Lib or the code examples in the itertools doc.

1 Like

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.

I downloaded Pyscripter but it will not run under Wine.

Netbeans is too complicated to use.

I guess that you are using Linux. Am I right?

I am happy with Visual Studio Code.

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:

  • VS Code
  • GitHub
  • WSL (Windows Subsystem for Linux)
  • Windows Terminal

I could not figure out how to use Visual Studio Code.

I am trying to remove visual studio code that is in code_1.72.2-1665614327_amd64.deb.

I have tried this.

andy@7 ~> sudo dpkg -r code_1.72.2-1665614327_amd64.deb
dpkg: error: you must specify packages by their own names, not by quoting the names 

This forum is not about Linux but you should normally use apt to install / upgrade / remove:

$ sudo apt remove code

…and dpkg -r expects package name, not file name. So if apt does not work you can try:

$ sudo dpkg -r code