Oct 10, 2018

[HDGEM] PYTHON 2.X VS. PYTHON 3.X

 Python comes in two basic flavors these days—Python 2.x and Python 3.x. This is an important difference— some code written for one won't run on the other. 

However, most code is interchangeable.

Here are some of the key differences:


Python 2.x
Python 3.x
print "hello" (print is a keyword)
print("hello") (print is a function)
except Exception, e: # OR except Exception as e
except Exception as e: # ONLY
Naming of Libraries and APIs are frequently inconsistent with PEP 8
Improved (but still imperfect) consistency with PEP 8 guidelines
Strings and unicode
Strings are all unicode and bytes type is for unencoded 8 bit values

There is a utility called 2to3.py that you can use to convert Python 2.x code to 3.x, while the '-3' command line switch in 2.x enables additional deprecation warnings for cases the automated converter cannot handle. Third party tools like python-modernize and the 'six' support package make it easy to target the large common subset of the two variants for libraries and applications which support both 2.x and 3.x.

--
Posted By Blogger to HDGEM at 3/08/2017 07:29:00 AM