Unlocking Indentation in Python: Essential Tips for Clarity in 2024

indentation in python

Let’s discuss what is indentation in python?

Indentation in Python

  • Python does not support explicit grouping of statements using { } like in C, C++, Java etc.
  • Indentation in python is the leading whitespace(spaces and tabs) at the beginning of the logical line.
  • Indentation in python is used to compute the level of the line for grouping of statements.
  • As per google, indentation in python or indent is an empty space at the beginning of a line to signal the start of new paragraph. Many computer programming language have adopted this technique to designate “paragraphs” or other logical blocks in the program.
  • Tabs in indentation are replaced by spaces (as per tab value defined, can set in option menu- Configure IDLE – font/tabs – indentation width).
  • Total spaces before first character determines the indentation of that line.
  • Maximum level of indentation in python may vary platform to platform.

How Python Understand Indents ?

  • The indentation levels of consecutive lines are used to generate INDENT and DEDENT using stack.
  • Initially before reading first line, single zero is pushed on the stack which is never popped.
  • Indentation in python have levels that are always in increasing order from bottom to top.
  • At the beginning of each logical line, its indentation level is compared to the top of the stack.
    1. If it is equal, no INDENT or DEDENT generated.
    2. If it is larger, it is pushed on the stack and one INDENT is generated.
    3. If it is smaller, all numbers on the stack that are larger are popped and DEDENT is generated.
  • At the end of the file, a DEDENT is generated for each number remaining on the stack which is larger than zero.
  • It generates error in case first line indented or unexpected or inconsistent indent occurs.

Indentation in Python Example:

a = 10
b = 5
c = 15

if a > b:
    if a > c:
        print(a, "is larger")
        print(c, "is larger")
    15 is larger

Keywords in Python

  • Python has reserved some words that programmer cannot use as any identifier i.e. name of variable or function.
  • Such reserved words are called keywords of language.
  • Python keywords (in ascending order) and their meaning tabulated below.
andA logical operator
asTo create an alias
assertFor debugging
breakTo break execution sequence and transfer control out of loop
classTo define a class
continueTo continue to the next iteration of loop
defTo define new function
delTo delete an object
elifUsed in if statement to indicate else if
elseUsed in if…else
exceptSpecifies what to do when an exception occurs
False Boolean value
FinallyUsed in exception handling
forTo create a loop
fromTo import specific part of a module
globalTo declare global variable
ifTo define conditional statement
importTo import a module
inChecks if value present in list, tuple etc.
isTo test if two variables are equal
lambdaTo create an anonymous function
NoneNull value
NonlocalTo declare non-local variable
notLogical operator
orLogical operator
passA null statement that does nothing
raise To raise an exception
returnReturns control and value to calling routine
True Boolean value
tryTo make try..except statement
whileTo create a while loop
withTo simplify exception handling
yieldTo end a function, return a generator

Escape Sequences in Python

  • \ is a special character called escape sequence character.
  • Python made following escape sequence characters available for output formatting.
Escape SequenceUse / Purpose
\New line (explicit line joining)
\\Prints single backslash
\’Prints single quote
\”Prints double quote
\aASCII audible Bell
\bASCII Backspace
\fASCII form feed
\nASCII line feed
\rASCII carriage return
\tASCII horizontal tab
\vASCII vertical tab
\000Character with octal value 000
\xhhCharacter with hexadecimal value hh
\uxxxxCharacter with 16-bit value xxxx
\uxxxxxxxxCharacter with 32-bit value xxxxxxxx

Example of Escape Sequences

one two three        

print("\\ is single slash")
\ is single slash

print("\' is single quote")
'is single quote

print("\" is double quote")
"is double quote



One     Two



Note that, (for above 2 example)
ASCII value of A is 65 and octal equivalent of 65 is 101
ASCII value of n and o are 110 and 111 respectively and their hex equivalent are 6E and 6F.

2 thoughts on “Unlocking Indentation in Python: Essential Tips for Clarity in 2024

Leave a Reply

Your email address will not be published. Required fields are marked *