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:
1234567value1 = 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 start coding
You're working on an application that receives readings from three different sensors: temperature, altitude, and pressure. All sensors return float values, but your database only accepts integers.
- Use the
int()
function to convert each reading into an integer.- Convert
temperature_celsius
intointeger1
. - Convert altitude_meters into integer2.
- Convert
pressure_pascal
intointeger3
.
- Convert
- Each resulting variable must be of type
int
. - Store the final results in the variables
integer1
,integer2
, andinteger3
.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.03Awesome!
Completion rate improved to 3.03
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:
1234567value1 = 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 start coding
You're working on an application that receives readings from three different sensors: temperature, altitude, and pressure. All sensors return float values, but your database only accepts integers.
- Use the
int()
function to convert each reading into an integer.- Convert
temperature_celsius
intointeger1
. - Convert altitude_meters into integer2.
- Convert
pressure_pascal
intointeger3
.
- Convert
- Each resulting variable must be of type
int
. - Store the final results in the variables
integer1
,integer2
, andinteger3
.
Solution
Thanks for your feedback!
single
Awesome!
Completion rate improved to 3.03
Convert Types
Swipe to show menu
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:
1234567value1 = 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 start coding
You're working on an application that receives readings from three different sensors: temperature, altitude, and pressure. All sensors return float values, but your database only accepts integers.
- Use the
int()
function to convert each reading into an integer.- Convert
temperature_celsius
intointeger1
. - Convert altitude_meters into integer2.
- Convert
pressure_pascal
intointeger3
.
- Convert
- Each resulting variable must be of type
int
. - Store the final results in the variables
integer1
,integer2
, andinteger3
.
Solution
Thanks for your feedback!