
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Division Operators in Python
The Python division operator is used to divide two integer values and return a quotient. To perform a division operation, we use the / symbol. For example, 16 divided by 4 is written as 16 / 4, which returns 4 as the quotient. Here, 16 is known as divident and 4 is known as divisor.
The division operator is one of the arithmetic operator. When we divide an integer by zero, it will raise a ZeroDivisionError exception in Python. For example, if we try to divide 55 by 0 using 55 / 0, it will raise a ZeroDivisionError exception.
Types of Division
In Python, there are two types of divisions ?
- Floor Division
- Float Division
Python Floor Division
Floor division is performed using the // symbol, which returns the largest integer less than or equal to the result. If either number is a float, the result will be a float. If a negative number is involved, the result will be rounded down to the nearest integer (towards negative infinity).
Following is a syntax of the Python floor division ?
var_1 // var_2
where, var_1 and var_2 are numeric values.
Example
Following is an example of the Python Floor division ?
num_1 = 19 num_2 = 2 num_3 = 12.5 num_4 = 2 floor_div1 = num_1 // num_2 floor_div2 = -num_1 // num_2 floor_div3 = num_3 // num_4 print("Floor Division :",floor_div1) print("Floor division when one number is negative :",floor_div2) print("Floor division when one number is float :",floor_div3)
Following is the output of the above code ?
Floor Division : 9 Floor division when one number is negative : -10 Floor division when one number is float : 6.0
Float Division
The float division is a normal division operation, which is performed using / symbol and returns a float number irrespective data type of the numbers.
Following is a syntax of the Python float division ?
var_1/var_2
Where var_1 and var_2 are two numeric values.
Example
Following is an example of the Python float division ?
var_1 = 44 var_2 = 3 var_3 = -85 var_4 = 7 float_div1= var_1 / var_2 float_div2 = var_3 / var_4 print("Float division :",float_div1) print("Float division with one value is negative number :",float_div2)
Following is the output of the above code ?
Float division : 14.666666666666666 Float division with one value is negative number : -12.142857142857142