liveBook · Manning

Type function

As explained in Get Programming: Learn to code with Python

A Python built-in function that returns the type of an object.

The type() function is a Python built-in that reports the data type of an object. Use it in the console when you are unsure whether a value is an integer, string, or another type. section 5.3

Check two values

type(3) reports that 3 is an integer. type("wicked") reports that "wicked" is a string. Type each expression in the Python console and press Enter to inspect the result. 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 how arithmetic involving integers and floats produces different result types. section 5.3

How Get Programming: Learn to code with Python covers it

How deep it goes
Defines type() and demonstrates console checks, but does not explain its internal implementation.
Examples
Uses type(3) and type("wicked") to identify integer and string values.
What it assumes
Assumes basic Python expressions and values while introducing object types through simple examples.
What you can do after
Evaluate values in the console to identify their data types.
How it connects
Connects object types with expressions, arithmetic, and type conversion.

Also explained in

Compare how 3 books explain type function →

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.