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


