DigitalOcean Referral Badge
Udit Vashisht
Author: Udit Vashisht


Chapter 5- Indentation

Chapter 5- Indentation

  • 2 minutes read
  • 289 Views
Chapter 5- Indentation

    Table of Contents

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

Python Decorators - What are Python Decorators - A Complete Tutorial
By Udit Vashisht

What are Python Decorators ?

Python Decorators function is just a function that takes another function as an argument and add extra features or behaviour to another function without altering the source code of original function.

The python decorators take advantage of the fact that the functions in python are ...

Read More
Plot Time Series in Python | Matplotlib Tutorial | Chapter 8
By Udit Vashisht

Plot Time Series data in Python using Matplotlib

In this tutorial we will learn to create a scatter plot of time series data in Python using matplotlib.pyplot.plot_date(). We will use Pandas Dataframe to extract the time series data from a CSV file using pandas.read_csv().

The ...

Read More

Introduction to Python Language - Chapter 1
By Udit Vashisht

Introduction to Python

Curiosity and zeal to learn something new have probably brought you here. It doesn’t matter whether you are new to programming or switching from some other redundant or less useful language, you must be having a few very important questions in your mind. We will be answering ...

Read More
Search
Tags
tech tutorials automate python beautifulsoup web scrapping webscrapping bs4 Strip Python3 programming Pythonanywhere free Online Hosting hindi til github today i learned Windows Installations Installation Learn Python in Hindi Python Tutorials Beginners macos installation guide linux SaralGyaan Saral Gyaan json in python JSON to CSV Convert json to csv python in hindi convert json csv in python remove background python mini projects background removal remove.bg tweepy Django Django tutorials Django for beginners Django Free tutorials Proxy Models User Models AbstractUser UserModel convert json to csv python json to csv python Variables Python cheats Quick tips == and is f string in python f-strings pep-498 formatting in python python f string smtplib python send email with attachment python send email automated emails python python send email gmail automated email sending passwords secrets environment variables if name == main Matplotlib tutorial Matplotlib lists pandas Scatter Plot Time Series Data Live plots Matplotlib Subplots Matplotlib Candlesticks plots Tutorial Logging unittest testing python test Object Oriented Programming Python OOP Database Database Migration Python 3.8 Walrus Operator Data Analysis Pandas Dataframe Pandas Series Dataframe index pandas index python pandas tutorial python pandas python pandas dataframe python f-strings padding how to flatten a nested json nested json to csv json to csv python pandas Pandas Tutorial insert rows pandas pandas append list line charts line plots in python Django proxy user model django custom user model django user model matplotlib marker size pytplot legends scatter plot python pandas python virtual environment virtualenv venv python python venv virtual environment in python python decorators bioinformatics fastafiles Fasta python list append append raspberry pi editor cron crontab Cowin Cowin api python dictionary Python basics dictionary python list list ios development listview navigationview swiftui ios mvvm swift environmentobject property wrapper @State @Environm popup @State ios15 alert automation instagram instaloader texteditor youtubeshorts textfield multi-line star rating reusable swift selenium selenium driver requests-html youtube youtube shorts python automation python tutorial algo trading nifty 50 nifty50 stock list nifty50 telegram telegram bot dictionary in Python how to learn python learn python