While Carleton College has a good guide about the differences that new students will likely see between the two, and there is an excellent blog article about why Python 3 hasn’t been more widely adopted, and the official Python site tries to explain the differences between the two versions; I have not been able to find any guides on how to write code that will automatically work in Python 3.x and the latest version of 2.x. This is surprising, because one of the ways to get past the “gravity” of Python 2, is for people to learn to code in a manner that works for both 2 & 3. So I decided to write a short guide myself.
Note: If I am missing some of the differences between the languages in this guide, I truly would appreciate feedback so I can make this guide more comprehensive. Also, if I am just technically wrong about something, please tell me nicely 🙂 But, I’m not going to get into a war about whether what I’m espousing here is stylistically right. I’m espousing it, because while I would rather just do things the Python 3 way, this won’t happen until Python 2 is no longer the dominant language. And so I think it is legitimate to teach new coders how to write something that just works in both, and then have them learn the “right” way for the particular version later.
Use Print as a Function, not a Statement
The newer versions of Python 2 support print as a function, so there is no reason to ever write code that treats print as a statement. So, when coding in python 2.x or 3.x, the following should be your first program:
Always Divide Floats in Python with the / and use // to do Integer Division
Many programming languages have counter-intuitive results when you divide two integers, of having a result that is an integer. This is the case in Python 2.x where 3/2 will result in 1 instead of 1.5. And while Python 3 now will have 3/2 result in 1.5, which is more intuitive, it can clearly cause coding issues. Thus as a best practice, whenever doing division you should make sure that you always specify that the numbers are floating point. Here are two ways of doing this, with the first example being something that can work for variables also, and the second example being shorter, but only works when typing the number themselves:
float(3)/float(2) or 3.0/2.0
will result in 1.5 as an answer in both Python 2.x and 3.x
If you want to have an integer result, with no “remainder” or fractional component, use the // because this works in both Python 2.x and 3.x:
3 // 2 or 3.0 // 2.0
will result in 1 as an answer in both Python 2.x and 3.x
Use Eval(Input()) when wanting the 2.x meaning of Input()
If you use eval(input()) you will get the equivalent of the 2.x input function in either Python 2.x or 3.x.
Make an Alias to Raw_Input in 3.x
Of all the cross-version methods I share, this is the one I like least. Python 3 doesn’t have “raw_input” because it just uses input(). The only way to get raw_input to work is to make a function that calls it. I’m working on writing a function that can be generically copied into any python program that will do this.
Use Open() and don’t use File()
Open works in Python 2.x and 3.x, the File() function went away in 3.x.
Use Range() and don’t use Xrange()
There is no need to use xrange() in Python 3 (and it doesn’t exist), so just use range() in both python 2.x and 3.x, and while it may not be as efficient in 2.x, it will still work, and hopefully Python 2 will have its “gravity” broken soon, and Python 3 will finally take off!
As of Python 3.3, the u prefix is supported again. Thus, if you follow the Python 2.x standard of using the u prefix for unicode, it will run just fine in newer Python 3.x. Although, I know PEP 414 warned us about people like me suggesting to do this…
But, I want to be clear, my goal is to have new coders be able to not worry about which version of Python they are using. Advanced coders should know better 🙂