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!
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!
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.
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.
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.
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.
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.
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.
Important differences between Python 2.x and Python 3.x with examples - GeeksforGeeks
Division operator If we are porting our code or executing python 3.x code in python 2.x, it can be dangerous if integer…