Udit Vashisht
Author: Udit Vashisht


Chapter 5- Indentation

Chapter 5- Indentation

  • Feb. 13, 2019, 4:05 p.m.
  • 2 minutes read
  • 31 Views
Chapter 5- Indentation

Like many other languages, python is also a block-structured language.

Blocks

Block is basically a group of statements in a code script. A block in itself can have another block or blocks, hence making it a nested block. Now, to differentiate one block of code from another, we need some kind of separator or indication etc. Programming languages like C++, Java etc. use braces {} to define hence differentiate a block of code.

Indentation

But Python is different and uses indentation to define a block of code. Indentation is basically the distance from the very beginning of the line in term of whitespaces. You can choose as many whitespaces as you wish, but that should be consistent throughout your code. Generally, people use 4 white-spaces and most of the code-editors come with a preset of 4 white-spaces on the press of the tab button (but that setting is editable). So, basically, the statements which are at an equal distance from the beginning of the line will be part of one block. e.g.

This belongs to block 1
    This belongs to block 2
        This belongs to block 3
    This belongs to block 2
This belongs to block 1
        This belongs to block 3

Indentation is otherwise used in many languages for code-readability but in Python in addition to making the code more readable, it defines a block of code too.

In addition to indentation, all the conditional statements, loops, functions and other structure introducing blocks ends with a colon followed by indentation:-

while true:
    print("yes")

You must practice indentation and pay special attention to it because in the beginning one error which you will get the most will be IndentationError:-

IndentationError: unindent does not match any outer indentation level

Note

Most of the code-editors and even IDLE are programmed to auto-indent your code i.e. if you will write a conditional statement followed by a colon and hit return/enter, it will auto-indent the next line for you.

Table of Contents

Chapter 4 - Print function

Chapter 6 - Data Types & Variables



Related Posts

Python unittest module - How to test your python code?
By Udit Vashisht | 1 month, 2 weeks ago

Python unittest module

Why to unit test your python source code?

All programmers want their code to be impeccable, but as the saying goes, to err is human, we make mistakes and leave bugs in our source code. Here is where the unit testing comes to our rescue. If you ...

Read More
Chapter 7- Numbers, Conversions and Operators
By Udit Vashisht | 8 months ago

In the previous chapter, we learned about various data types. From this chapter onward, we will learn in detail about each data type.

Numbers

This data type stores the numeric values and is immutable. For the beginners, it is sufficient to understand that immutable objects are those which can not ...

Read More
Matplotlib Subplot in Python | Matplotlib Tutorial | Chapter 10
By Udit Vashisht | 2 months ago

Matplotlib Subplot in Python | Chapter 10

In this Matplotlib Subplot tutorial, we will be learning to create Matplotlib Subplots. Till now, we have been using matplotlib.pyplot() to create the plots. But now, we will be using matplotlib.pyplot.subplots() (ptl.subplots()) to create Matplotlib Subplots.

Matplotlib.pyplot() or plt was automatically ...

Read More
Search