# Indentation is important

**URL:** <https://discuss.python.org/t/indentation-is-important/19835>\
**Category:** Python Help\
**Created:** [October 9, 2022, 6:51pm UTC](https://discuss.python.org/t/indentation-is-important/19835 "2022-10-09T18:51:46Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![fixit7](https://avatars.discourse-cdn.com/v4/letter/f/b4bc9f/32.png) [@fixit7](https://discuss.python.org/u/fixit7)\
**Post date:** [October 9, 2022, 6:51pm UTC](https://discuss.python.org/t/indentation-is-important/19835/1 "2022-10-09T18:51:46Z")

</div>

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?

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

```

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [October 9, 2022, 6:59pm UTC](https://discuss.python.org/t/indentation-is-important/19835/2 "2022-10-09T18:59:30Z")

</div>

The convention is four spaces:

```python
for x in range(10):
    if x > 2:
        pass # a place holder

```

Or…

```python
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:

```python
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.

---

<div class="post-metadata">

**Author:** ![fixit7](https://avatars.discourse-cdn.com/v4/letter/f/b4bc9f/32.png) [@fixit7](https://discuss.python.org/u/fixit7)\
**Post date:** [October 9, 2022, 8:19pm UTC](https://discuss.python.org/t/indentation-is-important/19835/3 "2022-10-09T20:19:58Z")

</div>

Thanks Rob.

---

<div class="post-metadata">

**Author:** ![steven.daprano](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/steven.daprano/32/1083_2.png) [@steven.daprano](https://discuss.python.org/u/steven.daprano)\
**Post date:** [October 9, 2022, 11:44pm UTC](https://discuss.python.org/t/indentation-is-important/19835/4 "2022-10-09T23:44:57Z")

</div>

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:

```python
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`.

---

<div class="post-metadata">

**Author:** ![fixit7](https://avatars.discourse-cdn.com/v4/letter/f/b4bc9f/32.png) [@fixit7](https://discuss.python.org/u/fixit7)\
**Post date:** [October 10, 2022, 12:17am UTC](https://discuss.python.org/t/indentation-is-important/19835/5 "2022-10-10T00:17:46Z")

</div>

Thanks, I made a note of your recommendations.

---

<div class="post-metadata">

**Author:** ![tjreedy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/tjreedy/32/137_2.png) [@tjreedy](https://discuss.python.org/u/tjreedy)\
**Post date:** [October 10, 2022, 4:16am UTC](https://discuss.python.org/t/indentation-is-important/19835/6 "2022-10-10T04:16:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Dorthy](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/dorthy/32/7803_2.png) [@Dorthy](https://discuss.python.org/u/Dorthy)\
**Post date:** [October 19, 2022, 10:19am UTC](https://discuss.python.org/t/indentation-is-important/19835/7 "2022-10-19T10:19:49Z")

</div>

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:

```auto
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](http://code.google.com/p/pyscripter/) or NetBeans.  
Also, in whatever editor you’re using, enable visible whitespace and replace tabs with spaces. To learn more about [Indentation](https://www.scaler.com/topics/python/indentation-in-python/), 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.

---

<div class="post-metadata">

**Author:** ![fixit7](https://avatars.discourse-cdn.com/v4/letter/f/b4bc9f/32.png) [@fixit7](https://discuss.python.org/u/fixit7)\
**Post date:** [October 19, 2022, 1:26pm UTC](https://discuss.python.org/t/indentation-is-important/19835/8 "2022-10-19T13:26:12Z")

</div>

I downloaded Pyscripter but it will not run under Wine.

Netbeans is too complicated to use.

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [October 19, 2022, 1:45pm UTC](https://discuss.python.org/t/indentation-is-important/19835/9 "2022-10-19T13:45:27Z")

</div>

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

I am happy with [Visual Studio Code](https://code.visualstudio.com/).

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

---

<div class="post-metadata">

**Author:** ![fixit7](https://avatars.discourse-cdn.com/v4/letter/f/b4bc9f/32.png) [@fixit7](https://discuss.python.org/u/fixit7)\
**Post date:** [October 19, 2022, 2:30pm UTC](https://discuss.python.org/t/indentation-is-important/19835/10 "2022-10-19T14:30:42Z")

</div>

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.

```auto
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 

```

---

<div class="post-metadata">

**Author:** ![vbrozik](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/vbrozik/32/7423_2.png) [@vbrozik](https://discuss.python.org/u/vbrozik)\
**Post date:** [October 19, 2022, 3:34pm UTC](https://discuss.python.org/t/indentation-is-important/19835/11 "2022-10-19T15:34:06Z")

</div>

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

```bash
$ sudo apt remove code

```

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

```bash
$ sudo dpkg -r code

```

---

<div class="post-metadata">

**Author:** ![davidism](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/davidism/32/3295_2.png) [@davidism](https://discuss.python.org/u/davidism)\
**Post date:** [February 28, 2024, 2:02pm UTC](https://discuss.python.org/t/indentation-is-important/19835/18 "2024-02-28T14:02:41Z")

</div>


