Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Python

Python Statements, Indentation, and Comments: A Clear Guide

Python Statements, Indentation, and Comments

This guide explains Python statements, the importance of consistent indentation, and how to use comments and docstrings effectively.

Python Statements

Python instructions that the interpreter executes are called statements. Common examples include assignment statements like a = 1, control flow statements such as if, for, and while. These fundamentals form the building blocks of every Python program.

Multi-line Statements

By default, a newline terminates a statement. To split a long statement across multiple lines, you can use the backslash (\) line‑continuation character:

a = 1 + 2 + 3 + \
    4 + 5 + 6 + \
    7 + 8 + 9

More idiomatically, Python allows implicit continuation inside parentheses, brackets, or braces:

a = (1 + 2 + 3 +
     4 + 5 + 6 +
     7 + 8 + 9)

Similarly, list literals can span multiple lines:

colors = ['red',
          'blue',
          'green']

You can also place several simple statements on one line using semicolons:

a = 1; b = 2; c = 3

Python Indentation

Unlike languages that rely on braces, Python defines code blocks purely through indentation. A block begins after a colon and ends at the first line that is not indented relative to the block’s start.

While the language permits any consistent indentation level, the community standard is four spaces. Tabs should be avoided to prevent confusion.

for i in range(1, 11):
    print(i)
    if i == 5:
        break

Proper indentation not only enforces syntax but also makes code immediately readable and maintainable. For example:

if True:
    print('Hello')
    a = 5

versus the less readable single‑line form:

if True: print('Hello'); a = 5

Incorrect indentation triggers an IndentationError and stops execution.


Python Comments

Comments are vital for documenting intent and logic, especially when revisiting code months later. In Python, a comment starts with a hash (#) and extends to the line’s end. The interpreter ignores these lines entirely.

# This is a comment
# Print greeting
print('Hello')

Multi-line Comments

To span multiple lines, you can prefix each line with #:

# This is a long comment
# that continues over several
# lines

Alternatively, triple‑quoted strings can serve as block comments. When not assigned or used as a docstring, they are discarded by the interpreter, imposing no runtime cost:

"""This is a multi‑line comment
that spans several lines."""

Docstrings in Python

A docstring is a string literal that follows a function, class, or module definition. It documents the element’s purpose and is stored in its __doc__ attribute. Triple quotes are conventionally used:

def double(num):
    """Return twice the input value."""
    return 2 * num

You can access it programmatically:

print(double.__doc__)

Output:

Return twice the input value.

For more detailed guidelines, consult the official Python documentation on comments and docstrings.


Python

  1. Python Keywords and Identifiers: Mastering Reserved Words and Naming Conventions
  2. Python Namespaces & Variable Scope: Understanding Names, Bindings, and Scopes
  3. Mastering Python If…Else Statements
  4. Mastering Python Loop Control: break & continue
  5. Mastering the Python Pass Statement: A Practical Guide
  6. Mastering Directory and File Operations in Python
  7. Master Python Exception Handling: try, except, else, and finally Explained
  8. Python vs JavaScript: Key Differences, Features, and When to Choose Each
  9. Python vs Ruby: A Comprehensive Comparison of Features, Advantages, and Use Cases
  10. Master Python Loops: Control Flow & Repetition Techniques