# Python Numbers

> Learn how numbers work in Python with the int, float, and complex types, the arithmetic operators, and built-in helpers like abs() and round() for rounding.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-12-05 | Topics: [Python](https://flaviocopes.com/tags/python/) | Canonical: https://flaviocopes.com/python-numbers/

Numbers in [Python](https://flaviocopes.com/python-introduction/) can be of 3 types: `int`, `float` and `complex`.

### Integer numbers

Integer numbers are represented using the `int` class. You can define an integer using a value literal:

```python
age = 8
```

You can also define an integer number using the `int()` constructor:

```python
age = int(8)
```

To check if a variable is of type `int`, you can use the `type()` global function:

```python
type(age) == int #True
```

### Floating point numbers

Floating point numbers (fractions) are of type `float`. You can define an integer using a value literal:

```python
fraction = 0.1
```

Or using the `float()` constructor:

```python
fraction = float(0.1)
```

To check if a variable is of type `float`, you can use the `type()` global function:

```python
type(fraction) == float #True
```

### Complex numbers

Complex numbers are of type `complex`.

You can define them using a value literal:

```python
complexNumber = 2+3j
```

or using the `complex()` constructor:

```python
complexNumber = complex(2, 3)
```

Once you have a complex number, you can get its real and imaginary part:

```python
complexNumber.real #2.0
complexNumber.imag #3.0
```

Again, to check if a variable is of type `complex`, you can use the `type()` global function:

```python
type(complexNumber) == complex #True
```

### Arithmetic operations on numbers

You can perform arithmetic operations on numbers, using the arithmetic operators: `+`, `-`, `*`, `/` (division), `%` (remainder), `**` (exponentiation) and `//` (floor division):

```python
1 + 1 #2
2 - 1 #1
2 * 2 #4
4 / 2 #2
4 % 3 #1
4 ** 2 #16
4 // 2 #2
```

and you can use the compound assignment operators

- `+=`
- `-=`
- `*=`
- `/=`
- `%=`
- ..and so on

to quickly perform operations on variables, too:

```python
age = 8
age += 1
```

### Built-in Functions

There are 2 built-in functions that help with numbers:

`abs()` returns the absolute value of a number.

`round()` given a number, returns its value rounded to the nearest integer:

```python
round(0.12) #0
```

You can specify a second parameter to set the decimal points precision:

```python
round(0.12, 1) #0.1
```

Several other math utility functions and constants are provided by the [Python standard library](https://flaviocopes.com/python-standard-library/):

- the `math` package provides general math functions and constants
- the `cmath` package provides utilities to work with complex numbers.
- the `decimal` package provides utilities to work with decimals and floating point numbers.
- the `fractions` package provides utilities to work with rational numbers

We'll explore some of those separately later on.
