Variables are containers that allow us to store and manipulate values in our programs. This value can be of any data type in Python (such as numbers, text strings, lists, dictionaries, among others).
Internally, the variable name acts as an “alias” to refer to a memory address in the computer, where we will store a value.
If you want to learn more, check out the Introduction to Programming Course
Variable Declaration
When assigning a value to a variable, Python reserves space in memory to store this value.
In Python, it is not necessary to explicitly declare the type of a variable before assigning it a value (the type is automatically inferred based on the value we assign).
Therefore, to create a variable in Python, we simply assign a value using the = operator.
# Assigning values to variables
number = 10
name = "Luis"
list = [1, 2, 3]
# Printing the values of the variables
print("Number:", number) # Output: Number: 10
print("Name:", name) # Output: Name: Luis
print("List:", list) # Output: List: [1, 2, 3]
In this example, we have created three variables:
numberwhich contains the value10namewhich contains the string"Luis"listwhich contains a list[1, 2, 3].
