16 septiembre, 2024

Types of variables in Python | Bootcamps

If you have ever had contact with the world of programming, whether in Python or in any other area, you will have heard that there are different types of variables in Python, as well as in other languages ​​such as Java, C, C++ or JavaScript.

In this post we will see what these types of variables are in Python and how to use each of them, depending on what we need to create.

Types of variables in Python

Integers

These types of variables in Python correspond to the integer numbers. Within this type of data there are subdivisions. Let’s look at each of them in Python.

num_int = 12121 print (num_int)

12121

What we are doing in the first line is declaring our variable (num_int) and assigning a value to it (12121).

In the second line we are printing the value housed in the num_int variable. Print (print) is to display the data through the console.

num_int = -512 num_int

-512

You can also display the result without entering printas in this second case.

If we want to see the type of data we have, we use the command type:

print (type (num_int))

Floating – point number

floating point numbers, float Or simply decimals They are numbers that have remainders. Let’s see how these types of variables work in Python:

num_float = 25.12 print (type (num_float))

If we do not put any number before the floating point, the algorithm will automatically fill this empty space with a zero:

.9

0.9

We can also have negative decimal values:

-4.5616

-4.5616

Something very curious that happens with type variables float is that if we add 1 with 1 plus 2 with 2 and print the result, it will give us the following:

float_test = 1.1 + 2.2 print (float_test)

3.3000000000000003

This infinitesimal remainder is a rounding of operations in bytes. We can use rounding so that such a large number of decimals do not appear:

print (f’ {float_test: . 2f})

3.30

So what we have done is put the variable that we want to round, in this case it is the float_test, followed by the number of points that we want to show after the comma, which in our case is 2.

The f at the beginning, which encloses everything we are going to print in braces, is an f – string. These are used to write code within a text string.

We can put the number of points we want after the comma:

print (f’ {float_test: .4f}’)

3.3000

The maximum value for a number of the floating – point type is approximately 1.8×10308. When we exceed that value, Python indicates it.

1.8e308 #let’s remember that e is used for powers

decimal module

Python’s decimal module provides greater precision for truncating the decimal part of type variables float. This module is widely used when working with money in variable types in Python that are float.

Taking the following operation as an example: 0.1 + 0.1 + 0.1 – 0.3, it is logical to think that its result must be 0. Let’s check it:

0.1 + 0.1 + 0.1 – 0.3

5.551115123125783e-17

It gives us a slightly strange number that, if we check with the calculator, gives us ALMOST 0, but not exactly 0.

Boolean

Variable types in Python that are boolean They can have two values: true either false.

type (True)

bool

type (False)

bool

7 == 3

False

7 == 7

true

In the same way as equality, we could test inequalities: greater, lesser, different.

Greater than: > Less than: < Greater than or equal to: >= Less than or equal to: <= Inequality: != 7 >= 3

true

7 > 3

true

7 != 3

true

String

These types of variables in Python are basically a immutable sequence of characters:

txt_1 = ‘I’m Amanda’ print (txt_1)

I’m Amanda

txt_2 = ‘I’m Amanda’ print (txt_2)

I’m Amanda

We can check if txt_1 and txt_2 are equivalent even though one is written with double quotes and the other with single quotes:

txt_1 == txt_2

False

If we want to place quotes inside the text in these types of variables in Python, we must do it as follows:

txt_3 = ‘I’m Amanda’ txt_3

I’m Amanda

Whats Next?

If you want to learn more about the interesting discipline of Big Data, we have for you the Big Data, Artificial Intelligence & Machine Learning Full Stack Bootcamp, a high-intensity training in which you will learn about all the topics necessary to enter a full job market. of excellent job opportunities. In addition, you will be accompanied by the best professionals, who will guide your training process at all times. Don’t wait any longer to change your future and come in to request more information now!

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *