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

Swipe um das Menü anzuzeigen

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").

Aufgabe

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 4. Kapitel 1
single

single

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

close

Awesome!

Completion rate improved to 3.03

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").

Aufgabe

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.

Lösung

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

close

Awesome!

Completion rate improved to 3.03

Swipe um das Menü anzuzeigen

some-alt