Now if after importing print_function from __future__ we attempted to use the print function, it will never behave like a statement again which is the native Python 2 behavior
Just as in Python 3, print"hello", errors out. See below
In Python 3: if we were to import print_function from __future__
_from __future__ import print_function _it would just ignore it.
Nothing changes in the behavior of Python 3 by importing the print function from __future__
Division
In Python 2, floor division drops the remainder and returns only a whole number.
Python 2 thinks that you are dividing an integer by an integer, and that you probably want and integer back and drops the remainder.
If, instead, we were to provide one of the numbers as a float, the returned result would be a float too.
A floating point number aka "float", is a number that includes decimal portion.
Again, if we wanted to have Python 2 behave like Python 3, we may import the **division __future__ **module
We import division from __future__
After importing the **division module from __future__ **and performing a division that would yield a remainder we can observe that **Python 2 **is outputting the expected remainder
Additionally, we can observe that if we escape the / (backslash) we may receive the output without the remainder again.
we may use double slash '//' and receive the truncated remainder output
Why would we want to do this? Convenience, sometimes you would want to get one output or the other
To provide our Python 2 scripts with some forward compatibility, we may use the following line in our scripts:
from __future__ import absolute_import, division, print_function
Input Function
If I wanted to get input from an user, assigning it to variable X:
We may prompt the user to type something:
we would get a name error saying that "hello" is not defined
The error is letting us know that that python does not have any objects named "hello"
If now, in contrast we do create an object called hello and assign it the value 242
The value we initialized (242) and assigned to hello is now "x"
This time we get no error because hello has been previously defined
It would not be uncommon to think that x = hello right?
No. Why? Because it assigned to X the value of the hello object we had previously created
As you can imagine this behavior is not useful.
raw_input function
The raw_input function helps us in this case. Fixes the issue because using raw input, when I type something
and I check the value of x, it is being assigned to what I typed.
This is typically what you want.
In Python 3:
The behavior raw_input would have in Python 2 is default in Python 3
In Python 3: the functions of raw_input are performed by input. raw_input does not exist in Python 3.
raw_input does not exist in Python 3.
How to run a script that works consistently on both versions
We may leverage the fact that raw_input creates an error in python 3 and we can tell it to try raw input and catch the exception and NameError, use input function instead.
Attempt to use raw_input, otherwise, in case of NameError use input()
The following snippet was tested on Python 2.7 and Python 3.8 and demonstrate this concept:
from __future__ import absolute_import, division, print_function