python-tipos-de-datos

Data Types in Python

  • 3 min

Data types allow us to categorize and handle 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 data manipulations effectively.

If you want to learn more, check out the Introduction to Programming Course

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 statement, it evaluates whether a condition is true or false.

true_value = True
false_value = False
Copied!

Integers (int)

Integers (int) are 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
Copied!

Floating-point numbers (float)

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

floating_number = 3.14
Copied!

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!"
Copied!

Composite data types

Lists (list)

Lists in Python (list) are ordered and modifiable collections of elements. They can contain any type of data, 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]
Copied!

Tuples (tuple)

Tuples in Python (tuple) are ordered and immutable collections of elements. Like lists, they can contain any type of data. 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)
Copied!

Dictionaries (dict)

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

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

Sets (set)

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

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