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

Venv Python - A complete tutorial on Virtual Environments in Python
By Udit Vashisht

What is Virtual Environment in Python ?

Virtual Environment is a kind of a container which runs specific version of Python and its modules. And it is used in the cases where you are working on two different project which has different dependencies. Say one of your project uses Django ...

Read More
Getting Started with Python: A Comprehensive Guide to Learning Python Programming
By Udit Vashisht

Introduction

Python is a versatile and beginner-friendly programming language that has gained immense popularity due to its simplicity, readability, and extensive community support. Whether you’re an aspiring programmer or looking to expand your skill set, learning Python can be a rewarding journey. This article aims to provide a comprehensive guide ...

Read More
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
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