Concepts / Understanding Python Data Types

Understanding Python Data Types

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

  • Programming
Interactive lab

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

  1. int(text) strips surrounding whitespace, allows one sign, then reads digits (underscores only between digits); any other character raises ValueError: invalid literal for int().
  2. int(text, base) reads digits in that base; base 0 takes the base from a 0x / 0o / 0b prefix like a Python literal.
  3. float(text) accepts decimals like '.5', '5.', '1e3' and the names inf, infinity and nan, then rounds to the nearest double.
  4. int() of a float truncates toward zero; str() gives the printed form (repr digits for floats).
  5. 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.

Educational simulation

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?

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

7.977.17-7.9-7int()int()int()
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.

3.14float value3integer result'3'whole-number string3integer result'3.14'decimal stringerrorinvalid inputerrorint()int()int()int()
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

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.

python
Output
3
Worked example

Tracing a Decimal String

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

  1. 1
    Start with the string

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

  2. 2
    Apply float()

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

  3. 3
    Apply int()

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

Result

The result is the integer 3.

Avoiding Conversion Mistakes

  • Common mistake

    Assuming int() rounds to the nearest whole number.

    What it looks like

    Treating 3.99999 as though it becomes 4.

    Why it fails

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

    Do this instead
    Expect 3 when int() converts 3.99999.
  • Common mistake

    Passing a decimal string directly to int().

    What it looks like
    int('3.14')
    Why it fails

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

    Do this instead
    Use the two-step route int(float('3.14')).
  • Common mistake

    Treating every string as valid input for int().

    What it looks like
    Using int() with invalid input.
    Why it fails

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

    Do this instead

    Check 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 like

    Assuming these function names describe one identical transformation.

    Why it fails

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

    Do this instead

    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.
Worked example

Practice Check

Classify the inputs 7.9, '7', '7.9', and invalid input.

  1. 1
    7.9

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

  2. 2
    '7'

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

  3. 3
    '7.9'

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

  4. 4
    invalid input

    int() raises an error for invalid input.

Result

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. 1
    int() converts values to integers by truncating the decimal portion rather than rounding.
  2. 2
    A value such as 3.99999 becomes 3, not 4.
  3. 3
    int() works with floats and whole-number strings, but decimal strings and invalid input raise an error.
  4. 4
    To convert a decimal string, use float() first and then int(), as in int(float('3.14')).
  5. 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.

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.

Related concepts

More concepts starting with U