Understanding Python Data Types
int() converts values to integers by truncating (not rounding) the decimal portion
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…
The Conversion Question
When a value contains a decimal portion, converting it to an integer is not the same as rounding it. The int() function removes the decimal portion rather than choosing the nearest whole number. That difference matters for values such as 7.9, 7.1, and 3.99999.
What do you think happens?
What whole-number value should int() produce from 3.99999?
Reveal answer
3
int() truncates the decimal portion. It does not round 3.99999 up to 4.
Removing the Decimal Portion
The central rule is simple: int() converts a value to an integer by truncating its decimal portion. Truncation means chopping off the decimal part entirely. The size of that decimal part does not change the rule, so 3.99999 becomes 3 rather than 4. This is different from rounding, which would consider how large the decimal portion is.
| Input | Effect of int() | Result |
|---|---|---|
| 7.9 | Decimal portion is removed | 7 |
| 7.1 | Decimal portion is removed | 7 |
| 3.99999 | Decimal portion is removed instead of rounded | 3 |
| -7.9 | Decimal portion is removed | -7 |
These examples illustrate truncation: int() chops off the decimal portion rather than rounding.
Inputs That Pass or Fail
int() works with floating-point values and with strings that contain whole numbers. It raises an error for strings that contain decimal values and for invalid input. The important distinction is between a numeric value with a decimal portion and a string representation that includes a decimal point.
| Input category | int() behavior |
|---|---|
| Float, such as 3.14 | Converts it to an integer by truncating the decimal portion |
| Whole-number string, such as '3' | Converts successfully to an integer |
| Decimal string, such as '3.14' | Raises an error |
| Invalid input | Raises an error |
The written form of an input matters: a float and a decimal string do not follow the same direct int() path.
Converting Decimal Strings
A decimal string cannot be passed directly to int() according to the supplied conversion rule. Instead, convert the string to a float first, then convert that float to an integer. The first conversion changes the decimal string into a value that int() can process; the second conversion truncates the decimal portion.
3Tracing a Decimal String
Determine the result of int(float('3.14')).
- 1Start with the string
The input is the string '3.14', which is a decimal string.
- 2Apply float()
float()is used first so the decimal string becomes a float value thatint()can process. - 3Apply int()
int()removes the decimal portion instead of rounding the value.
The result is the integer 3.
Avoiding Conversion Mistakes
Common mistake Assuming int() rounds to the nearest whole number.
What it looks likeTreating 3.99999 as though it becomes 4.
Why it failsint()truncates the decimal portion. It does not round based on the decimal's size.Do this insteadExpect 3 when int() converts 3.99999.Common mistake Passing a decimal string directly to int().
What it looks likeint('3.14')Why it failsThe source states that
int()raises an error for decimal strings.Do this insteadUse the two-step route int(float('3.14')).Common mistake Treating every string as valid input for int().
What it looks likeUsing int() with invalid input.Why it failsint()works with whole-number strings, but invalid input raises an error.Do this insteadCheck whether the string represents a whole number or use the documented float-then-int route for a decimal string.
Common mistake Ignoring the conversion path when comparing int(), float(), and str().
What it looks likeAssuming these function names describe one identical transformation.
Why it failsThe source specifically defines
int()as conversion to an integer and identifiesfloat()as the intermediate step for a decimal string.Do this insteadTrack which function is being applied and what role it plays in the conversion path.
Practice the Path
For each input, decide whether int() can process it directly. If it can, state the integer result. If it cannot, identify whether the issue is a decimal string or invalid input. Then explain how the decimal string case could be handled using float() first: 7.9, '7', '7.9', and invalid input.
Hints
- A float can be converted by truncating its decimal portion.
- A whole-number string is a supported direct input.
- A decimal string needs the float-then-int route.
- Invalid input raises an error.
Practice Check
Classify the inputs 7.9, '7', '7.9', and invalid input.
- 17.9
This is a float.
int()truncates the decimal portion, producing 7. - 2'7'
This is a whole-number string, which
int()can convert successfully. - 3'7.9'
This is a decimal string, so direct
int()conversion raises an error. The source-supported route isint(float('7.9')). - 4invalid input
int()raises an error for invalid input.
The direct successes are the float 7.9 and the whole-number string '7'. The decimal string and invalid input do not pass directly through int().
Key Takeaways
- 1
int()converts values to integers by truncating the decimal portion rather than rounding. - 2A value such as 3.99999 becomes 3, not 4.
- 3
int()works with floats and whole-number strings, but decimal strings and invalid input raise an error. - 4To convert a decimal string, use
float()first and thenint(), as inint(float('3.14')). - 5Keep the conversion path clear when comparing
int()withfloat()orstr(); the source-supported rule here is specifically aboutint()conversion and the float-then-int route.
At a glance
int()removes decimal portions; it does not round.- Floats and whole-number strings can be converted directly by
int(). - Decimal strings and invalid input raise an error when passed directly to
int(). - Use
int(float('3.14'))to convert a decimal string through a float first. - Always distinguish the input's representation and the conversion function being applied.