Python 2 and Python 3 Difference

By Xah Lee. Date: . Last updated: .

the 2 big major difference between python 2 and 3 are:

That's basically the major differences. There are other, minor ones, but most are also incorporated into python 2.x.

Convert python 2 to 3

use the script at /usr/bin/2to3.

For example, 2to3 filename

It is installed if you have python3 installed.

Porting Python 2 Code to Python 3

Here's official doc:

Porting Python 2 Code to Python 3 — Python v3.3.3 documentation

Here's a example python 2 to python 3 conversion differences, just to give a idea.

-            os.makedirs(dirName,0775)
+            os.makedirs(dirName,0o775)

-    u"""Generate thumbnail images.
+    """Generate thumbnail images.

-    print '</a>'
+    print('</a>')

-    txt_segs = re.split( re.compile(r'src', re.U|re.I), unicode(FF.read(), 'utf-8'))
+    txt_segs = re.split( re.compile(r'src', re.U|re.I), str(FF.read(), 'utf-8'))

-        match_result = re.search(ur'\s*=\s*\"([^\"]+)\"', link_block, re.U)
+        match_result = re.search(r'\s*=\s*\"([^\"]+)\"', link_block, re.U)

-    for im in filter(lambda x : (not x.startswith('http')) and (not x.endswith('icon_sum.gif')), get_inline_img_paths(dPath + '/' + fName)):
+    for im in [x for x in get_inline_img_paths(dPath + '/' + fName) if (not x.startswith('http')) and (not x.endswith('icon_sum.gif'))]:

If you have a question, put $5 at patreon and message me.