Udit Vashisht
Author: Udit Vashisht


Chapter 5- Indentation

Chapter 5- Indentation

  • 2 minutes read
  • 40 Views
Chapter 5- Indentation

What is indentation in Python?

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

Blocks of code in Python

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 in Python

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 terms of white-spaces. You can choose as many whitespaces as you wish, but that should be consistent throughout your code. Generally, programmers 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

Matplotlib Tutorial in Python | Chapter 2 | Extracting Data from CSVs and plotting Bar Charts.
By Udit Vashisht

Matplotlib Tutorial in Python

Chapter 2 | Extracting Data from CSVs and plotting Bar Charts

In the last chapter, we learned to draw simple plots in Matplotlib and further customizing it.In this chapter we will be learning to extract data from external sources like CSV and creating ...

Read More
Matplotlib Tutorial in Python | Chapter 1 | Introduction
By Udit Vashisht

Matplotlib Tutorial in Python

Chapter 1 | Introduction

In this series of Matplotlib Tutorials in Python, we will cover all concepts of matplotlib from beginners to expert. Starting with how to install Matplotlib to how to create matplotlib plots, this series is the only matplotlib tutorial which you will need. ...

Read More
Python Tutorial for Beginners
By Udit Vashisht

Python Tutorial for Beginners

Have you heard a lot about Python Language? Are you looking for free and reliable resource to learn Python? If Yes, your search for the Best Python Tutorial is over.

We are excited to bring an exhaustive Python tutorial for a complete beginner. Even ...

Read More
Search