×

Division in Python

In this tutorial, we will understand what mathematical operation division is and how is it performed in Python Programming language. We will also focus on the operators being used in both the versions of python which are python 2 and python 3.

What is Mathematical Division

The division is a technique of allotting a cluster of belongings into equivalent shares. It is one of the four elementary mathematical processes, which gives an impartial result of the distribution. It takes two operands as input - one for the dividend (the no. that has to be divided) and the other for the divisor (one which divides).

The division works opposite to that of multiplication.

In multiplication, the resultant is 16 If 4 clusters of 4 are formed;

On the contrary, in the division, the resultant is 4 when 16 is divided into 4 equal clusters.

Example 1:  16 % 4 = 4


Here, 16 is the dividend
	4 is divisor
             4 is quotient


Example 2:  25 % 4 = 6 (rem=1)


Here, 25 is the dividend
	4 is divisor 
	6 is quotient
	1 is remainder

Python division

Python provides two ways to perform python division.

1.Integer Division

2.Float division

Let us try to understand both ways with the help of examples.

Integer division

Integer division gives out only the integer number.

Integer division gives out the base or floor of the division. That is the values until the decimal point gets encountered are considered and the rest are discarded.

The operator used for such operation is '//' in Python 3.

Illustration 1

# Integer division in Python 3
print (16//4) 


output for the above = 4

Division in Python

Illustration 2

# Integer division in Python 3
print (25//5) 

output for the above = 5

Division in Python

Python 2 does not support the ‘//’ operator.

It supports the ‘/ ‘operator for integer division.

Illustration-

# Integer division in python 2 using the '/ ' operator 
print 16/4  

output for the above = 4

Division in Python

To perform integer division using the ‘// ‘operator, one needs to import from a future module. It can be imported as-

Illustration-


# Integer division in python 2 using the '//'operator
from __future__ import division
print 16 // 4 

Output for the above = 4

Division in Python

Float Division

In Float division, the resultant is a floating-point estimate of the result of a division.

Output depends on the capacity of a float data type. The exact result is shown until the capacity of float gets exhausted. After that number, an approximate result is shown on the output screen.

Thus, it’s not possible to store every value after the decimal point. so, it is impossible to store an exact binary representation of various floating-point numbers. This can be considered a drawback of float division as it often leads to glitches when comparing numbers or when rounding.

The operator used by float division is ‘ / ‘

In Python 3,

# Illustration 1 to show float division in python 3
print ( 20 / 6 )

output of the above code = 3.33333333333

Division in Python
# Illustration 2 to show float division in python 3
print ( 35 / 6 )

output for the above = 5.83333333333

Division in Python

In Python 2,

The only typical division operator is '/'.

If both values are integers, the resultant value or quotient is also an integer.


# Illustration 1 to show floating point division using int
print 4 / 2

output = 2

Division in Python

The above code gives out the integer value and not the float value.


# Illustration 2 to show floating point division using int


print 16 / 3 

output = 5

Division in Python

The output of the above code should be 5.333333333333333 but instead, the output gives out is 3 only which is the integer value and the float value has been discarded.

To obtain a floating-point value, either of the values should be a float,

# Floating point division using one float value.
print 20 / 3.0

output = 6.666666666666667

Division in Python

The __future__ module can also be taken into account in python 2 to perform python division normally as python 3 does.

# Floating point division using future module
from __future__ import division
print 20 / 3 

output = 6.666666666666667

Division in Python

In this way, we can escape from the compulsion of adding a 0 after a decimal point and perform normal float division just as it is performed in python 3.


Related Topics

Python String splitlines() method

Python String splitlines() method The string.splitlines() method in Python splits the specified string and returns a list of the lines in the string, breaking at line boundaries.  Syntax splitlines([keepends]) Parameter keepends(optional): This parameter specifies if...

1 minute read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

How to read data from com port in python

Comport, the I/O interface is known as a COM port that allows the connection for a serial device to a computer. COM ports are sometimes referred to as serial ports....

3 minutes read.

Confusion Matrix Visualization Python

The confusion matrix is a two-dimensional array that compares the anticipated and actual category labels. These are the True Positive, True Negative, False Positive, and False Negative classification categories for...

4 minutes read.

Best Database for Python

Database The collection of structured data or information in an organized format in a computer system is known as Database. The data is inserted, deleted, updated, controlled, or manipulated in a...

6 minutes read.

Python Key Error

What is an Error? Errors are nothing but problems in the program which occur in a program code, and this will stop the execution of the program. It is also called...

6 minutes read.

Python getattr() function

Python getattr() function The getattr() function in Python returns the value of the named attribute for the given object. Syntax getattr(object, name[, default]) Parameter object: It is a required parameter which represents an object. name: This parameter represents the...

1 minute read.

PyShark in Python

Python: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

4 minutes read.

Python Built-in Functions

Python Built-in Functions The Python interpreter has a number of functions and types built into it that are always available. The Python built- in functions are as follow: Methods Explaining abs() The abs() function returns...

6 minutes read.

First Unique Character in a String Python

This article aims to introduce you to strings and how to create a string in Python, and then we will solve a simple yet exciting DSA (Data Structures and Algorithms)...

3 minutes read.

Inheritance in Python

Inheritance in Python Object-Oriented Programming provides reusable patterns to the code for restricting the redundancy in development projects. One of the basic principles of Object-Oriented Programming that helps achieve recyclable code...

8 minutes read.

How to print in same line in python

How to Print in the Same Line in Python To print in Python, we use the print () function and the syntax of print () goes like this: print (values, sep =...

3 minutes read.

Io stringio Python

Python programming language: Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a...

7 minutes read.

Django vs NodeJS

Django and NodeJS are open-source frameworks used to develop web applications. Both Django and NodeJS are cross-platform and robust technology used for developing versatile web applications and mobile applications. Django Django is...

2 minutes read.

_dict_ in Python

An unordered collection of data values known as a dictionary can be used in Python to store data values similar to a map. Dictionaries can also store a key: value...

6 minutes read.

Python Set update() method

Python Set update() method The set.update() method in Python updates the current set, by adding items from another set. If an element is common in both the sets, only one appearance of this item...

1 minute read.

Python Filter List

To filter a list, we use filter () method function. The filter () will test every element true or not in the sequence. Syntax: Filter(function, sequence) Parameters: Function: Function that check and verifies if...

2 minutes read.

Python ascii() Function

Python ascii() Function The ascii() function returns a readable version of any object (Strings, Tuples, Lists, etc). This function will replace any non-ascii characters with escape characters. Syntax ascii(object) Parameter object:  An object, like String, List, Tuple, Dictionary, etc. Return This...

1 minute read.

Python variance() function

Variance The variance is the average of the square deviations from the mean. The variance will measure the spread of the dataset from its mean or median value. The greater the...

4 minutes read.

API Requests using Python

What is an API? API stands for Application Programming Interface. It is commonly known as API. It provides an environment that helps two or more computer programs to contact each other....

5 minutes read.