# Python Number Data Types Tutorial

In this tutorial we learn integer, floating point, and complex numbers in Python, and how to convert between (cast) them.

We also cover common mathematical and trigonometric functions.

- Number Data Types
- The int type
- The float type
- The complex type
- How to see which type a number is
- How to convert (cast) a number type to another
- How to convert a number to int
- How to convert a number to float
- How to convert a number to complex
- List of common mathematical functions
- List of common trigonometric functions
- Summary

## Number Data Types

Python supports three different numerical types.

- int is a positive or negative whole number with unlimited size.
- float , or floating point number, is a number with decimal points.
- complex is a number with imaginary parts.

## The int type

An int is a whole number. It can be positive or negative but it shouldn’t have a decimal point.

` print(10)`

## The float type

A float is a floating point number, that’s to say it’s a number with a decimal point.

` print(3.14)`

A float can also be a scientific number with an float to indicate the power of 10.

` print(-78e3)`

## The complex type

A complex number is a float number and a square root of -1. The square root is represented as an imaginary number by the lowercase or uppercase letter J .

` print(619j)`

## How to see which type() a number is

If we want to see which type a number is, we pass the number to the built-in type() function.

```
print( type(20) )
print( type(3.14) )
print( type(-78e3) )
print( type(619j) )
```

In the example above we check the type of an int , two float numbers and a complex number.

```
<class 'int'>
<class 'float'>
<class 'float'>
<class 'complex'>
```

The output shows us that the numbers belong to one of the numeric data types.

## How to explicitly convert a number type to another number type

In most cases Python will convert numbers internally, but sometimes we may want to convert one type of number to another explicitly. To do this we use specific conversion functions.

### How to convert a number to int

To convert a number to an int we use the int() function.

` print( int(3.14) )`

note
*When the conversion happened, it simply cut away everything after the decimal point.*

### How to convert a number to float

To convert a number to a float we use the float() function.

` print( float(10) )`

### How to convert a number to complex

To convert a number to a complex number we use the complex() function with one or two parameters.

` print( complex(10) )`

In the example above we used only one number. It will use the 10 as the float part of the complex number and the imaginary part will be 0.

` print( complex(10, 5) )`

In the example above we used two numbers separated by a comma. It will convert the 10 as the float part of the complex number and use the 5 as the imaginary part of the complex number.

## List of common mathematical functions

The following table lists some of the commonly used math functions:

Function | Description | Example | Returns |
---|---|---|---|

abs(x) | Positive (absolute) value integer | abs(-10) | 10 |

ceil(x) | Round to closest integer top | ceil(3.14) | 4 |

exp(x) | Exponential of x | exp(1.5) | 4.481 |

fabs(x) | Positive (absolute) value float | fabs(-10) | 10.0 |

floor(x) | Round to closest integer bottom | floor(3.14) | 3 |

pow(x, y) | Power of. x**y | pow(2, 2) | 4 |

round(x) | Round to closest top or bottom | round(3.14) | 3 |

sqrt(x) | Square root of x | sqrt(5) | 2.236 |

max(x, y, …) | Largest number of the set | max(1, 3, 5) | 5 |

min(x, y, …) | Smallest number of the set | min(1, 3, 5) | 1 |

## List of common trigonometric functions

The following table lists some of the commonly used trigonometric functions:

Function | Description |
---|---|

acos(x) | Return the arc cosine of x, in radians |

asin(x) | Return the arc sine of x, in radians |

atan(x) | Return the arc tangent of x, in radians |

cos(x) | Return the cosine of x radians |

sin(x) | Return the sine of x radians |

tan(x) | Return the tangent of x radians |

degrees(x) | Converts angle x from radians to degrees |

radians(x) | Converts angle x from degrees to radians |

## Summary: Points to remember

- An int is a positive or negative whole number.
- A float is a number with a fractional part in which the fractional component is denoted by a decimal symbol or scientific notation.
- A complex is a number with real and imaginary parts.
- We can check the type of number by passing it to the built-in type() function.
- We can convert one number type to another by using the int() , float() or complex() functions.