Page 491 - ComputerScience_Class_11
P. 491
12.2 INDENTATION IN PYTHON
In Python, indentation refers to the spaces or tabs used at the beginning of a line of code to define the structure
of the program. It is crucial in Python because it indicates which statements belong to which block of code. Unlike
other programming languages that use curly braces {} to define blocks, Python uses indentation to group statements
together.
12.2.1 Importance of Indentation
The importance of indentation is mentioned below:
• Defining code blocks: Indentation is used to define the boundaries of code blocks that belong together. For example,
in conditional statements (if, elif, else), loops (for, while) and functions, the indented lines after the statement are
considered part of that block.
• Readability and structure: Indentation helps to make the code more readable. It allows a programmer to visually
identify which statements are part of the same logical block. This also helps when debugging the code, as the
program structure is clearer.
12.2.2 Rules for Using Indentation
The rules to use indentation in Python programming are as follows:
• Use 4 spaces for indentation (as per PEP 8 guidelines).
• Consistently use either spaces or tabs for indentation, but not both.
• All statements within a block (e.g., inside if, for, while, def) must be indented at the same level.
• The first line after a control structure or function definition must be indented.
• Nested blocks should have an additional level of indentation.
Forgetting indentation or mixing spaces and tabs results in an IndentationError.
12.3 TYPES OF FLOW OF CONTROL
Flow of control is essential for managing how a Python program executes. There are three types of flow of control as
given below:
• Sequential flow of control
• Conditional flow of control
• Iterative statements
12.3.1 Sequential Flow of Control
In Python, sequential flow of control refers to the default order in which instructions in a program
are executed. This is the most basic type of flow of control, where statements are executed one Statement 1
after another, from the top of the program to the bottom, in the order they appear.
The characteristics of sequential flow are mentioned below: Statement 2
• Default execution order: In the absence of conditional statements (like if) or loops (like for
and while), the program executes each line of code one by one.
Statement 3
• No branching or repetition: There are no decisions to be made or tasks to repeat. Each line
is executed in a straightforward manner.
Flow of Control 489

