Language: EN

python-variables

Variables in Python

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, strings, lists, dictionaries, among others).

Internally, the name of the variable acts as an “alias” to refer to a memory address in the computer, where we will store a value.

Variable Declaration

When assigning a value to a variable, Python reserves a 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 inferred automatically 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:

  • number which contains the value 10
  • name which contains the string "Luis"
  • list which contains a list [1, 2, 3].