Main Differences Between Python 2.x & Python 3.x

In my quest to expand my coding knowledge, I recently started dabbling in Python. For those who may not know, Python is an interpreted and easily readable language used predominately for web/software development, as well as data science. An interpreted language will directly execute code by converting it to “byte code”, increasing the speed of production. Created in ’91 by Guido van Rossum, it has gone through a few iterations before reaching its most current form — Python 3.

Python is used by a majority of the major companies that you can think of, and its popularity doesn’t seem to be dwindling soon.

But, wait! Can I still use Python 2?

A lot of companies still have Python 2 legacy code, as well as online learning materials having Python 2 and 3 code that they use for examples. Even if your company is slowly making the switch to Python 3 and you only know 2, the changes between them are minimal enough, and it will still take the companies some time before switching fully to Python 3.

So with that being said… here are the differences!

Syntax

The syntax in Python 3 vs Python 2 is arguably easier to understand, which in turn has made it easier for newer developers (or developers new to Python) to be able to understand and read back. After all, Python is known for its ability to be easily read!

Libraries

Most of the new Python libraries being written and created right now are strictly in Python 3, which will not be compatible with Python 2.

Print function

In the code snippet above, the change is the print() function in Python 3. If needing to use the Python 2 print statement, be sure to leave a space and use parentheses.

Dividing Integers

In Python 2, dividing integers was fairly simple and the output would also be an integer. However, unnoticed integer division changes in Python 3 could be dangerous to your code as no errors would be raised. It’s preferred to use a float when dividing integers to ensure the expected result.

Backwards Compatibility

This isn’t necessarily a syntactical difference, but a difference nonetheless. Python 3 is backwards compatible with Python 2, but not the other way around. With that being said, it is not the most reliable and can cause unexpected errors — or no error messages at all depending on the issue — which in turn can cause your code to work unexpectedly.

Iteration

Iterating over a sequence of numbers has been switched from xrange() in Python 2 to range() in Python 3. There are a few pros to this: Range() will provide a static list every time, as xrange() will reconstruct the list every time, as well as not being able to work with slice, etc. However, the pro to xrange() is that it takes up less memory when iterating over larger ranges.

Variable Leakage

If you are setting a value to a global variable in Python 2, once iterated through in a for loop, the variable can change. In Python 3, the global variable remains unchanged.

Resources

https://www.guru99.com/python-2-vs-python-3.html

Flatiron School alumni with a sociology and neuroscience background.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store