Topic 6:- Python blocks?
What are Python Blocks?
A block in Python is a group of statements that are executed together. Blocks define the structure and scope of your program (what belongs where).
Every block starts with a header line (like if, for, def, or class) and is followed by an indented set of statements.
Common Types of Blocks in Python
-
Conditional Block — using
if,elif,else -
Loop Block — using
for,while -
Function Block — using
def -
Class Block — using
class
1. Conditional Block:
if, elif, and else to check conditions and control the flow of execution.![]() |
| Conditional Block |
Real-Life example:
Conditional blocks work like traffic signals:
-
🟢 If the light is green → Go
-
🟡 Else if the light is yellow → Slow down
-
🔴 Else → Stop
Syntex- if True:
print("This is an if block")
elif:
print("This is an elif block")
else: print("This is an else block")
Here, the indented part after if, elif or else is a block.
2. Loop Block:
A loop block in Python is a group of statements that executes repeatedly as long as a given condition is True. It helps in repeating tasks without writing the same code multiple times.
![]() |
| Loop Structure flowchart |
for i in range(n):
print("Inside loop:", n)
print("Outside loop")
Types of Loops in Python
for loop → used when you know how many times to repeatSyntex
Example
for i in range(5):
print("Iteration:", i)
Output: Iteration: 0 Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 42. while loop → used when you repeat until a condition becomes false.
Syntex
while condition:
# loop block
Example:
count = 1 while count <= 3:
print("Count =", count)
count += 1
The indented statement under the for loop belongs to the loop block.The last print is outside the block (no indentation).
Why Use Loop?
| Purpose | Explanation |
|---|---|
| Automation | Repeat tasks automatically |
| Efficiency | Avoid writing repetitive code |
| Data Processing | Iterate through lists, files, datasets |
| Dynamic Logic | Run code until a condition changes |
3. Function Block:
A Function Block in Python is a group of related statements that perform a specific task.
Functions make your program modular, organized, and reusable. A function is defined using the keyword def, followed by the function name and parentheses ().
Syntex: def greet():
print("Hello, world!")
print("Welcome to Python")
greet()
All statements inside the function (indented equally) form a function block.
4. Class Block:
class Student:
def display(self):
print("This is a class block")
obj = Student()
obj.display()
Here, both class Student: and the function inside it are nested blocks.
Important Rule — Indentation
Python uses indentation (spaces or tabs) to define blocks. Unlike other languages (C, Java), Python doesn’t use braces {}.
Example of indentation error:
if True:
print("Error!") # Incorrect — not indented
This will raise:
IndentationError: expected an indented block
Summary
| Concept | Description | Example Keyword |
|---|---|---|
| Block | Group of statements executed together | if, for, def, class |
| Defined by | Indentation (4 spaces recommended) | — |
| Purpose | Structure & scope of code | — |


Comments
Post a Comment