Skip to main content
मुख्य पृष्ठ
  • Tutorials
  • SEO
  • AI
    • How to write an AI prompt
    • GenAI Roadmap for Beginners
  • Crypto
  • Web
    • Sample resume
    • Resume builder
    • Download movies for free
    • Download paid sotware for free
  • Python
  • langchain
  • HTML
  • CSS
  • JavaScript
  • Algorithm
  • DSA
  • PHP
  • Docker

पग चिन्ह

  1. मुख्य पृष्ठ
  2. Python
  3. Self employed

Concept of Indentation in Python

Profile picture for user admin
द्वारा admin , 6 जनवरी, 2022

Indentation

Indentation is an empty space (whitespace) used at the beginning of any statement to start new paragraph.

 indentation should be consistent throughout your code.

Indents
There are two types of indents:


Horizontal indents: spaces at the left 2 or 4 spaces.


Vertical indents(hanging indent): empty lines for splitting code into logical blocks.

Indentation in Python is mandatory.

Indentation in other languages like C,C++,Java etc is just for readability but in python, the indentation is a mandatory concept that should be followed when writing a python code, otherwise, python interpreter will  thrown IndentationError.

 

Reason why indentation is important in python

because the indentation serves another purpose other than code readability 

  • To represent a block of code(Python treats the statements as a single block of code that  have the same indentation level.)
  •  Without indentation, Python interpreter will get confused and does not know which statement to execute next or which statement belongs to which block.
  • Indentation helps interpreter to understand the order in which each block/statement should be executed.

Python Indentation Rules

  • The first line of Python code can’t have indentation, it will throw IndentationError.
  • The best practice is to use 4 spaces for indentation.
  • If you have multiple lines inside the if block, all the lines will need to be indented with same level otherwise You will get indentation error
  • If there is colon at the end of a line, you will need to indent the next line/lines by ignoring your current level of indentation.
  • If there is no colon, you should not indent the starting of a line.
  • Python doesn't allow mixing tabs and spaces for indentation.

 

4-space or 2-space indentation

 

Indentation style

There are several styles .Pick a style that suits you, then use it consistently.

  1. Allman
  2. K&R(Kernighan & Ritchie Style)
  3. GNU
  4. Whitesmiths
  5. Horstmann
  6. Haskell
  7. Pico
  8. Ratliff
  9. Lisp

K&R(Kernighan & Ritchie Style) In K&R, single-line blocks do not have braces.

About text formats

सादा टेक्स्ट

  • No HTML tags allowed.
  • लाइन और पैराग्राफ स्वतः भंजन
  • Web page addresses and email addresses turn into links automatically.

Book navigation

  • Code Standards in Python
  • Installing Python
  • Python Classes
  • Python Conditional structure
  • Python Data Types
  • Python History
  • Python Loops
  • Python Variables
  • Python functions
  • Working with Files
  • Python Comments
    • About Python
    • Concept of Indentation in Python
    • Python Syntax
  • Configure Python with VS Code
RSS feed
Section
  • Home
  • Features
  • Pricing
  • FAQs
  • About
About
  • Advertise With Us
  • Disclaimer
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Sitemap
Section
  • Home
  • Features
  • Pricing
  • FAQs
  • About
Newsletter signup
The subscriber's email address.
Manage your newsletter subscriptions
Select the newsletter(s) to which you want to subscribe.
Stay informed - subscribe to our newsletter.
Manage existing

© 2022 - 2024 techbrushup.com. All rights reserved.