Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer Essentiële Typeconversie | Kruistype-interacties
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Datatypen in Python

bookEssentiële Typeconversie

Typeconversie maakt het mogelijk om tussen de kern-Python-types te wisselen, zodat waarden kunnen worden vergeleken, berekend of weergegeven.

Converteren naar int

int(x) maakt een geheel getal.

  • Van een int: retourneert hetzelfde getal;
  • Van een float: wordt naar nul afgekapt (bijvoorbeeld, int(2.9) retourneert 2, int(-2.9) retourneert -2);
  • Van een string: de string moet een geheel getal voorstellen (optionele spaties en teken zijn toegestaan).

Geldige conversies

12345678910
# Converting different types of user input to integers age_input = " 42 " temperature_reading = 2.9 negative_balance = -2.9 print(int(age_input)) # 42 → clean string converted to int print(int(temperature_reading)) # 2 → fractional part truncated print(int(negative_balance)) # -2 → also truncates toward zero print(int("7")) # 7 → string number becomes integer print(int(" -15 ")) # -15 → handles spaces and sign
copy

Deze veroorzaken ValueError

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

Converteren naar float

float(x) maakt een drijvend-kommagetal.

  • Werkt voor gehele getallen en decimale of wetenschappelijke notatie-strings;
  • Komma's zijn geen decimale punten in Python.

Geldige conversies

12345678
# Converting numeric inputs for a shopping calculator quantity = 3 price_str = "2.5" discount_factor = "1e3" # scientific notation for 1000 print(float(quantity)) # 3.0 → integer converted to float print(float(price_str)) # 2.5 → string price converted to float print(float(discount_factor)) # 1000.0 → converts from scientific notation
copy

Deze veroorzaken ValueError

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

Converteren naar str

str(x) maakt een voor mensen leesbare stringrepresentatie. Geef de voorkeur aan f-strings bij het samenstellen van berichten.

12345678910
# Formatting a student's exam result student_age = 42 average_score = 3.5 print(str(student_age)) # "42" → number converted to string print(str(average_score)) # "3.5" → float converted to string student_name, final_score = "Ada", 98 report_message = f"{student_name} scored {final_score} points on the exam." print(report_message)
copy

Converteren naar bool

bool(x) volgt de waarheidswaarderegels van Python.

  • Getallen: 0 is False, elk ander getal is True;
  • Strings: "" (leeg) is False, elke niet-lege string is True (zelfs "0" en "False").
123456789101112
# Checking how different user inputs behave as boolean values login_attempts = 0 notifications = 7 username = "" user_id = "0" status = "False" print(bool(login_attempts)) # False → 0 means no attempts yet print(bool(notifications)) # True → non-zero means new notifications print(bool(username)) # False → empty string means no username entered print(bool(user_id)) # True → any non-empty string is truthy print(bool(status)) # True → text "False" is still a non-empty string
copy

Fouten om te vermijden

  • int("2.5") geeft een ValueError - eerst parsen als float(), daarna afronden of afkappen;
  • Locale gewoonte: "2,5" is ongeldig - gebruik "2.5";
  • Liggende streepjes in invoerstrings: "1_000" is ongeldig - verwijder eerst de streepjes: "1_000".replace("_", "");
  • Verrassing bij truthiness: bool("0") is True - vergelijk de stringinhoud expliciet indien nodig, bijvoorbeeld s == "0".

1. Wat levert elke regel op?

2. Welke aanroep veroorzaakt een ValueError?

3. Kies de juiste bewering.

question-icon

Wat levert elke regel op?

int(3.9)
int(" -8 ")

bool("0")

Click or drag`n`drop items and fill in the blanks

question mark

Welke aanroep veroorzaakt een ValueError?

Select the correct answer

question mark

Kies de juiste bewering.

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

Suggested prompts:

Can you explain more about how type conversion works in Python?

What happens if I try to convert a string that isn't a valid number?

Are there any tips for safely converting user input to numbers?

bookEssentiële Typeconversie

Veeg om het menu te tonen

Typeconversie maakt het mogelijk om tussen de kern-Python-types te wisselen, zodat waarden kunnen worden vergeleken, berekend of weergegeven.

Converteren naar int

int(x) maakt een geheel getal.

  • Van een int: retourneert hetzelfde getal;
  • Van een float: wordt naar nul afgekapt (bijvoorbeeld, int(2.9) retourneert 2, int(-2.9) retourneert -2);
  • Van een string: de string moet een geheel getal voorstellen (optionele spaties en teken zijn toegestaan).

Geldige conversies

12345678910
# Converting different types of user input to integers age_input = " 42 " temperature_reading = 2.9 negative_balance = -2.9 print(int(age_input)) # 42 → clean string converted to int print(int(temperature_reading)) # 2 → fractional part truncated print(int(negative_balance)) # -2 → also truncates toward zero print(int("7")) # 7 → string number becomes integer print(int(" -15 ")) # -15 → handles spaces and sign
copy

Deze veroorzaken ValueError

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

Converteren naar float

float(x) maakt een drijvend-kommagetal.

  • Werkt voor gehele getallen en decimale of wetenschappelijke notatie-strings;
  • Komma's zijn geen decimale punten in Python.

Geldige conversies

12345678
# Converting numeric inputs for a shopping calculator quantity = 3 price_str = "2.5" discount_factor = "1e3" # scientific notation for 1000 print(float(quantity)) # 3.0 → integer converted to float print(float(price_str)) # 2.5 → string price converted to float print(float(discount_factor)) # 1000.0 → converts from scientific notation
copy

Deze veroorzaken ValueError

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

Converteren naar str

str(x) maakt een voor mensen leesbare stringrepresentatie. Geef de voorkeur aan f-strings bij het samenstellen van berichten.

12345678910
# Formatting a student's exam result student_age = 42 average_score = 3.5 print(str(student_age)) # "42" → number converted to string print(str(average_score)) # "3.5" → float converted to string student_name, final_score = "Ada", 98 report_message = f"{student_name} scored {final_score} points on the exam." print(report_message)
copy

Converteren naar bool

bool(x) volgt de waarheidswaarderegels van Python.

  • Getallen: 0 is False, elk ander getal is True;
  • Strings: "" (leeg) is False, elke niet-lege string is True (zelfs "0" en "False").
123456789101112
# Checking how different user inputs behave as boolean values login_attempts = 0 notifications = 7 username = "" user_id = "0" status = "False" print(bool(login_attempts)) # False → 0 means no attempts yet print(bool(notifications)) # True → non-zero means new notifications print(bool(username)) # False → empty string means no username entered print(bool(user_id)) # True → any non-empty string is truthy print(bool(status)) # True → text "False" is still a non-empty string
copy

Fouten om te vermijden

  • int("2.5") geeft een ValueError - eerst parsen als float(), daarna afronden of afkappen;
  • Locale gewoonte: "2,5" is ongeldig - gebruik "2.5";
  • Liggende streepjes in invoerstrings: "1_000" is ongeldig - verwijder eerst de streepjes: "1_000".replace("_", "");
  • Verrassing bij truthiness: bool("0") is True - vergelijk de stringinhoud expliciet indien nodig, bijvoorbeeld s == "0".

1. Wat levert elke regel op?

2. Welke aanroep veroorzaakt een ValueError?

3. Kies de juiste bewering.

question-icon

Wat levert elke regel op?

int(3.9)
int(" -8 ")

bool("0")

Click or drag`n`drop items and fill in the blanks

question mark

Welke aanroep veroorzaakt een ValueError?

Select the correct answer

question mark

Kies de juiste bewering.

Select the correct answer

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 4. Hoofdstuk 1
some-alt