{"id":243,"date":"2023-05-09T11:21:50","date_gmt":"2023-05-09T11:21:50","guid":{"rendered":"http:\/\/askpython.com\/?p=243"},"modified":"2026-04-14T06:14:12","modified_gmt":"2026-04-14T06:14:12","slug":"python-indentation","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python\/python-indentation","title":{"rendered":"Indentation in Python (With Examples)"},"content":{"rendered":"\n<p>Python uses indentation \u2014 not braces or keywords \u2014 to group statements into logical code blocks. Every function, loop, class, and conditional in Python depends on consistent indentation to work correctly. Get it wrong and Python throws an <code>IndentationError<\/code> immediately. This guide covers the rules, common mistakes, and how to avoid indentation-related bugs in your code.<\/p>\n\n\n\n<p>Indentation in Python refers to the whitespace at the start of a line that tells Python which block a statement belongs to. Blocks are groups of code that run together \u2014 like the body of an <code>if<\/code> statement or the statements inside a <code>for<\/code> loop. Python requires you to indent those statements uniformly, typically using 4 spaces per level. This is different from languages like C, C++, and Java that use curly braces <code>{}<\/code> to define blocks. Let\u2019s walk through everything you need to know to use indentation correctly.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is Indentation in Python?<\/h2>\n\n\n\n<p>Indentation in Python refers to the whitespaces at the start of a line that indicate a block of code. Python uses indentation where other languages use braces <code>{ }<\/code>. When you write a function, loop, or conditional statement, the lines that \u201cbelong\u201d to it must be indented consistently.<\/p>\n\n\n\n<p>The leading whitespaces (spaces or tabs) at the start of a line determine the indentation level. You increase the indent level to open a new block and decrease it to close one. Python\u2019s official style guide (PEP 8) recommends using <strong>4 spaces per indentation level<\/strong>.<\/p>\n\n\n\n<p>Here\u2019s a complete example showing indentation in action:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndef foo():\n    print(&quot;Hi&quot;)\n\n    if True:\n        print(&quot;true&quot;)\n    else:\n        print(&quot;false&quot;)\n\nprint(&quot;Done&quot;)\n\n<\/pre><\/div>\n\n\n<figure class=\"wp-block-image aligncenter\"><img loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"336\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/06\/python-indentation.png\" alt=\"Python Indentation\" class=\"wp-image-244\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/06\/python-indentation.png 806w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/06\/python-indentation-300x125.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2019\/06\/python-indentation-768x320.png 768w\" sizes=\"auto, (max-width: 806px) 100vw, 806px\" \/><figcaption class=\"wp-element-caption\">Python Indentation<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Python vs Other Languages: A Quick Comparison<\/h2>\n\n\n\n<p>Most mainstream programming languages use braces <code>{ }<\/code> to define code blocks. Python takes a different approach \u2014 it uses indentation. Here\u2019s how they compare:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Feature<\/th><th>Python<\/th><th>C \/ C++ \/ Java \/ JavaScript<\/th><\/tr><\/thead><tbody><tr><td>Block delimiter<\/td><td>Indentation (spaces\/tabs)<\/td><td>Curly braces <code>{ }<\/code><\/td><\/tr><tr><td>Enforcement<\/td><td>Python interpreter requires it<\/td><td>Braces required, whitespace ignored<\/td><\/tr><tr><td>Style guide<\/td><td>PEP 8 recommends 4 spaces<\/td><td>Commonly 4 spaces or 2 spaces<\/td><\/tr><tr><td>Mixing tabs and spaces<\/td><td>Allowed but discouraged<\/td><td>Irrelevant (braces do the grouping)<\/td><\/tr><tr><td>Readability<\/td><td>Forced clean code structure<\/td><td>Flexible but easy to write messy code<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Python Indentation Rules<\/h2>\n\n\n\n<p>Follow these rules to write valid Python code:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>The first line of a Python file cannot have indentation \u2014 it will raise <code>IndentationError<\/code>.<\/li>\n\n\n\n<li>Never mix tabs and spaces. Use one or the other consistently throughout your file.<\/li>\n\n\n\n<li>PEP 8 recommends using 4 spaces per indentation level \u2014 stick to this for best results.<\/li>\n\n\n\n<li>All statements within the same block must have the same indentation level.<\/li>\n\n\n\n<li>You must indent the body of functions, loops, conditionals, classes, and context managers.<\/li>\n\n\n\n<li>Python raises <code>IndentationError: expected an indented block<\/code> if you leave a block empty without a <code>pass<\/code> statement.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Benefits of Indentation in Python<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Readability:<\/strong> Indentation makes the code hierarchy immediately visible \u2014 you can see at a glance which statements belong together.<\/li>\n\n\n\n<li><strong>No delimiters needed:<\/strong> Unlike C or Java, you don\u2019t need braces for basic block grouping \u2014 Python handles it through whitespace.<\/li>\n\n\n\n<li><strong>Consistency:<\/strong> All Python code follows the same indentation rules, making it easier to read and maintain.<\/li>\n\n\n\n<li><strong>Built-in beauty:<\/strong> Python\u2019s indentation requirement forces you to write well-structured, clean-looking code.<\/li>\n\n\n\n<li><strong>IDE support:<\/strong> Nearly every Python IDE and code editor auto-indents for you, so writing properly indented code is nearly effortless.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Disadvantages of Indentation in Python<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Copy-paste issues:<\/strong> Copying code from PDFs, Word documents, or poorly formatted websites can corrupt indentation and cause hard-to-debug errors.<\/li>\n\n\n\n<li><strong>Learning curve:<\/strong> Developers coming from brace-based languages need time to adjust to whitespace-based grouping.<\/li>\n\n\n\n<li><strong>Hidden errors:<\/strong> If a block\u2019s indentation gets accidentally broken in a large file, finding it can be tedious.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Indentation in a Python for Loop<\/h2>\n\n\n\n<p>All statements inside a <code>for<\/code> loop must be indented to belong to that loop\u2019s block. Here\u2019s an example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ndef print_numbers(x, y):\n    for i in range(x, y):\n        print(i)\n\n<\/pre><\/div>\n\n\n<p>The <code>print(i)<\/code> line is indented under the <code>for<\/code> loop, so it runs for each iteration. The function definition itself is at the top level (no indentation), and the <code>for<\/code> line is indented once inside the function.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Common IndentationError Examples<\/h2>\n\n\n\n<p>Here are the most frequent indentation errors and how to fix them.<\/p>\n\n\n\n<p><strong>Example 1: Indentation on the first line<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n&gt;&gt;&gt;     x = 10\n  File &quot;&quot;, line 1\n    x = 10\n    ^\nIndentationError: unexpected indent\n<\/pre><\/div>\n\n\n<p>The first line of a file or code block cannot be indented. Python throws <code>IndentationError: unexpected indent<\/code> because there\u2019s no preceding block to justify the indentation.<\/p>\n\n\n\n<p><strong>Example 2: Inconsistent indentation inside a block<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif True:\n    print(&quot;true&quot;)\n     print(&quot;Hi&quot;)   # extra space \u2014 inconsistent!\nelse:\n    print(&quot;false&quot;)\n<\/pre><\/div>\n\n\n<p>All lines inside an <code>if<\/code> block must use the same indentation level. The second <code>print(\"Hi\")<\/code> has an extra space, so Python raises an indentation error.<\/p>\n\n\n\n<p><strong>Example 3: Orphaned indentation (nothing to attach to)<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif True:\n    print(&quot;true&quot;)\nelse:\n    print(&quot;false&quot;)\n\n print(&quot;Done&quot;)   # indented but no block above it!\n<\/pre><\/div>\n\n\n<p>Python raises <code>IndentationError<\/code> here because the final <code>print(\"Done\")<\/code> is indented but has no parent block \u2014 it doesn\u2019t belong to anything above it.<\/p>\n\n\n\n<p><strong>Example 4: Missing indentation inside a block<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nif True:\nprint(&quot;true&quot;)   # missing indentation!\nelse:\nprint(&quot;false&quot;)  # missing indentation!\n<\/pre><\/div>\n\n\n<p>Python raises <code>IndentationError: expected an indented block<\/code> because the bodies of the <code>if<\/code> and <code>else<\/code> statements are not indented.<\/p>\n\n\n\n<p><strong>Fix for empty blocks:<\/strong> If you need a block that does nothing, use the <code>pass<\/code> statement:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif True:\n    pass   # do nothing\nelse:\n    print(&quot;false&quot;)\n\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions (FAQ)<\/h2>\n\n\n\n<p><strong>How many spaces should I use for Python indentation?<\/strong><\/p>\n\n\n\n<p>Use 4 spaces per indentation level, as recommended by PEP 8 \u2014 Python\u2019s official style guide. This is the most widely adopted convention in the Python community.<\/p>\n\n\n\n<p><strong>Can I use tabs instead of spaces in Python?<\/strong><\/p>\n\n\n\n<p>Yes, Python accepts tabs, but mixing tabs and spaces causes subtle bugs that are hard to debug. Stick to spaces for consistency across all editors and tools.<\/p>\n\n\n\n<p><strong>Why does Python use indentation instead of braces?<\/strong><\/p>\n\n\n\n<p>Guido van Rossum, Python\u2019s creator, chose indentation-based grouping because it enforces readable code by design. It removes the option to write messy, poorly structured code that compiles but is hard to maintain.<\/p>\n\n\n\n<p><strong>What is IndentationError in Python?<\/strong><\/p>\n\n\n\n<p><code>IndentationError<\/code> is raised when Python encounters a line with incorrect or unexpected indentation. Common causes include mixing tabs and spaces, leaving a block empty without <code>pass<\/code>, or inconsistent indentation within the same block.<\/p>\n\n\n\n<p><strong>How do I fix an unexpected indent error?<\/strong><\/p>\n\n\n\n<p>Check that the offending line doesn\u2019t have extra spaces or tabs at the beginning compared to surrounding lines. Use a code editor that shows hidden whitespace characters, or run <code>python -t<\/code> to get warnings about inconsistent tab\/space usage.<\/p>\n\n\n\n<p><strong>What is the pass statement used for in indentation?<\/strong><\/p>\n\n\n\n<p><code>pass<\/code> is a null operation used as a placeholder when a block can\u2019t be empty. For example, if you have an <code>if<\/code> statement that intentionally does nothing yet, write <code>pass<\/code> inside its body to avoid an <code>IndentationError<\/code>.<\/p>\n\n\n\n<p><strong>Does Python 3 require indentation?<\/strong><\/p>\n\n\n\n<p>Yes. In Python 3, all code blocks inside functions, loops, conditionals, classes, and context managers must be indented. Unlike some languages where it\u2019s optional, omitting indentation in Python is a syntax error.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p>Indentation in Python is not optional \u2014 it\u2019s a core part of the language syntax. It groups statements into blocks and determines the structure of your program. Use 4 consistent spaces per indentation level, never mix tabs and spaces, and always indent the body of functions, loops, and conditionals. If you need an intentionally empty block, use the <code>pass<\/code> statement. Following these rules will help you avoid <code>IndentationError<\/code> entirely and write clean, readable Python code.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Python Topics<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.askpython.com\/python\/python-if-else-elif-statement\">Python if-else Statement<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python\/python-for-loop\">Python for Loop<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python\/python-while-loop\">Python while Loop<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python\/python-functions\">Python Functions<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python\/python-classes-objects\">Python Classes and Objects<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python\/python-exception-handling\">Python Exception Handling<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.askpython.com\/python\/python-data-types\">Python Data Types<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Python uses indentation \u2014 not braces \u2014 to define code blocks. Learn Python indentation rules, common IndentationError examples, and best practices to write clean, bug-free code.<\/p>\n","protected":false},"author":3,"featured_media":50435,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/243","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=243"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/243\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/50435"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}