liveBook · Manning

Integer

As explained in Get Programming: Learn to code with Python

int is Python's built-in type and constructor for representing or converting values to integers.

An integer is a whole-number value. In Python, its type is called int: 0, 5, and -4 are integers. A value’s type determines which operations you can use with it. For example, you can add two integers with 1 + 2 to get 3. section 5.2

An integer is not the same type as a float, even when their values look similar: 1 is an int, while 1.0 is a float. You can check a value’s type with type(), such as type(3). section 5.2 section 5.3

The int() constructor also converts suitable values to integers:

  • int("4") gives 4. The first value is a string; the result is an integer. int("a") causes an error because the string is not a number. section 5.3
  • int(3.94) gives 3: conversion removes the decimal part. int(False) gives 0, and int(True) gives 1. section 5.3
Table 5.1. Mathematical operations on ints and floats and the resulting types

Type of first object

Operation(s)

Type of second object

Type of result

Example

int + - * ** % int int 3 + 2 3 - 2 3 * 2 3 ** 2 3 % 2 gives 5 gives 1 gives 6 gives 9 gives 1
int / int float 3 / 2 gives 1.5
int + - * / ** % float float 3 + 2.0 3 - 2.0 3 * 2.0 3 / 2.0 3 ** 2.0 3 % 2.0 gives 5.0 gives 1.0 gives 6.0 gives 1.5 gives 9.0 gives 1.0
float + - * / ** % int float 3.0 + 2 3.0 - 2 3.0 * 2 3.0 / 2 3.0 ** 2 3.0 % 2 gives 5.0 gives 1.0 gives 6.0 gives 1.5 gives 9.0 gives 1.0
float + - * / ** % float float 3.0 + 2.0 3.0 - 2.0 3.0 * 2.0 3.0 / 2.0 3.0 ** 2.0 3.0 % 2.0 gives 5.0 gives 1.0 gives 6.0 gives 1.5 gives 9.0 gives 1.0

Compare the result types as well as the numbers: 3 + 2 produces an int, but 3 / 2 produces the float 1.5. Operations mixing an int and a float also produce floats in the examples shown. section 5.3

Find whole hours

Suppose you need to express 123 minutes as hours and leftover minutes. Dividing by 60 gives 2.05; int(123 / 60) gives 2 whole hours. The leftover minutes need a separate calculation. Try the same first step with 800 minutes: what whole-hour count do you get? section 6.3

Listing 11.5. Calculations with user input
user_input = input("Enter a number to find the square of: ")        #1
num = int(user_input)                                               #2
print(num*num)                                                      #3

input() supplies a string, even when someone types a number. This program converts that string with int() before multiplying the number by itself. section 11.2

Integers are useful beyond ordinary arithmetic. In a string expression, an integer can specify how many repetitions to make: "-" * 10 creates ten dashes. section 8.2

How Get Programming: Learn to code with Python covers it

How deep it goes
Defines int, demonstrates arithmetic and several conversions, and works through extracting whole hours; does not examine integer storage internals.
Examples
Uses 123-minute conversion, a user-entered number squared, and integer-controlled string repetition.
What it assumes
Builds from basic object types, variables, and expressions, defining these ideas as integer operations are introduced.
What you can do after
Apply it via Python’s int() to convert numeric input and extract whole hours from a division result.
How it connects
Links integers to floats, strings, Booleans, expressions, type conversion, and user input.

Also explained in

Compare how 15 books explain integer →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Get Programming: Learn to code with Python. The section links point to the parts of the book it draws on.