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

Pyyhkäise näyttääksesi valikon

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

Tehtävä

Swipe to start coding

  1. Convert variable1 to an integer.
  2. Convert variable2 to an integer.
  3. Convert variable3 to an integer.

I want to admit that you are going to get an attractive result: 3 perfect numbers. I'll explain what perfect numbers are at the end of chapter.🤔

Ratkaisu

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.😄

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 4. Luku 1
single

single

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

close

Awesome!

Completion rate improved to 2.94

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

Tehtävä

Swipe to start coding

  1. Convert variable1 to an integer.
  2. Convert variable2 to an integer.
  3. Convert variable3 to an integer.

I want to admit that you are going to get an attractive result: 3 perfect numbers. I'll explain what perfect numbers are at the end of chapter.🤔

Ratkaisu

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.😄

Switch to desktopVaihda työpöytään todellista harjoitusta vartenJatka siitä, missä olet käyttämällä jotakin alla olevista vaihtoehdoista
Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

close

Awesome!

Completion rate improved to 2.94

Pyyhkäise näyttääksesi valikon

some-alt