×

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 Ways to find nth occurrence of substring in a string

Introduction In Python, a string is a collection of characters that can be employed to conduct additional operations. In Python, a substring is a group of characters that are a part...

4 minutes read.

Python memoryview() Function

Python memoryview() Function The memoryview() function in Python returns a memory view object from a specified object. Syntax memoryview(obj) Parameter obj: This parameter represents a Bytes object or a bytearray object. Return This function returns a “memory view” object...

1 minute read.

How to Create an Array in Python?

How to Create an Array in Python In the previous article, we have discussed how to declare an array in Python. So, let's have a quick revision of that, and then we...

5 minutes read.

Program of Cumulative Sum in Python

Introduction This tutorial, explains what is meant by lists, the cumulative total, several approaches to calculating the cumulative sum of digits in a list, etc. Learn the straightforward Python codes for...

5 minutes read.

Difference between Python and CPP

Difference between Python and C++ We all know that both C++ and Python are two of the most popular programming languages. Both C++ and Python shares some basic similarities such as...

6 minutes read.

Exrex 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...

4 minutes read.

Working with JSON in Python

Python is an Object-Oriented high-level language. Python is designed to be highly beginner-friendly. Python has an English-like syntax, which is very easy to read. In this article, we are going...

4 minutes read.

How to Pass a list as an Argument in Python

Lists in Python A list is a datatype in python which is used to store the data in a sequence. The lists are mutable; that is, we can change the values...

3 minutes read.

Python String title() method

Python String title() method The string.title() method in Python returns a string where the first character in every word is upper case. If the word contains a number or a symbol,...

1 minute read.

Python Queue

Python Queue There are various day to day activities where we find ourselves engaged with queues. Whether it is waiting in toll tax lane or standing on the billing counter for...

7 minutes read.

EDA in Python

The EDA is the exploratory data analysis; the data scientists mainly use the EDA to understand the main features of the data quickly, the variables in python and the relationship...

6 minutes read.

Commands in Python

In this tutorial, we will see some of the widely used python commands along with their syntaxes and examples. To make Python more user-friendly, developers have provided these commands to...

12 minutes read.

Subprocess in Python

In this tutorial, we will understand what is a subprocess in python and will understand how to use it. Subprocess A subprocess is a very prevailing portion of the python library. It...

3 minutes read.

Abstraction in Python

What is meant by abstraction generally? A very general notion of a thing or work is known as abstract. To be more precise, the process of having a brief idea but...

4 minutes read.

ComboBox in Python

Python includes several graphical user interface (GUI) libraries, including PyQT, Tkinter, Kivy, WxPython, and PySide. Tkinter is the most commonly used GUI module in Python because it is simple and...

2 minutes read.

How to add 2 lists in Python?

In Python, a list is defined as a data structure that contains a sequence of elements. It can contain any kind of data type inside it but in order to concatenate two...

3 minutes read.

Change Data Type in Python

Python is a dynamic language where it is not always required to consider every variable type. Python supports a wide range of data types, but There are mainly six data...

3 minutes read.

Python Modules List

Python is like an ocean. If you have been working with Python, you might’ve already heard the word module. When we solve a problem in mathematics, there will be several...

3 minutes read.

Python List remove() method

Python List remove() method The list.remove () method in Python removes the item at the specified position in the given list. Syntax list.remove(x) Parameter x: This parameter represents the element you want to remove and accepts any type...

1 minute read.

Python Function

Python Function A Python function is a reusable, organized block of code that is used to perform the specific task. The functions are the appropriate way to divide an extensive program into a...

7 minutes read.