Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Grunnleggende om Typekonvertering | Interaksjoner Mellom Ulike Datatyper
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Datatyper i Python

bookGrunnleggende om Typekonvertering

Type conversion lets you move between core Python types so values can be compared, calculated, or displayed.

Converting to int

int(x) makes an integer.

  • From an int: returns the same number;
  • From a float: truncates toward zero (for example, int(2.9) returns 2, int(-2.9) returns -2);
  • From a string: the string must represent an integer (optional spaces and sign are ok).

Valid Conversions

123456
age_input = " 42 " print(int(age_input)) # 42 print(int(2.9)) # 2 print(int(-2.9)) # -2 print(int("7")) # 7 print(int(" -15 ")) # -15
copy

These Raise ValueError

12
int("2.5") # ValueError - not an integer string int("42a") # ValueError
copy

Converting to float

float(x) makes a floating-point number.

  • Works for ints and decimal or scientific-notation strings;
  • Commas are not decimal points in Python.

Valid conversions

123
print(float(3)) # 3.0 print(float("2.5")) # 2.5 print(float("1e3")) # 1000.0
copy

These Raise ValueError

1
float("2,5") # ValueError - use a dot, not a comma
copy

Converting to str

str(x) makes a human-readable string representation. Prefer f-strings when you are building messages.

123
print(str(42)) # "42" print(str(3.5)) # "3.5" print(f"Ada scored {98} points.")
copy

Converting to bool

bool(x) follows Python truthiness rules.

  • Numbers: 0 is False, any other number is True;
  • Strings: "" (empty) is False, any non-empty string is True (even "0" and "False").
12345
print(bool(0)) # False print(bool(7)) # True print(bool("")) # False print(bool("0")) # True print(bool("False")) # True
copy

Mistakes to Avoid

  • int("2.5") raises ValueError - parse as float() first, then truncate or round;
  • Locale habit: "2,5" is invalid - use "2.5";
  • Underscores in input strings: "1_000" is invalid - remove underscores first: "1_000".replace("_", "");
  • Truthiness surprise: bool("0") is True - compare string contents explicitly if needed, for example s == "0".

1. Which call raises a ValueError?

2. Pick the correct statement.

question mark

Which call raises a ValueError?

Select the correct answer

question mark

Pick the correct statement.

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookGrunnleggende om Typekonvertering

Sveip for å vise menyen

Type conversion lets you move between core Python types so values can be compared, calculated, or displayed.

Converting to int

int(x) makes an integer.

  • From an int: returns the same number;
  • From a float: truncates toward zero (for example, int(2.9) returns 2, int(-2.9) returns -2);
  • From a string: the string must represent an integer (optional spaces and sign are ok).

Valid Conversions

123456
age_input = " 42 " print(int(age_input)) # 42 print(int(2.9)) # 2 print(int(-2.9)) # -2 print(int("7")) # 7 print(int(" -15 ")) # -15
copy

These Raise ValueError

12
int("2.5") # ValueError - not an integer string int("42a") # ValueError
copy

Converting to float

float(x) makes a floating-point number.

  • Works for ints and decimal or scientific-notation strings;
  • Commas are not decimal points in Python.

Valid conversions

123
print(float(3)) # 3.0 print(float("2.5")) # 2.5 print(float("1e3")) # 1000.0
copy

These Raise ValueError

1
float("2,5") # ValueError - use a dot, not a comma
copy

Converting to str

str(x) makes a human-readable string representation. Prefer f-strings when you are building messages.

123
print(str(42)) # "42" print(str(3.5)) # "3.5" print(f"Ada scored {98} points.")
copy

Converting to bool

bool(x) follows Python truthiness rules.

  • Numbers: 0 is False, any other number is True;
  • Strings: "" (empty) is False, any non-empty string is True (even "0" and "False").
12345
print(bool(0)) # False print(bool(7)) # True print(bool("")) # False print(bool("0")) # True print(bool("False")) # True
copy

Mistakes to Avoid

  • int("2.5") raises ValueError - parse as float() first, then truncate or round;
  • Locale habit: "2,5" is invalid - use "2.5";
  • Underscores in input strings: "1_000" is invalid - remove underscores first: "1_000".replace("_", "");
  • Truthiness surprise: bool("0") is True - compare string contents explicitly if needed, for example s == "0".

1. Which call raises a ValueError?

2. Pick the correct statement.

question mark

Which call raises a ValueError?

Select the correct answer

question mark

Pick the correct statement.

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 4. Kapittel 1
some-alt