Numbers in Python

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

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)

Output – 19

#Convert to long integer
y = float(78.897)

Output – 78.897

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

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

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.

+Addition: adds two operandsx + y
Subtraction: subtracts two operandsx – y
*Multiplication: multiplies two operandsx * y
/Division (float): divides the first operand by the secondx / y
//Division (floor): divides the first operand by the secondx // y
%Modulus: returns the remainder when the first operand is divided by the secondx % y
**Power: Returns first raised to power secondx ** y
Arithmetic Operator

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

4 + 6

8 - 5

2 * 5 

9 / 3

#Floor Division
7 // 4 

9 % 2

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


pow(x,y) – The pow() function returns the value of x to the power of y (xy).

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.


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


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


Please download the example of Numbers in Python
Numbers in Python
Numbers in Python

Leave a Comment

Your email address will not be published. Required fields are marked *