Conteúdo do Curso
Tipos de Dados em Python
Tipos de Dados em Python
Convert Types
At times, Python may automatically assign a data type to a variable, which might not align with our intended choice. Hence, it is advantageous to possess the capability to exert control over the data type selection.
For instance, we can turn a number into a string, change the type of numerical data, or even use whichever number as a boolean data type. In this chapter, we are going to take care of converting numerical data types.
First and foremost, take a look at the syntax of converting a number to the integer data type:
value1 = int(657.89) value2 = int(90e3) value3 = int("678") print(value1) print(value2) print(value3)
Note
It has a simple syntax,
int(number)
, but if we want to convert a string to an integer, this string should contain integer numbers in quotes, likeint("8990")
, notint("899.0")
.
Swipe to begin your solution
In this exercise, you'll practice converting float values to integers.
- Convert
variable1
to an integer and store the result ininteger1
- Convert
variable2
to an integer and store the result ininteger2
- Convert
variable3
to an integer and store the result ininteger3
Once converted, you’ll have three interesting numbers! At the end of this chapter, you'll learn why each of these numbers is considered a "perfect number." 🤔
Solução
Note
A perfect number is an integer that is the sum of its positive proper divisors: 6 = 1+2+3. Our courses don't just help develop in programming.😄
Obrigado pelo seu feedback!
Convert Types
At times, Python may automatically assign a data type to a variable, which might not align with our intended choice. Hence, it is advantageous to possess the capability to exert control over the data type selection.
For instance, we can turn a number into a string, change the type of numerical data, or even use whichever number as a boolean data type. In this chapter, we are going to take care of converting numerical data types.
First and foremost, take a look at the syntax of converting a number to the integer data type:
value1 = int(657.89) value2 = int(90e3) value3 = int("678") print(value1) print(value2) print(value3)
Note
It has a simple syntax,
int(number)
, but if we want to convert a string to an integer, this string should contain integer numbers in quotes, likeint("8990")
, notint("899.0")
.
Swipe to begin your solution
In this exercise, you'll practice converting float values to integers.
- Convert
variable1
to an integer and store the result ininteger1
- Convert
variable2
to an integer and store the result ininteger2
- Convert
variable3
to an integer and store the result ininteger3
Once converted, you’ll have three interesting numbers! At the end of this chapter, you'll learn why each of these numbers is considered a "perfect number." 🤔
Solução
Note
A perfect number is an integer that is the sum of its positive proper divisors: 6 = 1+2+3. Our courses don't just help develop in programming.😄
Obrigado pelo seu feedback!