Python Syntax & Comments

22 Mar 2026, Updated: 13 Jul 2026 4 min read
0
In Python, the syntax is designed to be simple and human-friendly, which makes it one of the easiest languages for beginners to learn.

This article will guide you through the structure of a Python program, explain how indentation and line breaks work, and show how to use comments effectively.

Structure of a Python Program

A typical Python program is made up of one or more lines of code that perform specific tasks. Each Python file is known as a script and usually follows this structure:
# Step 1: Comments and Documentation
# This program prints a simple message


# Step 2: Import Statements import math

# Step 3: Function Definition def greet(): print("Hello, Python Learner!")

# Step 4: Main Program Execution if __name__ == "__main__": greet()

Key Points:

1. Comments help describe the purpose of the code.
2. Import statements allow you to use external or built-in modules.
3. Functions help organize and reuse code.
4. The condition if __name__ == "__main__": ensures that the code inside it runs only when the script is executed directly.
5. This simple structure gives your Python program a logical flow that's easy to understand and maintain.

Indentation in Python

Unlike many programming languages that use braces {} or keywords to define blocks, Python uses indentation (spaces or tabs) to indicate code blocks.
if True:
    print("This is indented correctly.")
    print("Python uses indentation to define blocks.")
In the example above, both print() statements are part of the if block because they are indented equally.

If indentation is missing or inconsistent, Python will throw an error:
if True:
print("Missing indentation!")   # ❌ IndentationError

Recommended Indentation Rule:

- Use 4 spaces per indentation level (PEP 8 standard).
- Avoid mixing tabs and spaces. Stick to one method.

Proper indentation not only avoids errors but also improves code readability β€” a core principle of Python syntax.

Line Breaks in Python

In Python, each line typically represents a single statement. You can, however, split long lines or combine short ones using special symbols.

1. Implicit Line Continuation

You can write long expressions inside parentheses (), brackets [], or braces {} without using any special symbol:
numbers = [
    1, 2, 3, 4, 5,
    6, 7, 8, 9, 10
]

2. Explicit Line Continuation

Use a backslash \ to break a line manually:
total = 10 + 20 + 30 + \
        40 + 50

3. Multiple Statements on One Line

Use a semicolon ; to write multiple short statements on the same line (though not recommended):
a = 10; b = 20; print(a + b)
Keeping lines concise and readable is a good practice for maintaining clean Python code.

Comments in Python

Comments are non-executable lines that help explain your code. They are ignored by the Python interpreter but are essential for documentation and readability.

1. Single-Line Comments

Use the hash symbol # at the beginning of the line:
# This is a single-line comment
print("Hello, Python!")  # Inline comment

2. Multi-Line Comments

Although Python doesn’t have a built-in multi-line comment syntax, you can use triple quotes (''' or """) to create documentation-style comments:
"""
This is a multi-line comment.
It can span across several lines.
Useful for explaining complex code.
"""
print("Multi-line comment example")

3. Docstrings (Documentation Strings)

Docstrings are special multi-line comments used to describe functions, classes, and modules. They can be accessed at runtime using the .__doc__ attribute.
def add(a, b):
    """This function returns the sum of two numbers."""
    return a + b

print(add.__doc__)
Output:
This function returns the sum of two numbers.
Using proper comments and docstrings helps others (and your future self) understand your code better.

Summary

Python's focus on clean syntax, readable indentation, and meaningful comments makes it one of the most elegant programming languages.

In the next article, we'll explore Variables and Data Types in Python β€” the foundation of storing and manipulating data effectively.
Nagesh Chauhan

Nagesh Chauhan

Principal Software Engineer β€’ Java β€’ Python β€’ Distributed Systems β€’ AI/ML

Principal Software Engineer with 14+ years of experience designing and delivering large-scale distributed systems, cloud-native applications, and AI-powered platforms.

Passionate about solving complex engineering problems using strong data structures and algorithms, along with expertise in Java, Spring Boot, Python, System Design, Microservices, Cloud, Kafka, Elasticsearch, and Generative AI.

Share this Article

πŸ’¬ Comments

Join the Discussion