Why is Indentation Important in Python?
Indentation in Python is a crucial syntax element that defines a code block
, which is a group unit of code execution.
Unlike other programming languages that use curly braces { }
or semicolon ;
to indicate the start and end of a code block, Python uses indentation to separate code blocks.
Typically, indentation is done using 4 spaces or a tab to delineate the code execution units.
In most programming languages, indentation is used to improve code readability; however, in Python, incorrect indentation results in a syntax error.
How is Indentation Used?
Indentation is used to define code blocks in Python. The following are some examples of how indentation is used.
1. Conditional Statements: Code blocks executed only when specific conditions are met
Python uses the if
and else
keywords to define conditional statements.
A Keyword is a term that has a special meaning within a program.
The if keyword defines the code block executed when the condition is true, and the else keyword defines the code block executed when the condition is false.
if condition: print("Condition is True") # Indentation else: print("Condition is False") # Indentation
2. Functions: Code blocks that perform specific tasks
Python defines functions that perform specific tasks using the def
keyword.
The code below defines a function called multiply, which returns the product of two numbers a and b using the return
keyword.
Unlike everyday arithmetic, multiplication in programming is represented by the asterisk symbol *
instead of the letter x
.
def multiply(a, b): # Define the function body with indentation result = a * b return result # Define the return value with indentation print(multiply(3, 5)) # Outputs 15 print(multiply(4, 6)) # Outputs 24
The function defined as multiply can be called multiple times within the program, as shown with multiply(3, 5)
and multiply(4, 6)
.
Using functions in this way reduces code duplication by allowing code reuse.
3. Loops: Code blocks that repeat under certain conditions
Loops are executed repeatedly while a specific condition is true and are defined using the for
or while
keywords.
for i in range(4): # Loops 4 times from 0 to 3 print(i) # Indent the block of code that repeats
-
for
is the keyword that defines a loop, whilei in range(4)
specifies the range of repetition. -
The indented code block beneath the colon (:) defines the code to execute for each loop iteration. In the example,
print(i)
outputs the value ofi
for each loop. -
i
is the loop's variable, incrementing sequentially from 0 to 3. Each loop iteration increasesi
by 1. -
range
is a function that generates a sequence of numbers;range(4)
generates numbers from 0 to 3. -
The loop in the example executes
print(i)
wheni
is 0, 1, 2, and 3, outputting 0, 1, 2, and 3.
Python Indentation Guidelines
-
Typically, 4 spaces or 1 tab are used for indentation.
-
All lines of code within the same block should be indented at the same level. In the code example below,
if
andelse
are at the same conditional level, and therefore, they share the same level of indentation. -
Nested code blocks receive additional indentation. For example, in the example code below, the
print
function inside theif
block is indented one more level.
def greet(name): # Indent to separate the function body # Conditional block executed if a name is provided if name: # Further indent code within the if block print("Name:", name) # Conditional block executed if no name is provided # Use the same level of indentation as if for else else: print("Hello.") greet("CodeFriends") # Outputs "Name: CodeFriends"
Which of the following statements is correct regarding indentation in Python?
Curly brackets { } are used to define code blocks.
Semicolons ; are used to define code blocks.
The code will not execute if the indentation is inconsistent.
You can execute code even if the indentation is inconsistent.
Guidelines
AI Tutor
Publish
Design
Upload
Notes
Favorites
Help
Code Editor
Execution Result