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 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.

Python 3

Lang Detail

  1. Print Version String
  2. Quoting String
  3. String Operations
  4. Format String
  5. Operators
  6. Object, ID, Type

Misc

  1. Traverse Directory
  2. Sort List, Matrix, Object
  3. Python 3: Map with Side Effect Doesn't Work If Result is Not Used
  4. Python 3 Closure
  5. Python 2 and 3 Difference