__future__  module with division function

Here we are going to use the division function in Python2.x and Python3.x.

Let’s see the example in Python2.x. 

Python




# In 2.7 python compiler
print 7 / 5
   
print -7 / 5


Output:

1.4
-1.4

And let see this with the future module, it will give you the accurate result.

Python




# In below python 2.x code, division works
# same as Python 3.x because we use  __future__
   
from __future__ import division
     
print 7 / 5
print -7 / 5


Output:

1.4
-1.4

__future__ Module in Python

__future__ module is a built-in module in Python that is used to inherit new features that will be available in the new Python versions.. 

This module includes all the latest functions which were not present in the previous version in Python. And we can use this by importing the __future__ module. Its releases are in Python version 2.1. The basic idea of the __future__ module is to help migrate to use Python 3.X features. 

Note: The future statements must at the top of the file, otherwise the Python interpreter will raise SyntaxError

Similar Reads

Following features in this module:

Features Optional in Mandatory in nested_scopes 2.1 2.2 generators 2.2 2.3 division 2.2 3.0 absolute_import 2.5 3.0 with_statement 2.5 2.6 print_function 2.6 3.0 unicode_literals 2.6 3.0 generator_stop 3.5 3.7 annotations 3.7 3.11...

__future__ module with print_function

...

__future__  module with division function

Example 1:...

__future__  module with unicode_literals function

...