Language: EN

python-indentacion-comentarios

Basic Python Syntax

The code in Python is divided into lines, which we call statements. A statement is an instruction that the Python interpreter can execute, such as assignments, expressions, flow control statements, etc.

Python is an interpreted language, which means that its code will be executed line by line through an interpreter.

An example of a statement would be like this

x = 5

In this example, x = 5 is an assignment statement that assigns the value 5 to the variable x.

Each statement will contain operations, control structures, data manipulation. We will be seeing each point in the following articles.

Indentation in Python

In Python, indentation is used to define the structure and code blocks. Unlike other languages that use braces {} to delimit blocks, Python uses indentation.

Indentation consists of inserting spaces or tabs at the beginning of code lines to indicate the belonging of one block to another.

There are different conventions for adding indentation. Probably the most common convention in Python is to use 4 spaces for each level of indentation.

In the end, one or the other is indifferent. What is important is to maintain consistency in the indentation of your code (that is, always using the same format).

Example of indentation in Python

Let’s see an example illustrating the use of indentation to delimit code blocks.

# Variable declaration
x = 5
y = 10

# Function definition
def sum(a, b):
    # Indented code block
    result = a + b
    return result

# Control structure
if x < y:
    # Indented code block
    print("x is less than y")
else:
    # Another indented code block
    print("y is less than x")

In this example, it can be seen how indentation spaces are used to delimit blocks both in a function and in an if conditional.

If you want to learn more
consult the Introduction to Programming Course read more

Comments in Python

Comments in Python are a way to add notes or explanations in the code. They are created using the # symbol and everything that follows after # on the same line is treated as a comment and will not be executed.

# This is a comment in Python

Comments are useful for making the code more understandable for other programmers or for yourself in the future.

If you want to learn more
consult the Introduction to Programming Course read more