Language: EN

python-tipos-de-datos

Data Types in Python

Data types allow us to categorize and manage different values that we can assign to variables in our Python programs.

Each data type has specific characteristics and behaviors that help us perform operations and manipulate data effectively.

Basic Data Types

Booleans (bool)

Booleans (bool) are truth values that represent boolean logic. They can be True or False and are used in logical expressions and flow control. For example, in an if expression, it is evaluated whether a condition is true or false.

true_value = True
false_value = False

Integers (int)

Integers (int) are whole numbers without a decimal point, both positive and negative. They can be of any length and are used to represent whole quantities. Some examples of integers are 5, -10, 1000, etc.

integer_number = 10

Floating Point Numbers (float)

Floating point numbers (float) are numbers with a decimal point. They can represent both whole numbers and fractions. Some examples of floating point numbers are 3.14, -0.5, 2.71828, etc.

floating_number = 3.14

Strings (str)

Strings in Python (str) are sequences of characters enclosed in single (') or double (") quotes. They can contain letters, numbers, symbols, and spaces. Some examples of strings are "Hello", 'Python', "123", etc.

text_string = "Hello, world!"

Composite Data Types

Lists (list)

Lists in Python (list) are ordered and mutable collections of elements. They can contain any data type, including numbers, strings, nested lists, etc. The elements of a list are indexed and can be accessed and modified using indices.

my_list = [1, 2, 3, 4, 5]

Tuples (tuple)

Tuples in Python (tuple) are ordered and immutable collections of elements. Like lists, they can contain any data type. The main difference is that the elements of a tuple cannot be modified once the tuple has been created.

my_tuple = (10, 20, 30, 40, 50)

Dictionaries (dict)

Dictionaries in Python (dict) are unordered collections of key-value pairs. Each element in a dictionary has a unique key that is used to access the corresponding value. They are very useful for representing structured and related data.

my_dictionary = {"name": "Luis", "age": 30, "city": "Madrid"}

Sets (set)

Sets in Python (set) are unordered collections with no duplicate elements. They are used for mathematical set operations such as union, intersection, difference, etc.

my_set = {1, 2, 3, 4, 5}