Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Convert Types | Bring All the Topics Together
Data Types in Python

Swipe to show menu

book
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:

1234567
value1 = int(657.89) value2 = int(90e3) value3 = int("678") print(value1) print(value2) print(value3)
copy

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, like int("8990"), not int("899.0").

Task

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.

  1. Use the int() function to convert each reading into an integer.
    • Convert temperature_celsius into integer1.
    • Convert altitude_meters into integer2.
    • Convert pressure_pascal into integer3.
  2. Each resulting variable must be of type int.
  3. Store the final results in the variables integer1, integer2, and integer3.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 1

Ask AI

expand
ChatGPT

Ask anything or try one of the suggested questions to begin our chat

book
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:

1234567
value1 = int(657.89) value2 = int(90e3) value3 = int("678") print(value1) print(value2) print(value3)
copy

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, like int("8990"), not int("899.0").

Task

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.

  1. Use the int() function to convert each reading into an integer.
    • Convert temperature_celsius into integer1.
    • Convert altitude_meters into integer2.
    • Convert pressure_pascal into integer3.
  2. Each resulting variable must be of type int.
  3. Store the final results in the variables integer1, integer2, and integer3.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 4. ChapterΒ 1
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt