# Python Numbers

Here's an overview of the various numeric types in Python, and how to work with them.

Python has three distinct numeric types: **integers**, **floating point numbers**, and **complex numbers**. These are usually referred to as `int`

, `float`

, and `complex`

types. Also, the **boolean** type is a subtype of the integer type.

`int`

- Refers to an integer. An integer is a whole number (i.e. not a fraction). Integers can be a positive number, a negative one, or zero. Examples of integers: -3, -2, -1, 0, 1, 2, 3
`float`

- Refers to a floating point number. Floating point numbers represent real numbers and are written with a decimal point dividing the integer and the fractional parts. Floating point numbers can also be in scientific notation, with
`E`or`e`indicating the power of 10 (eg,`+1e3`

is equivalent to`1000.0`

). Examples of floats: 1.0, 12.45, 10.4567, -10.0, -20.76789, 64.2e18, -64.2e18. `complex`

- A complex number takes the form
`a + bj`

where`a`

is a real number and`b`

is an imaginary number. Each argument an be any numeric type (including complex). The first argument can also be a string (but the second argument can't). Examples: 1.4j, -1.4j, 2+18j, -2.18j, 5.14-7j, 5.14e+45j, -5.14e+45j.

In Python, all numeric types are immutable. If you want to change any part of a number you need to reassign the number itself.

Here's an example of creating some number objects, then printing each number along with its type:

<class 'int'> 1 <class 'int'> -1 <class 'float'> 1.0 <class 'float'> -1.0 <class 'float'> 2000.0 <class 'float'> -2000.0 <class 'complex'> 3.14j <class 'complex'> (-0-3.14j)

## Random Numbers

Python provides various functions that allow you to generate random numbers. These are made possible by the `random`

module. The `random`

module must be imported using the `import`

keyword before you can use any of the random number functions. Here are a few:

0.8296271520534051 34 90 [60, 70, 20] [90, 50, 80, 20, 70]

Of course, this is only an example of the results that could be returned. Seeing as the results are randomly generated, they will be different each time it's run.

## Minimum and Maximum Numbers

You can use the `min()`

and `max()`

functions to return the smallest or largest number within a group of numbers. You can supply the numbers as multiple parameters or as a list. Like this:

3 1 77 11

## Type Conversion

Python has an inbuilt ability to convert numbers to a single type when performing calculations. For example, say you want to do this:

These are two different number types. The `100`

is an integer while the `2.5`

is a float. However, Python can handle this. It will make the resulting number a float.

Here's a demo:

100 <class 'int'> 2.5 <class 'float'> 102.5 <class 'float'>

### Conversion Functions

You can also use functions such as `int()`

, `float()`

, and `complex()`

to make an explicit conversion between one number type and another.

Here are some examples:

<class 'int'> <class 'float'> <class 'complex'> <class 'bool'>

## Numbering Systems

The decimal numbering system is the most widely used system in the modern world. Also called base-ten, the decimal system has 10 as its base, and uses the digits 0 to 9.

There are other numbering systems though, that don't use 10 as its base. The binary system is base-two (uses the digits 1 and 0), the octal system is base-eight (uses digits 0 to 7), and the hexadecimal system is base-sixteen (uses digits 0 to 9, and letters A to F). These numbering systems tend to be more popular in mathematics and computing.

In Python, you can specify the numbering system a number uses by using a two-digit prefix as follows:

Numbering System | Prefix |
---|---|

Binary | `0b` or `0B` |

Octal | `0o` or `0O` |

Hexadecimal | `0x` or `0X` |

The prefix can be uppercase or lowercase.

Here's an example of printing out various numbers from different numbering systems. Although we provide the numbers in binary, octal, and hexadecimal, the output is in decimal.

3 13 14

The following example prints out a range of numbers from each system. You can see how each value in the code maps to a base-ten number in the output:

... 1 2 3 4 5 ... ... 5 6 7 8 9 10 11 12 13 ... ... 7 8 9 10 11 12 13 14 15 16 18 19 ...

You can also use functions such as `hex()`

and `oct()`

to return an integer as a hexadecimal or octal number.

0o144 0x64