Fundamentals of Programming with Python
- Created: Sunday April 20, 2025
- Categories: guide
- Status:in development
Introduction to Programming
Every program can be understood as a system of inputs and outputs where each action generates a corresponding reaction. This process can be divided into three essential parts:
- 
Inputs: Inputs are the data or information provided to the program to perform an action. These inputs can come from various sources, such as a user interacting with a user interface, an online form, or a message sent during a chat. Inputs are essential for a program to perform specific tasks. 
 
- 
Process: The process represents the operations that the program performs based on the provided inputs. These actions are commonly represented as loading screens where the background process involves the core of our program. These can involve mathematical calculations, data manipulation, or decision-making. These processes are what make a program useful and functional. 
 
- 
Output: The output is the information or action that the program generates after processing the inputs. The results can be presented to the user in an interface, saved to a file, or used as input for subsequent actions. Outputs allow the user to get value from the actions performed by the program. 
An example in the context of video games can be represented as the interaction between the player and the character.
- The input is represented by pressing a button, where our program will process this press and send the corresponding action.
Hello World
"Hello, world" A simple example of how we can represent the principle of input, process, and output.
- 
Input: In this program, the input could be any string of characters we wish to display in the output.  
 
- 
Process: The main action of this program is to print the text "Hello, world!" on the screen.  
 
- 
Result (Output): The result or output of this program is the text "Hello, world!" which is displayed on the computer screen.  
Variables
Variables are like containers in which you can store different types of data, such as numbers, text, and other values.

Variable Declaration
To create a variable in Python, simply choose a descriptive name and assign a value using the assignment operator =.
pythonname = "John"
age = 25
bank_balance = 1000.50
Variable Names
Variable names in Python must follow a few rules:
- Variables must begin with a letter (a-z, A-Z) or an underscore (_).
 
Symbolic Names
Thanks to variables, we can assign representative names to our input values and use those names throughout the execution of our program.

Execution Sequence
In Python, code is executed sequentially, from top to bottom and line by line. This means that we must assign a value to a variable before we can use it later in the code.

pythonprint(greeting) ## NameError: name 'greeting' is not defined
greeting = "Hello World"
Variable Reassignment
Keeping the execution sequence in mind, in Python you can change the value stored in a variable by simply reassigning a new value to the variable name on a subsequent line.

In that case, the print() function will operate with the last assigned value: 30.
Data Types
Data types are essential for programming. Almost all, if not the vast majority, of programming languages share a series of essential data types that will help us interpret our ideas in the best possible way.
Text Strings (Strings)
Text strings, also known as Strings, are used to represent sequences of characters, such as words, phrases, or text in general.

Whole Numbers (Integers)
Integers are used to represent complete numerical values, such as 1, -5, or 1000, without including decimals or fractions.

Decimal Numbers (Floats)
Decimal numbers, or floating-point numbers, are used to represent numerical values that include decimals, such as 3.14, -0.5 or 2.0.

Logical Values (Booleans)
Boolean values can be used to perform logical representations or to represent the state of an application. These values are defined as 'true' or 'false'.

Operators
An operator is a symbol used to perform operations between different data types.
Arithmetic Operators
These operators are used to perform basic mathematical operations, such as addition (+), subtraction (-), multiplication (*), and division (/). Along with more complex operations like modulus (%), integer division (//), and exponentiation (**).
Numeric Operations
Numeric operations are operations that can only be performed between numeric data types. Performing addition between a number and a string will result in an error.
The sum of two numbers:
pythonprint(5 + 3) # 8
pythona = 5
b = 3
print(a + b) # 8
pythona = 5
b = 3
result = a + b
print(result) # 8
The difference between two numbers:
pythonprint(5 - 3) # 2
pythonprint(3 - 5) # -2
pythona = 5
b = 3
print(a - b) # 2
print(b - a) # -2
print(a - a) # 0
pythona = 5
b = 3
result = a - b
print(result) # 2
result = b - a
print(result) # -2
result = a - a
print(result) # 0
The product of two numbers:
pythonprint(5 * 3) # 15
pythonprint(5 * -3) # -15
pythona = 5
b = 3
print(a * b) # 15
pythona = 5
b = -3
print(a * b) # -15
pythona = 5
b = 3
print(a * -b) # -15
pythona = 5
b = 3
result = a * b
print(result) # 15
pythona = 5
b = -3
result = a * b
print(result) # -15
pythona = 5
b = 3
result = a * -b
print(result) # -15
The division of two numbers:
pythonprint(15 / 3) # 5.0
The quotient of a division consists of finding a number that, when multiplied by the divisor, gives the dividend as a result.
pythonprint(15 / 0) # ZeroDivisionError: division by zero
pythona = 15
b = 3
print(a / b) # 5.0
pythona = 7
b = 3
print(a / b) # 2.3333333333333335
pythona = 15
b = 0
print(b / a) # 0.0
pythona = 15
b = 3
result = a / b
print(result) # 5.0
pythona = 15
b = 3
result = a / a
print(result) # 1.0
pythona = 7
b = 3
result = b / a
print(result) # 0.42857142857142855
Used to find the remainder of a division.
pythonprint(3 % 15) # 3
pythonprint(15 % 0) # ZeroDivisionError: division by zero
pythona = 15
b = 3
print(a % b) # 0
pythona = 7
b = 3
print(a % b) # 1
pythona = 15
b = 0
print(b / a) # 0.0
pythona = 15
b = 3
result = a % b
print(result) # 0
pythona = 15
b = 3
result = a % a
print(result) # 0
pythona = 7
b = 3
result = b % a
print(result) # 3
Performs an integer division where the decimal part is completely omitted without any rounding.
pythonprint(15 // 2) # 7
pythonprint(15 // 0) # ZeroDivisionError: division by zero
pythona = 15
b = 3
print(a // b) # 5
pythona = 7
b = 3
print(a // b) # 2
pythona = 15
b = 0
print(a // b) # ZeroDivisionError: division by zero
pythona = 15
b = 3
result = a // b
print(result) # 5
pythona = 15
b = 3
result = a // a
print(result) # 1
pythona = 15
b = 3
result = b // a
print(result) # 0
Raises a number to the power of another.
pythonprint(5 ** 3) # 125
Raising a number to a negative power involves calculating the inverse (or reciprocal) of raising the number to the corresponding positive power.
pythonprint(5 ** -3) # 0.008
pythona = 5
b = 3
print(a ** b) # 125
pythona = 5
b = -3
print(a ** b) # 0.008
pythona = 5
b = 3
print(-a ** b) # -125
pythona = 5
b = 3
result = a ** b
print(result) # 125
pythona = 5
b = -3
result = a ** b
print(result) # 0.008
pythona = 5
b = 3
result = -a ** b
print(result) # -125
Character Strings
Since text is interpreted as a string of characters, concatenation is the union of two or more strings, where characters like space are also part of the string.
pythonname = "David"
sentence = "My name is " + name
pythonage = 28
sentence = "I am " + age + " years old"
pythonage = 28
sentence = "I am " + str(age) + " years old"
Assignment Process
In the previous examples, we used operations directly inside the print() function. However, how can we assign the result of an operation to a variable?
The process of assigning an operation's result to a variable and its subsequent display can be broken down into three fundamental steps:
- 
Evaluation: During evaluation, Python analyzes the type of operation being performed and calculates the corresponding result. 
 
- 
Assignment: The result obtained during evaluation is finally assigned to the corresponding variable. 
 
- 
Visualization: To display the result on the screen, the print() function is used along with the assigned variable. 
 

User Input
The ability to allow a user to enter data into our program is fundamental for assigning values to our variables and working with them.
Input
In Python, the most basic form of data entry is through the input() function. This function excellently represents the input, process, and output scheme in a program.
Input
Input is a function that takes a string as its input. This input will be displayed on the screen in the same way as the print() function.
Process
- When input() is called, the program stops and waits for the user to enter a line of text.
- The user enters a line of text and presses the "Enter" key.
- The input() function reads the entered line of text and returns it as a string of characters.
Output
- 
You can assign the value returned by input() to a variable to store and use the user's input in your program. 
 
- 
Once the value is assigned to a variable, we can use print() to display it on the screen. 
 
- We write the input() function with the input message: "Please, enter your name: ".
- 
This will print the message on the screen, followed by a space where the user can type their name and then press "Enter" to submit it. 
 
- 
When the message is sent, it is stored in the result variable. 
 
- 
Finally, we use the print() function to display the content of the result variable on the screen, which will allow us to see the name John. 
 

Basic Calculator
Now that we have understood the fundamental concepts related to variables and user inputs, let's proceed to create a basic calculator that adds two numbers provided by the user.
pythonnumber_a = input("Please, enter the first number: ")
number_b = input("Please, enter the second number: ")
print(number_a + number_b)
This program prompts the user to enter two numbers and uses the input function to capture them. During execution, in the first step, the user is asked to enter the first number and, once stored in the number_a variable, the program proceeds to request the second number to then assign it to the number_b variable. Once both numbers are stored in their respective variables, the print function will display their sum on the screen.
Let's assume our inputs are the numbers 1 and 1; our expected output would be the number 2. However, there is a fundamental problem in our code when we run it.
python# Output
'11' 
The reason our output shows the number 11 is because the input function takes what we enter as text and stores it as a string of characters. If we use the addition operator + on two text strings, they will be concatenated, which results in our two numbers being joined into a single text.
Type Coercion
Type coercion, in short, refers to the ability of a programming language to change one data type to another.
Integer (int)
The int function is used to convert a value into an integer. It can accept various types of values and produce an integer as a result.
Input
The int() function can accept various data types as input, but in this case, we will use numbers in string format (string) as input.
Process
The int() function takes the input value and will try to convert it into an integer value.
Output
- If the string does not represent a valid numeric value, the int() function will generate an error.
- If the string represents a valid numeric value, for example, "2023", the int() function will return the converted value in the form of an integer.
- 
We write the int() function with the input string: "2023". 
- 
This will convert the value from a string to an integer, allowing us to perform arithmetic operations with a value that initially only represented text. 
- 
The number is stored in the result variable. 
- 
Finally, we use the print() function to display the sum of result and the number 1, which allows us to see the number 2024. 

Corrected Basic Calculator
Now that we have a way to convert the string data provided by the input function into numeric data using the int function, we can incorporate this functionality into our program.
pythonnumber_a = input("Please, enter the first number: ")
number_b = input("Please, enter the second number: ")
print(number_a + number_b)
When analyzing our initial code, two important aspects stand out:
- 
After the user has entered the data, we get two variables, number_a and number_b, which contain string data. 
- 
It is crucial to ensure that number_a and number_b are of a numeric type before performing the operation. 
We can make use of variable reassignment to reset the value of number_a and number_b as integer values.
pythonnumber_a = input("Please, enter the first number: ")
number_b = input("Please, enter the second number: ")
number_a = int(number_a)
number_b = int(number_b)
print(number_a + number_b)
A more direct solution involves passing the output of input as the input to the int function.
pythonnumber_a = int(input("Please, enter the first number: "))
number_b = int(input("Please, enter the second number: "))
print(number_a + number_b)
Flow Control
This refers to a program's ability to make decisions and allows a program to "flow" through different paths or branches of execution according to certain conditions or predefined rules.
Conditional Statements
Conditional statements allow us to determine the execution flow of our program, dividing it into branches according to our decisions.
These decisions are based on logical conditions, which are statements that can be true or false.
The "if" Statement
The if statement is a conditional that allows the code to continue its execution if a specific condition is met.
pythonif condition:
    # Block of code that executes if the condition is true
pythonage = 20
if age >= 18:
  print("You are an adult.")
- We start by declaring a variable called age with a value of 20.
- Then, we use the if statement followed by a condition.
- The condition checks if the age variable is greater than or equal to 18 using the >= operator.
- If the condition is True, the program will print the message You are an adult. on the screen.
- Otherwise, if the condition is False, Python will skip that line and execution will continue without displaying any message on the screen.
Analysis:
When analyzing the code, we can identify several key points:
- 
Condition: Our condition involves comparing two numeric values, 20 and 18, using the comparison operator >=. 
 Since age is a number greater than or equal to 18, this condition will evaluate to a boolean value of True. 
- 
Syntax: To clearly communicate to Python that we want to start a block of code, it is essential to pay attention to two other key points to ensure error-free execution: 
 - To indicate the start of a branch or block of code, Python interprets the use of a colon : as the beginning of a branch.
- After the block is declared with the colon, we must use indentation to write the code for this branch. Writing code without this indentation will signal to Python that the code is part of the main branch or the main flow of the program.
 
- 
Comparison Operators: Unlike arithmetic operators, comparison operators aim to compare two values of any type and return a boolean value, either True or False. 
 - Equal to (==)
- Not equal to (!=)
- Greater than (>)
- Less than (<)
- Greater than or equal to (>=)
- Less than or equal to (<=)
 
- 
Activation of the conditional (if): It is important to note that all if really needs to execute a block of code is a boolean value of True. 
 - 
Writing True directly as a condition is completely valid and will cause that block of code to always execute. pythonif True: print("This code always executes!")
 
- 
The "else" Statement
The else statement can be understood as "if not". This conditional allows for the execution of a "default" branch. If the if condition is not true, the code will proceed to execute the else code without checking any condition, making it the default code to run if our previous condition turns out to be false.
pythonif condition:
  # Code 
else:
  # Code
pythonage = 17
if age >= 18:
  print("You are an adult.")
else:
  print("You are a minor.")
- The condition checks if the age variable is greater than or equal to 18 using the >= operator.
- If the condition is True, the program will print the message You are an adult. on the screen.
- Otherwise, if the condition is False, Python will hand over execution to the else statement and the program will print the message You are a minor.
The "elif" Statement
The elif statement can be understood as "else if". This conditional allows us to add extra conditions to our logical tree. In the previous example, we interpreted all numbers greater than 18 as adults and all other numbers outside that range as minors.
pythonage = 18
if age > 18:
  print("You are an adult.")
elif age == 18:
  print("You are exactly 18 years old.")
else:
  print("You are a minor.")
- 
Now the condition only checks if the age variable is greater than 18 using the > operator. 
- 
If the condition is True, the program will print the message You are an adult. on the screen. 
- 
Otherwise, if the condition is False, Python will hand over execution to the elif statement and the condition will now check if the age variable is equal to 18 using the == operator. 
- 
If this condition is True, the program will print the message You are exactly 18 years old. on the screen. 
- 
Otherwise, if this condition is also False, Python will hand over execution to the else statement and the program will print the message You are a minor. 


