Concepts / Understanding Python Data Types

Understanding Python Data Types

int() converts values to integers by truncating (not rounding) the decimal portion

  • Programming

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?

  • 3
  • 4
  • An error
Reveal answer

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.

int()int()int()7.977.17-7.9-7
What happens to the decimal portion when int() converts values such as 7.9, 7.1, and -7.9?
InputEffect of int()Result
7.9Decimal portion is removed7
7.1Decimal portion is removed7
3.99999Decimal portion is removed instead of rounded3
-7.9Decimal 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.

int()int()int()int()3.14float value3integer result'3'whole-number string3integer result'3.14'decimal stringerrorinvalid inputerror
Which input values pass through int() successfully, and where does conversion stop with an error?
Input categoryint() behavior
Float, such as 3.14Converts 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 inputRaises an error

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.

python
Output
3

Tracing a Decimal String

Determine the result of int(float('3.14')).

Start with the string: The input is the string '3.14', which is a decimal string.

Apply float(): float() is used first so the decimal string becomes a float value that int() can process.

Apply int(): int() removes the decimal portion instead of rounding the value.

The result is the integer 3.

Avoiding Conversion Mistakes

  • Assuming int() rounds to the nearest whole number.

    int() truncates the decimal portion. It does not round based on the decimal's size.

    Fix: Expect 3 when int() converts 3.99999.

  • Passing a decimal string directly to int().

    The source states that int() raises an error for decimal strings.

    Fix: Use the two-step route int(float('3.14')).

  • Treating every string as valid input for int().

    int() works with whole-number strings, but invalid input raises an error.

    Fix: Check whether the string represents a whole number or use the documented float-then-int route for a decimal string.

  • Ignoring the conversion path when comparing int(), float(), and str().

    The source specifically defines int() as conversion to an integer and identifies float() as the intermediate step for a decimal string.

    Fix: Track which function is being applied and what role it plays in the conversion path.

Practice the Path

MEDIUM

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.

7.9: This is a float. int() truncates the decimal portion, producing 7.

'7': This is a whole-number string, which int() can convert successfully.

'7.9': This is a decimal string, so direct int() conversion raises an error. The source-supported route is int(float('7.9')).

invalid 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.
  2. A 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.
  4. To convert a decimal string, use float() first and then int(), as in int(float('3.14')).
  5. Keep the conversion path clear when comparing int() with float() or str(); the source-supported rule here is specifically about int() conversion and the float-then-int route.

Key Takeaways

  • 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.