C RUBY-ON-RAILS MYSQL ASP.NET DEVELOPMENT RUBY .NET LINUX SQL-SERVER REGEX WINDOWS ALGORITHM ECLIPSE VISUAL-STUDIO STRING SVN PERFORMANCE APACHE-FLEX UNIT-TESTING SECURITY LINQ UNIX MATH EMAIL OOP LANGUAGE-AGNOSTIC VB6 MSBUILD

# Using integer division in Python

By : ki min kim
Date : November 21 2020, 12:01 PM
Hope that helps Python2.7 automatically uses the / operator as integer (whole number) division, which will always produce a whole number.
For example:
code :
``````Area = 1.0/2 * Base * Height # 1.0 is a float type, 1 is an integer type
``````
``````Area = Base * Height * (1.0/2.0)
``````
``````Base * Height * (0.5)
= 192 * 0.5
= 96
``````
``````Base * Height * (1/2)
= Base * Height * (0) # since 1/2 rounds down to 0 in integer division
= 192 * 0
= 0
``````
``````from __future__ import division
``````
``````>>> from __future__ import division
>>> 1/2
0.5
``````

Share :

## Integer division instead of regular one in Python

By : spiral_polo
Date : March 29 2020, 07:55 AM
this one helps. In Python 2, dividing 2 integers will always produce an integer. (In Python 3, you get "real" division)
You can rewrite your code as:
code :
``````[n/2.0 for n in range(100, 200)]
``````
``````[float(n)/othervar for n in range(100, 200)]
``````
``````from __future__ import division
``````

## Detecting integer division in python

By : Patrick Ray
Date : March 29 2020, 07:55 AM
To fix this issue I have to make the Python 2 to 3 jump. Integer division is the only thing that has me scared. Our test suite is OK, but not necessarily set up to scrutinize division. Is there some sort of wrapper or environment or hack from which I could run scripts that can detect when integer division is used? I mostly use Anaconda Python 2.7 on Windows. , You can run your code with the python 2 engine but with -3 switch:
code :
``````L:\>Python27_X64\python.exe -3 test.py
test.py:1: DeprecationWarning: classic int division
i = 2/3
``````
``````from __future__ import division
``````
``````if False:
i = 2/3
``````
``````with open("toto.txt","wb") as f:
f.write("foo")
``````

## Integer modulus division and integer division remainder with Ballerina

By : user3295226
Date : March 29 2020, 07:55 AM
Any of those help You can get the modulus by using / operator and remainder using % operator.
Check the following code:
code :
``````import ballerina/io;

public function main() {
int i = 10;
int j = 3;

int x = i / j;
int y = i % j;

io:println("i / j = " + x.toString());
io:println("i % j = " + y.toString());
}
``````
``````i / j = 3
i % j = 1
``````

## Integer division in Python

By : Ricardo Tealdi
Date : March 29 2020, 07:55 AM
I hope this helps you . From the documentation:

## Why does Python 2 use '/' only as integer division?

By : Medhi Erraki
Date : March 29 2020, 07:55 AM
it should still fix some issue Python 2's / operator is either integer or floating point, depending on the types of the operands.