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
कई अन्य भाषाओं की तरह, python भी एक block-structured भाषा है।
ब्लॉक
ब्लॉक मूल रूप से एक कोड स्क्रिप्ट में statements के एक group को कहते है। अपने आप में एक ब्लॉक में एक या ज्यादा ब्लॉक हो सकते है, ऐसे ब्लॉक को नेस्टेड ब्लॉक कहते है। अब, कोड के एक ब्लॉक को दूसरे से अलग करने के लिए, हमें किसी प्रकार के विभाजक या संकेत आदि की आवश्यकता होती है। प्रोग्रामिंग भाषाएँ जैसे C ++, Java आदि, ब्रेसिज़ {} का उपयोग करते हैं ताकि कोड के एक ब्लॉक को दूसरे से अलग कर सकें।
Indentation
लेकिन Python अलग है और कोड के एक ब्लॉक को परिभाषित करने के लिए Indentation का उपयोग करती है। Indentation मूल रूप से लाइन की शुरुआत से दूरी है जिसे की whitespaces से मापा जाता है। आप जितने चाहें उतने whitespaces चुन सकते हैं, लेकिन यह आपके पूरे कोड के lie एक जैसी यानि consistent होनी चाहिए। आमतौर पर, लोग 4 whitespaces का उपयोग करते हैं और अधिकांश कोड-एडिटर टैब बटन के दबाने पर 4 whitespaces डालने के लिए प्रीसेट होते है। (लेकिन यह सेटिंग editable है)। तो, मूल रूप से, statements जो लाइन की शुरुआत से एक समान दूरी पर हैं, एक ब्लॉक का हिस्सा होंगे। जैसे
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
बाकी programming भाषाओं में Indentation का इस्त्माल कोड को ज्यादा readable बनाने के लिए होता है लेकिन python में इसका इस्त्माल कोड को readable बनाने के साथ साथ एक कोड के ब्लॉक को define करने के लिए भी होता है।
अगर हम conditional statements, loops, functions लिखते है तो Indentation के साथ-साथ,हमे लाइन के एंड मे colon लगाना पड़ेगा ओर अगली लाइन मे indentation डालेंगे:-
while true:
print("yes")
आपको Indentation का अभ्यास करना चाहिए और उस पर विशेष ध्यान देना चाहिए क्योंकि शुरुआत में एक error जो आपको सबसे अधिक आएगी व IndentationError होगी: -
IndentationError: unindent does not match any outer indentation level
नोट
अधिकांश code-editors और यहां तक कि IDLE आपके कोड को ऑटो-इंडेंट करने के लिए प्रोग्राम किया होता है। यदि आप conditional स्टेटमेंट लिख के colon डाल के enter दबाओ गे तो यह आपके लिए अगली पंक्ति को ऑटो-इंडेंट करेगा।
Table of Contents
Chapter 4 - Print function
Chapter 6 - Data Types & Variables