0% found this document useful (0 votes)
4 views

Indentation

Indentation is essential in Python to define the structure of code blocks, replacing the use of braces found in other languages. It indicates the grouping of statements and must be consistent throughout a block to avoid IndentationError. Proper indentation ensures that all statements within a block are executed together, as demonstrated in the provided examples.

Uploaded by

ruemumbire9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Indentation

Indentation is essential in Python to define the structure of code blocks, replacing the use of braces found in other languages. It indicates the grouping of statements and must be consistent throughout a block to avoid IndentationError. Proper indentation ensures that all statements within a block are executed together, as demonstrated in the provided examples.

Uploaded by

ruemumbire9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

Indentation

Indentation is a fundamental aspect of Python’s syntax, and it is required to indicate


the structure of code blocks such as loops, conditionals, functions, and classes.
Unlike other programming languages that use braces ({}) to define blocks of code,
Python relies on whitespace indentation to denote scope. If used incorrectly, Python
will raise an IndentationError.

Purpose of Indentation

Indentation in Python is used to define the grouping of statements. It marks the


beginning and end of code blocks. A block is a group of statements that are executed
together. For instance, in a loop or function definition, all the indented code within
that block is executed as part of the same operation

For example:
if condition:
# This indented block belongs to the 'if' statement
print("Condition is True")
# More code here will be part of the 'if' block

Indentation Rules

Consistent Indentation: Indentation must be consistent. That is, all lines of code
within the same block must use the same number of spaces or tabs

Example:
if True:
print("True")
else:
print("False") # This is correctly indented.

Incorrect indentation:

You might also like