Statements, Indentations, and Comments in Python

Statements, Indendations and Comments are the basic building blocks of a structured python program. Let’s see what these basic building blocks of python is in a nutshell and then we will dive deeper.

  1. Statement: In Python, a statement is a unit of code that performs a specific task or operation. Examples of statements include assignment statements, print statements, conditional statements, and loop statements. Statements are executed in the order in which they appear in the code, unless control structures like conditional statements and loop statements are used to change the order of execution.
  2. Indentation: In Python, indentation is used to indicate the structure of your code. Indentation is used to group statements together into blocks of code, which are used to define the scope of variables and functions and to control the flow of execution. Indentation is typically accomplished using spaces or tabs, and the number of spaces or tabs used for indentation must be consistent within a given block of code.
  3. Comment: In Python, comments are used to explain or clarify code. Comments are ignored by the Python interpreter when the code is executed, so they do not affect the behavior of the program. Comments are used to document code, disable code, or provide attribution. Comments are created by starting a line of code with the # character, and can also be created using multi-line comments enclosed in triple quotes (''' or """).

Python Statement

In Python, a statement is a unit of code that performs a specific task or operation. Examples of statements include:

  • Assignment statements, which assign a value to a variable:
x = 42
  • Print statements, which display information on the screen:
print("Hello, world!")
  • Conditional statements, which execute code based on a condition:
if x > 0:
    print("x is positive")
else:
    print("x is non-positive")
  • Loop statements, which repeat code until a certain condition is met:
while x > 0:
    print(x)
    x = x - 1

Statements are typically executed in the order in which they appear in the code. However, you can use control structures like conditional statements and loop statements to change the order of execution based on certain conditions.

Python Indentation

In Python, indentation is used to indicate the structure of your code. Specifically, indentation is used to group statements together into blocks of code. Blocks of code are used to define the scope of variables and functions, and to control the flow of execution.

In Python, indentation is accomplished using spaces or tabs. The number of spaces or tabs used for indentation can vary, but it must be consistent within a given block of code. Typically, four spaces are used for each level of indentation, although this can vary depending on personal preference or coding conventions.

For example, consider the following code:

if x > 0:
    print("x is positive")
    print("x is greater than zero")
else:
    print("x is non-positive")
    print("x is less than or equal to zero")

In this code, the statements inside the if and else blocks are indented by four spaces. This indicates that these statements belong to their respective blocks, and should be executed only if the condition is true or false.

Indentation is an important part of Python’s syntax, and it is used to enforce code readability and consistency. Improper indentation can lead to syntax errors or incorrect program behavior.

Python Comments

In Python, comments are used to explain or clarify code. Comments are ignored by the Python interpreter when the code is executed, so they do not affect the behavior of the program. Comments are used to:

  • Document code: Comments can be used to describe what a particular block of code does, or to provide context for a particular variable or function.
# This function calculates the sum of two numbers
def add_numbers(a, b):
    return a + b
  • Disable code: Comments can be used to temporarily disable a block of code, without actually deleting it. This can be useful for debugging, or for testing different parts of your program.
# This line of code is temporarily disabled
# x = 42
  • Provide attribution: Comments can be used to give credit to the original author of a piece of code, or to provide a link to additional resources.
# This code is based on an example from the Python documentation:
# https://docs.python.org/3/tutorial/controlflow.html#if-statements

In Python, comments are created by starting a line of code with the # character. Anything after the # character on that line is ignored by the Python interpreter. You can also use multi-line comments by enclosing the text in triple quotes (''' or """).

"""
This is a multi-line comment
that spans multiple lines of code
"""

In summary, statements are the lines of code that tell Python what to do, indentation is used to group statements into blocks of code, and comments are used to explain or disable parts of your code. Understanding these concepts is essential for writing clear, readable, and maintainable Python code.

The codes mentioned in this post can be downloaded from github.com. Share the post with someone you think can benefit from the information.

Leave a Comment

Share this