Skip to content

Variables

A variable is a container that stores data within a program. Each variable has a name and an associated data type, which allows the program to access the stored information and manipulate it as needed.

Unlike other programming languages, Python automatically assigns the data type based on the value assigned to the variable. That is, the language interprets the type dynamically, which means it’s not mandatory to declare the variable type when creating it.

However, if desired, it’s possible to manually specify the type using type annotations, which can improve code readability and facilitate debugging, especially in large projects.

In Python there are different types of variables (data types), similar to those found in other programming languages. Some of the most common are:

Represents whole numbers, positive or negative, without decimal parts. They are commonly used for counting, calculations, or representing discrete numerical values.

Int.py
number = 12 # This variable contains an integer
number1 = 0 # This variable contains an integer
number2 = -4 # This variable contains an integer

Store real numbers with decimal parts. They are useful for representing more precise quantities, such as measurements, percentages, or results from division operations.

Float.py
number = 0.0 # This variable contains a float
number1 = -1.5 # This variable contains a float
number2 = 3.14 # This variable contains a float

Used to store text. A string can contain letters, numbers, and symbols. It’s enclosed in single ' or double " quotes.

Str.py
string = 'Hello World' # This variable contains a string
string1 = '123' # This variable contains a string
string2 = '$%&&/' # This variable contains a string

This data type can only have two values: True or False. It’s used to represent logical states and perform conditions.

Bool.py
value1 = True # This variable contains a boolean
value2 = False # This variable contains a boolean

A list is an ordered collection of elements, which can contain different data types (integers, strings, etc.). Lists are modifiable, meaning you can change their content after creating them.

List.py
strings = ['apple', 'banana', 'pear'] # contains a list of words
numbers = [1, 2, 3] # contains a list of numbers
mixed = [1, 2.3, 'banana', False,[1,2,3]] # contains a list of different types of values

Similar to a list, but you cannot modify its content once created. They are useful for representing data collections that should not change, such as coordinates or configurations.

Tuples.py
strings = ('red', 'green', 'blue') # contains a tuple of words
numbers = (1, 2, 3) # contains a tuple of numbers

Structures that store data in key: value pairs. They allow quick access to information using the key. They are very useful for representing objects with properties.

Ditc.py
# contains a dictionary
dict = {'name': 'Ana', 'age': 25}
# contains a dictionary
dict1 = {'city': 'Bogotá', 'country': 'Colombia'}

An unordered collection of unique elements (without duplicates). They are used for mathematical operations like unions, intersections, or to remove repeated values from a list.

Set.py
# contains a set
set = {'python', 'java', 'c++'}
# contains a set
set1 = {'city', 'country', 'language'}

Python allows assigning multiple variables in a single line (something that other languages don’t allow), which makes your code cleaner and more compact. This technique is especially useful when you want to initialize several variables at the same time.

example.py
# Assigning three variables at once
a, b, c = 1, 2, 3
print(a) # 1
print(b) # 2
print(c) # 3

In Python, variables can change type during program execution. This is because Python is a dynamically typed language, which means it’s not necessary to declare the variable type beforehand, and it can change according to the value assigned to it.

example.py
x = 10 # Initial value of X
x = 'Now I am text'# New value assigned to X