Essentië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)retourneert2,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
Deze veroorzaken ValueError
12int("2.5") # ValueError - not an integer string int("42a") # ValueError
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
Deze veroorzaken ValueError
1float("2,5") # ValueError - use a dot, not a comma
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)
Converteren naar bool
bool(x) volgt de waarheidswaarderegels van Python.
- Getallen:
0isFalse, elk ander getal isTrue; - Strings:
""(leeg) isFalse, elke niet-lege string isTrue(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
Fouten om te vermijden
int("2.5")geeft eenValueError- eerst parsen alsfloat(), 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")isTrue- vergelijk de stringinhoud expliciet indien nodig, bijvoorbeelds == "0".
1. Wat levert elke regel op?
2. Welke aanroep veroorzaakt een ValueError?
3. Kies de juiste bewering.
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
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?
Geweldig!
Completion tarief verbeterd naar 5.26
Essentië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)retourneert2,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
Deze veroorzaken ValueError
12int("2.5") # ValueError - not an integer string int("42a") # ValueError
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
Deze veroorzaken ValueError
1float("2,5") # ValueError - use a dot, not a comma
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)
Converteren naar bool
bool(x) volgt de waarheidswaarderegels van Python.
- Getallen:
0isFalse, elk ander getal isTrue; - Strings:
""(leeg) isFalse, elke niet-lege string isTrue(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
Fouten om te vermijden
int("2.5")geeft eenValueError- eerst parsen alsfloat(), 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")isTrue- vergelijk de stringinhoud expliciet indien nodig, bijvoorbeelds == "0".
1. Wat levert elke regel op?
2. Welke aanroep veroorzaakt een ValueError?
3. Kies de juiste bewering.
Bedankt voor je feedback!