Section 2: Python Programming Fundamentals

Last update: May 27th 2025
  • Python is an accessible and exciting language for programming beginners.
  • Variables are containers for storing data, such as numbers and text.
  • Control structures allow you to make decisions and repeat actions in your code.
  • the loops while y for facilitate iteration over data sets.
Section 2: Python Programming Fundamentals

Welcome to the world of Python, where programming becomes exciting and accessible! In this introductory section, we'll explore the Python programming fundamentals that will get you started as a programmer. From creating variables and data types to using operators to perform calculations and make decisions, we've laid the groundwork for you to feel comfortable writing code in Python.

In addition, we will cover control structures if, else, elif, while y for, which will give you the power to control the flow of your program. Get ready to dive into an exciting learning journey as you delve into the magic of Python and discover all that this fascinating language has to offer!

Home Page of the Python Course from Scratch

Return to section 1

2.1 Variables and Data Types

Introduction to variables and their use in Python

In Python, variables are containers used to store data. This data can be numbers, text, lists, or other types of information. Variables allow programmers to access and manipulate data efficiently, which is essential in any program.

To define a variable in Python, you simply assign a value to a variable name using the assignment operator =. For example: uterine

name = "John"
age = 25
product_price = 19.99

In the above example, we have created three variables: nombre, edad y precio_producto. The variable nombre contains a text string, edad contains an integer and precio_producto contains a floating point number.

  Moment.js: Complete explanation and practical examples of the date library

Description of basic data types: integer, float, string, boolean, etc.

Python is a dynamically typed language , which means you don't need to explicitly declare a variable's type. The data type is automatically inferred from the assigned value. Some of the basic data types in Python include:

  • Integer (int): Represents whole numbers without decimals, such as 1, -5, 100, etc.
  • Float: Represent numbers with decimals, such as 3.14, -2.5, 0.5, etc.
  • String (str): Represents text and is defined in single or double quotes, such as 'Hello', 'Python', '123', etc.
  • Boolean (bool): Represents values ​​of true or false, used to express logic and make decisions.
  • List: It is an ordered and modifiable collection of elements, which can be of different data types.
  • Tuple (tuple): It is similar to a list, but it is immutable, meaning that its elements cannot be modified after they are created.
  • Dictionary (dict): It is a data structure that stores key-value pairs, where each key is unique and is used to access its corresponding value.

2.2 Operators and Expressions

Explanation of arithmetic, comparison and logical operators in Python

In Python, operators are special symbols used to perform different operations on variables and values. The main types of operators in Python are:

  • Arithmetic operators: They allow you to perform mathematical operations, such as addition (+), subtraction (-), multiplication (*), division (/), module (%) and exponent (**).
  • Comparison operators: They allow you to compare two values ​​and return a boolean value True or False depending on the comparison. Some examples are (==), (!=), (<), (>), (<=) Y (>=).
  • Logical operators: They allow you to combine boolean expressions. The main logical operators are and, or y not.

Using operators in expressions to perform calculations and decision making

Expressions in Python are combinations of variables, values, and operators that are evaluated to produce a result. These expressions are used to perform calculations and make logical decisions in a program.

  History of Python: From its beginnings to today

Example of expressions with arithmetic operators:

x = 10
y = 5

sum = x + y
subtraction = x – y
multiplication = x * y
division = x / y
module = x % y

Example of expressions with comparison operators:

a = 8
b = 10

is_equal = a == b
not_equal = a != b
is_less_than = a < b is_greater_or_equal = a >= b

Example of expressions with logical operators:

it rains = True
it_is_cold = False

if_it_rains_and_it_is_cold = it rains and it is cold
if_it_rains_or_it_is_cold = it rains or it is cold
no_llueve = it's not raining

2.3 Control Structures

Conditions: if, else, elif

Control structures are fundamental to the execution flow of a program. In Python, the most common control structure is if, which allows decisions to be made based on boolean conditions.

age = 18

if age >= 18:
print("You are of legal age")
else:
print("You are a minor")

In this example, we use the structure if to check if the variable edad is greater than or equal to 18. If the condition is true, “You are of legal age” will be printed; otherwise, “You are a minor” will be printed.

We can also use elif (short for “else if”) to check multiple conditions in sequence:

grade = 75

if note >= 90:
print("Your grade is A")
elif note >= 80:
print("Your grade is B")
elif note >= 70:
print("Your grade is C")
else:
print("Your grade is F")

In this example, we check different ranges of grades and assign a corresponding grade based on the value of the variable nota.

Loops: while and for

Loops are another important way to control the flow of a program in Python. Two of the most commonly used loops are while y for.

Loop while It is executed while a specific condition is true:

counter = 0

while counter < 5: print(counter) counter += 1

  Programming language tutorials: a complete guide to getting started and progressing
This loop will print the numbers 0 through 4 to the console. The loop for is used to iterate over a sequence, such as a list or tuple:

fruits =

for fruit in fruits:
print(fruit)

This loop will print each item in the list frutas.

Using these structures to control the flow of a program

Control structures if, else, elif, while y for are powerful tools that allow programmers to make decisions and repeat actions based on different conditions. With these structures, you can create more dynamic and flexible programs, capable of adapting to different situations and manipulating data effectively.

In summary, in this section you have learned about variables and data types in Python, as well as operators and expressions that allow you to perform calculations and make decisions in your code. You have also explored control structures if, else, elif, while y for, which are fundamental to controlling the flow of a program. These concepts will give you a solid foundation for tackling more complex problems and developing more advanced programs in Python. Go ahead and continue exploring the wonders of this powerful programming language!

Programming Fundamentals
Related articles:
Programming Fundamentals: Basic Concepts

Go to section 3: Functions and Modularity in Python