Python includes three numeric types to represent numbers: integers, float, and complex numbers. In this numbers in python tutorial, we will discuss creating numeric data type in python, the different numbers used in Python, how to convert from one data type to the other, and the mathematical operations supported in Python.

### Numbers in Python

Number data types store numeric values. They are immutable data types, which means that changing the value of a number data type results in a newly allocated object.

Python supports integers, floating-point numbers and complex numbers. They are defined as int, float, and complex classes in Python.

Integers and floating points are separated by the presence or absence of a decimal point. integers are zero, positive or negative whole numbers without a fractional part and having unlimited precision

Floating point numbers (float) are positive and negative real numbers with a fractional part denoted by the decimal symbol. or the scientific notation

For instance, 7 is an integer whereas 7.0 is a floating-point number.

A complex number is a number with real and imaginary components. Complex numbers are written in the form, a + bj, where a is the real part and b is the imaginary part and j represents the square root of -1.

```
#Assign value into variable
var1=13
var2=17
```

## Number Type Conversion

int(x) :- x convert to integer

long(x) :- x convert to long integer. There is no ‘long integer’ in Python 3 anymore.

float(x) :- x convert to complex number number and imaginary part is zero (0)

complex(x,y) :- x & y to a complex number with real part x and imaginary part y.

Please read the related topic for more understanding of Python

Python Introduction

Python Basic Syntax

Keywords and Identifiers

Variables and Data Types Numbers in Python

Let’s see the below example for converting numbers in python.

```
#Convert to integer
x = int(19.90)
print(x)
```

Output – 19

```
#Convert to long integer
y = float(78.897)
print(y)
```

Output – 78.897

```
#Convert to complex number
z = complex(10,8)
print(z)
```

Output – (10+8j)

## Checking Data Type in Python

We can use the type() function to know which class a variable or a value belongs to and isinstance() function to check if it belongs to a particular class.

Please see the below example for the convert data type.

```
#Checking Data Type in Python
type(x)
type(y)
type(z)
```

The above code will return the type of data type in python.

```
print(isinstance(x, int))
print(isinstance(y, complex))
```

The above code will return true or false.

## Arithmetic Operators

Arithmetic operators are used to performing mathematical operations like addition, subtraction, multiplication and division.

Operator | Description | Syntax |

+ | Addition: adds two operands | x + y |

– | Subtraction: subtracts two operands | x – y |

* | Multiplication: multiplies two operands | x * y |

/ | Division (float): divides the first operand by the second | x / y |

// | Division (floor): divides the first operand by the second | x // y |

% | Modulus: returns the remainder when the first operand is divided by the second | x % y |

** | Power: Returns first raised to power second | x ** y |

Let’s see each of one example in the below code.

```
#Addition
4 + 6
#Subtraction
8 - 5
#Multiplication
2 * 5
#Division
9 / 3
#Floor Division
7 // 4
#Modulus
9 % 2
#Power
2 ** 3
```

#### Other import function numbers in Python

Python has a built-in module that you can use for mathematical tasks.

The math module has a set of methods and constants.

In this case, We need to import math module.

**abs(x)** – Absolute value of x. distance between x and zero.**ceil(x)** – Ceiling of x the smallest not less than x

```
abs(-65.14)
abs(100.78)
math.ceil(-65.14)
math.ceil(100.78)
```

**pow(x,y) **– The pow() function returns the value of x to the power of y (x^{y}).

```
pow(3, 3)
```

**divmod(x,y)** – The divmod() method takes two numbers and returns a pair of numbers (a tuple) consisting of their quotient and remainder.

```
divmod(190, 100)
```

**trunc(x)** – The math.trunc() method returns the truncated integer part of a number.

Note: This method will NOT round the number up/down to the nearest integer, but simply remove the decimals.

```
math.trunc(-65.9)
math.trunc(100.8)
```

**round(x, [n]) **– x rounded to n digit, rounding half to even if n is omitted it defaults to 0

```
round(10.9)
round(10.789,2)
```

**floor(x) **– floor() method in Python returns the floor of x i.e., the smallest integer not lesser than x.

```
math.floor(10.9)
math.floor(10.1)
```

Please download the example of Numbers in Python

Numbers in Python

Numbers in Python