Open In App

Statement, Indentation and Comment in Python

Last Updated : 04 Oct, 2025
Comments
Improve
Suggest changes
374 Likes
Like
Report

Here, we will discuss Statements in Python, Indentation in Python, and Comments in Python. We will also discuss different rules and examples for Python Statement, Python Indentation, Python Comment, and the Difference Between 'Docstrings' and 'Multi-line Comments.

1. Statement in Python

A Python statement is an instruction that the Python interpreter can execute. There are different types of statements in Python language as Assignment statements, Conditional statements, Looping statements, etc.

Example:

Python
x = 10         # Assignment statement
y = 20
print(x + y)   # Function call statement

Output
30

Each line above is a statement.

There are several types of statements in Python, most of them are listed below:

1. Assignment Statements: store values in variables.

Python
name = "Python"
age = 30

2. Conditional Statements: make decisions in code.

Python
if age > 18:
    print("You are an adult")
else:
    print("You are a minor")

3. Looping Statements: repeat tasks.

Python
for i in range(3):
    print("Hello")

4. Import Statements: include external modules.

Python
import math
print(math.sqrt(16))

5. Exception Handling Statements: handle errors safely.

Python
try:
    x = 1 / 0
except ZeroDivisionError:
    print("Division by zero not allowed")

6. Flow Control Statements: break, continue, pass, and return.

7. Multi-line Statements

Some statements are too long to fit on one line. Python allows breaking them into multiple lines:

7.1 Using backslash (\)

Python
s = 1 + 2 + 3 + \
    4 + 5 + 6 + \
    7 + 8 + 9
print(s)

7.2 Using parentheses ()

Python
n = (1 * 2 * 3 +
     4 + 5 + 6)
print(n)

7.3 Using square brackets []

Python
footballers = [
    "Messi",
    "Neymar",
    "Suarez"
]
print(footballers)

7.4 Using braces {}

Python
numbers = {1, 2, 3,
           4, 5}
print(numbers)

2. Indentation in Python

In Python, indentation (spaces or tabs at the beginning of a line) is used to define blocks of code. This is different from most programming languages that use curly braces { } for grouping code.

Example:

Python
site = "gfg"

if site == "gfg":
    print("Logging on to GeeksforGeeks...")
else:
    print("Wrong site")

print("All set!")

Output
Logging on to GeeksforGeeks...
All set!

Rules of Indentation in Python

  • All statements inside a block must have the same indentation level.
  • Standard indentation in Python is 4 spaces (PEP 8 style guide).
  • Mixing tabs and spaces in indentation is not allowed.
  • Improper indentation will cause an IndentationError.

Example: Indentation in Loops

Python
j = 1
while j <= 5:
    print(j)     # inside loop
    j = j + 1

print("Done")    # outside loop

Output
1
2
3
4
5
Done

If we forget to indent properly, Python will throw an error.

3. Comments in Python

Comments are text in the code meant for humans, not the Python interpreter. They are ignored during execution.

They are useful for:

  • Explaining complex logic
  • Adding notes for future reference
  • Making code easier for others to understand

3.1 Single-line Comments

Start with # and continue until the end of the line.

Python
# This is a single-line comment
a, b = 5, 10
sum = a + b  # adding two numbers
print(sum)

Output
15

3.2 Multi-line Comments

Python doesn’t have official multi-line comments, but you can use:

Multiple # lines

Python
# This is line 1
# This is line 2
# This is line 3
print("Hello Python")

Triple quotes (""" or ''')

Python
"""
This is a multi-line comment
that spans across multiple lines
"""
print("GeeksforGeeks")

4. Docstrings in Python

Docstrings are documentation strings that look like multi-line comments but serve a special purpose: they explain how functions, classes, or modules work.

Example:

Python
def greet():
    """This function prints a greeting message"""
    print("Hello, World!")

greet()

print(greet.__doc__)  # Access the docstring

Output
Hello, World!
This function prints a greeting message

Difference Between Docstrings and Multi-line Comments

Feature

Docstrings

Multi-line Comments

Purpose

Document functions/classes/modules

Explain logic or code

Placement

Inside functions, classes, modules

Anywhere in the code

Syntax

Triple quotes (""")

# or triple quotes

Interpreter Use

Stored as .__doc__

Completely ignored


Article Tags :

Explore