This article focuses on describing the basic data types of Python: what is a data type, what are the basic data types, and what are their main characteristics and properties.

**What are the data types?**

Data types are handled in any high-level programming language. Data types define a set of values that have certain characteristics and properties.

Let’s think for a moment when we were young, and we were in college in math class. You probably took a class in which they taught you the different sets of numbers. The natural ones (1, 2, 3, 4,…), the integers (…, -2, -1, 0, 1, 2,…), the real ones (… -1.1, -0.3, 2.1,…), etc. . Well, in programming (and of course in Python), each of those sets would be what we call a data type.

In Python, **every value that can be assigned to a variable has a data type associated with it**. As I’ve already mentioned to you, in Python everything is an object. So the data types would be the classes (where the properties are defined and what can be done with them) and the variables would be the instances (objects) of the data types. Don’t worry if you don’t understand what a class or an object is; we’ll cover that in another post.

In short, **a data type establishes what values ********a variable can take and what operations can be performed on it**.

**Python basic data types**

What types of data does Python bring with it?

In Python, we can find different types of data with different characteristics and classifications. This post will go over the basic data types, although I will introduce you to other types of data that we will see in later posts.

The basic Python data types are *Boolean, numeric* (integer, floating point, and complex), and *character strings*.

Python defines other data types as well, including:

- Sequences: The
*list, tuple*and*range*types - Maps: The type
*dict* - Sets: The
*set*type - Iterators
- Lessons
- Instances
- Exceptions

In turn, the above types can be grouped in different ways. For example, the type string is an *immutable sequence*; the *lists, tuples* and *dictionaries*, among others, are *containers* and *collections*, etc. But we will not see this here.

Anyway, don’t get overwhelmed with so much type or so much new concept. Take it easy; you are learning. Let’s start easy by reviewing the basic Python data types.

**Numeric types**

Python defines three basic numeric data types: *integers, floating point numbers* (it would simulate the set of real numbers, but we will see that this is not the case at all), and *complex* numbers.

*Integer numbers*

The type of integers is ** int**. This data type comprises the set of all integers, but since that set is infinite, in Python, the set is really limited by the available memory capacity. There is no representation limit imposed by language.

But rest assured that for 99% of the programs you develop, you will have enough with the subset that you can represent.

A type number ** int** is created from a literal that represents an integer or as the result of an expression or a function call.

Examples:

>>> a = -1 # a is of type int and its value is -1

>>> b = a + 2 # b is of type int and its value is 1

>>> print (b)

1

We can also represent whole numbers in *binary, octal,* or *hexadecimal* format.

*Octal* numbers are created by preceding ** the 0oa** sequence of octal digits (0 through 7).

To create a whole number in *hexadecimal*, you must prepend ** 0x** a sequence of digits in hexadecimal (from 0 to 9 and from A to F).

For numbers in *binary*, it is prepended ** 0b** to a sequence of digits in binary (0 and 1).

>>> ten = 10

>>> binary_ten = 0b1010

>>> ten_octal = 0o12

>>> ten_hex = 0xa

>>> print (ten)

10

>>> print (ten_binary)

10

>>> print (ten_octal)

10

>>> print (ten_hex)

10

**Floating point numbers**

Well, well, well, we got into hot topic and I don’t really know how to explain this to make it easy to understand.

Do you remember that I told you that *floating point numbers represented, more or less,* the set of real numbers?

We are going to do an experiment that will make you checkered. Open a terminal and run the command ** python3** to launch the Python interpreter. Then enter the expression

**and see what the result is.**

*1.1 + 2.2*>>> 1.1 + 2.2

3.3000000000000003

Really what is that 3 at the end?

*Representation of floating-point numbers*

*Representation of floating-point numbers*

We have to go over a bit of theory that I am going to try to simplify because I want to provide a full explanation in this article.

As with whole numbers, real numbers are infinite and, therefore, it is impossible to represent the entire set of real numbers with a computer.

**To represent** as many **real numbers as** possible with memory limitations (32- and 64-bit word sizes), **the scientific notation for the representation of real numbers was adapted to the binary system** (which is the system used in programming to represent data and instructions).

In this scientific notation, the numbers are represented like this:

What a fabric, right? But it is a very good solution that has survived to this day.

The case is that the sum of the binary floating point representation of the number 1,1 and the binary floating point representation of the number 2,2, give as a result 3.3000000000000003

But there are more cases, such as the representation of the number 1/3. At some point, the computer has to truncate the resulting periodic number.

The final explanation is that floating point numbers are interpreted as base 2 (binary) fractions in computer hardware. And the problem is that most of the decimal fractions can not be precisely interpreted as binary fractions because they have infinite decimal numbers. As a result, the floating-point decimal number you use in the programs is usually an estimate of the binary floating point numbers currently installed on the computer.

**Floating Point Numbers in Python**

Well, once you’ve seen this simplified introduction to *floating point numbers*, I’ll tell you what this type of data in Python is float.

You can use the type ** float** without problems to represent any real number (always keeping in mind that it is as accurate an approximation as possible). Therefore, for lengths, weights, frequencies, …, in which 3.3 is practically the same as 3.3000000000000003, the type

**is the most appropriate.**

*float*When a number is ** a float** and going to be used by a person, instead of by the computer, you can format the number as follows:

>>> real = 1 . 1 + 2 . 2 # real is a float

>>> print (real)

3.3000000000000003 # Approximate representation of 3.3

>>> print (f ‘{real: .2f}’)

3.30 # real showing only 2 decimal places

Like integers, a ** float** is created from a literal, or as the result of an expression or a function.

>>> un_real = 1 . 1 # The literal must include the character.

>>> otro_real = 1 / 2 # 1/2 The result is a float

>>> not_cient = 1.23E3 # float with scientific notation (1230.0)

And to finish this section, I anticipate that, if for whatever reason you do need greater precision when working with real numbers, Python has other data types, such as *Decimal*.

The Decimal type is ideal when working, for example, with money or interest rates. This data type truncates the decimal part of the number to be more precise, but it is not the point of this article to talk about the *Decimal* data type.