liveBook · Manning

Integer

As explained in Python How-To

int is Python's built-in numeric type for representing integers and the callable that constructs such values.

Python’s built-in int type represents integers; int(...) is also a constructor that can convert a string containing an integer into an int. This lets you use numeric input in calculations and comparisons instead of treating it as text. section 2.2.2 section 2.2

>>> int("-5")
-5
>>> int("123")
123

The int constructor converts strings representing positive or negative integers. section 2.2.2

Conversion can fail when a string does not represent an integer, raising ValueError. Use try...except to handle potentially invalid input so the exception does not stop the program. section 2.2.2 section 2.2.4

Converting task urgency

A task record such as "Laundry,3" can be split into a title and urgency string, then int(urgency_str) converts the urgency to an integer. If the record contains "3#" instead, conversion raises ValueError. section 12.3

When Python converts JSON numbers, it uses int for integer values and float for real numbers; JSON itself does not distinguish those number types. section 9.3.2

How Python How-To covers it

How deep it goes
Explains integer-string conversion, failed casts, and exception handling, without detailing int internals.
Examples
Uses positive and negative integer strings, age comparisons, and task urgency records, including malformed input.
What it assumes
Starts from strings and teaches why their textual values must be converted before numeric comparison.
What you can do after
Apply it via the int constructor to convert integer strings and handle invalid input with try...except.
How it connects
Links integers to strings, float, ValueError, exception handling, and JSON number conversion.

Also explained in

Compare how 15 books explain integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python How-To. The section links point to the parts of the book it draws on.