# Python Numbers

In this tutorial, you’ll learn about **Python Numbers with Type conversion, checking and also functions specifically for handling numeric values.** In General, Numbers can be assigned to Python Variables directly through equal to an operator without the need of specifying the datatype whether the number is **integer** or **float**. Python is a **dynamically typed** programming language implicitly converts the assigned values to its respective datatype and also adds additional properties and methods through which you can access them to manipulate the output of a number.

## Post Update Timeline

### Updated on: 29-06-2020

Added new topics such as **Number Validation, Type Conversion and Formatting with Currency Formatting** etc

## As for now, Python supports three types of Datatypes

- Integer ( int )
- Float ( float )
- Complex ( complex )
- Python Type Conversion in Numbers
- Validating or Type Checking of Numbers in Python
- Python Number Formatting
- Formatting Currency in Python
- Conclusion

## Integer (int)

In simple words, An Integer is a signed or unsigned number with no decimal point. For example 12, -5, -120 etc are considered as an Integer.

x = 1 y = 1000 z = -1000 print(x) print(y) print(z) print(type(x)) print(type(y)) print(type(z)) #PYTHON OUTPUT 1 1000 -1000 <class 'int'> <class 'int'> <class 'int'>

The variables `x`

, `y`

and `z`

all have integer values. To get the variable datatype to use the `type()`

function. And in this case, all these variables show that the variable belongs to class `int`

. If you are new to python then let me remind you that in Python everything is an object. Hence the variables are also object of class `int`

.

## Float (float)

You can recognize float numbers by their decimal point. Some of those are -1.56, 65.10 etc. In Python, all the **float numbers** belong to the class `float`

.

x = 1.75 y = 1000.20 z = -1000.20 print(x) print(y) print(z) print(type(x)) print(type(y)) print(type(z)) #PYTHON OUTPUT 1.75 1000.2 -1000.2 <class 'float'> <class 'float'> <class 'float'>

## Complex ( complex )

According to Wikipedia, A Complex numbers are written in the form of a+bj, where **a** and **b** are the real numbers and **j** is an imaginary number. Below is the example shown to create a complex in Python.

x = 12.12+2-2j y = 123.020*2j print(x) print(y) print(type(x)) print(type(y)) #PYTHON OUTPUT (14.12-2j) 246.04j <class 'complex'> <class 'complex'>

## Python Type Conversion in Numbers

salary = 15025 print(f"Type Cast to Float: {float(salary)}") print(f"Type Cast to Complex: {complex(salary)}") #PYTHON OUTPUT Type Cast to Float: 15025.0 Type Cast to Complex : (15025+0j)

## Validating or Type Checking of Numbers in Python

salary = 15025.05 print( type(salary) is float ) # returns True

## Python Number Formatting

There are many ways to format number inside strings some of them are given below.

salary = 15025.12345 print( "John's monthly salary is %0.1f" % salary ) print( "John's monthly salary is {:0.2f}".format(salary) ) print( f"John's monthly salary is {salary:0.3f}" ) #PYTHON OUTPUT John's monthly salary is 15025.1 John's monthly salary is 15025.12 John's monthly salary is 15025.123

## Formatting Currency in Python

When we talk about numbers than we must also take currency into consideration. Formatting numbers in currency form is quite easier and you can achieve this using the `format()`

string method.

print("${:,.2f}".format(150.2)) print("${:,.2f}".format(1500.2)) print("${:,.2f}".format(15000.2)) print("${:,.2f}".format(150000.2)) print("${:,.2f}".format(1500000.2)) print("${:,.2f}".format(15000000.2)) #PYTHON OUTPUT $150.20 $1,500.20 $15,000.20 $150,000.20 $1,500,000.20 $15,000,000.20

#### Note

There are also other alternative ways of currency formatting. I only covered the most basic of all and for more details on currency formatting, you can visit this stack overflow thread.

## Conclusion

In conclusion, we have come to the end of this post on **Python Numbers**. Comment below for your suggestions and queries.