Converting Between Data Types: An Overview
A value can be represented in more than one data type.
Try it: Type conversion: int(), float(), str(), bool()
Exactly how int(), int(text, base), float(), str() and bool() turn a value into another type: Python's real parsing rules for text (whitespace, sign, underscores, 0x/0o/0b prefixes, inf/nan, exponents) and its truthiness rules.
How it works
- int(text) strips surrounding whitespace, allows one sign, then reads digits (underscores only between digits); any other character raises ValueError: invalid literal for int().
- int(text, base) reads digits in that base; base 0 takes the base from a 0x / 0o / 0b prefix like a Python literal.
- float(text) accepts decimals like '.5', '5.', '1e3' and the names inf, infinity and nan, then rounds to the nearest double.
- int() of a float truncates toward zero; str() gives the printed form (repr digits for floats).
- bool() is truthiness: '', 0, 0.0 and None are False; every non-empty string — even '0' or 'False' — is True.
Default run (8 steps): int(' -0x_1F ', 0): the argument is a str of 10 characters. … int(' -0x_1F ', 0) → -31
Simplified: The argument is a Python literal typed as text (ASCII only; escapes \t \n \r). Unicode digits and bytes arguments are not modelled.
Loading the simulation…
From Whole Numbers to Decimal Form
A value can be represented in more than one data type. The float() conversion changes an integer or a numeric string into a floating-point number. When an integer has no decimal representation, float() adds one to the representation while preserving the number's quantity.
7.0Tracing Numeric Strings
User input commonly arrives as a string, even when the user typed a number. float() can convert a string representation of a number into a floating-point number. For example, the numeric string "3.14" becomes the floating-point value 3.14. The important condition is that the string must represent a valid number.
Converting a Numeric String
Convert the numeric string "3.14" into a floating-point number.
- 1Start with the string
The input is a string representation of a number rather than a numeric value already stored as a number.
- 2Apply float()
float()reads the valid numeric string and converts it to floating-point form. - 3Read the result
The resulting numeric value is represented as 3.14.
float("3.14") produces 3.14 as a floating-point number.
3.14float() and int() Compared
The key difference is what happens to a fractional part. float() preserves fractional values and returns a true floating-point type. int(), by contrast, truncates a value when converting it to an integer. Therefore, choosing between the two conversions depends on whether the fractional part must remain available.
| Conversion | Behavior with a fractional value | Resulting form |
|---|---|---|
| float() | Preserves the fractional value | Floating-point number |
| int() | Truncates the fractional value | Integer |
The source's central distinction between float() and int() conversion behavior.
Predicting Conversion Results
What do you think happens?
What happens when float() receives each of these inputs?
Reveal answer
float(7) returns 7.0. float("3.14") returns 3.14. float("not a number") raises a ValueError because the string does not represent a valid number.
Integers and valid numeric strings can be converted to floating-point numbers. An input string that is not a valid number cannot be converted by float().
Mistakes Beginners Make
Common mistake Assuming float() removes the fractional part.
What it looks likeA value with a fractional part is passed to float().
Why it failsfloat()preserves fractional values.Do this insteadUse float() when the fractional part must remain available. int() is the conversion described as truncating.
Common mistake Treating every string as convertible.
What it looks likefloat("not a number")Why it failsThe string does not represent a valid number, so
float()raises aValueError.Do this insteadProvide a string representation of a valid number before using float().
Common mistake Forgetting that float() changes the data type.
What it looks likefloat(7) is considered the same type as the original integer.
Why it failsfloat()returns a true floating-point type.Do this insteadRemember that 7 becomes the floating-point representation 7.0.
Choosing the Conversion
Use float() when a calculation needs values in floating-point form or when a numeric value arrives as a string and its fractional part should be preserved. This is especially useful when handling user input represented as strings. Use int() instead when the intended behavior is truncation and an integer result is required.
For each input, predict the result of float(): float(12), float("8.5"), and float("eight"). Then identify which input causes a ValueError.
Hints
- An integer is converted to floating-point form with decimal representation.
- A valid numeric string can be converted.
- A string that does not represent a valid number causes a
ValueError.
Key Takeaways
- 1
float()converts integers and valid numeric strings to floating-point numbers. - 2Converting an integer adds decimal representation, such as 7 becoming 7.0.
- 3
float()preserves fractional values, whileint()truncates them. - 4An invalid numeric string causes
float()to raise aValueError. - 5
float()is useful for converting string-based user input and for ensuring values are in floating-point form for calculations.
At a glance
float()converts integers and numeric strings into floating-point numbers.- An integer such as 7 is represented as 7.0 after conversion.
float()preserves fractional values, unlikeint(), which truncates them.- Invalid numeric strings cause
float()to raise aValueError.