Python 2 and Python 3 Difference

By Xah Lee. Date: . Last updated: .

The major differences between python 2 and 3 is that python 3 made string into a Unicode type.

This has major consequences. It is not trivial to convert non-trivial python software from python 2 to 3. (say, code that's more than say 1k lines.)

Python 3 also introduced lots other minor changes.

Here's a list of the major notable differences.

String, Sequence of Bytes vs Unicode Characters

In python 2, a string of this syntax "abc" is type str, and it is a sequence of bytes. (can think of it as ASCII Characters) For unicode string, its syntax is u"abc", and type is unicode.

In python 3, string is a sequence of unicode characters. Its type is unicode. You don't need to use the u in u"…".

This change, is not just about syntax, but has pervasive impact on the language. If your code deal with string and byte a lot, it's not trivial to convert to python3.

print type("abc")
# <type 'str'>

print type(u"abc")
# <type 'unicode'>
print( type ("abc") )
# <class 'str'>

Print is Now a Fuction

In python 2, print is a statement, no need parenthesis, like this: print arg1, arg2.

In python 3, print is a function, need parenthesis, like this: print(arg1, arg2). Separator for between args can be specified, and ending char can be specified.

print( "a", "b", sep=",", end="\n")
print( "c")

# prints

# a,b
# c

Division of Integers

In python 2, 3/2 return 1.

In python 3, 3/2 return 1.5. The division operator automatically convert integers to float. To get floor division, use //, example: 3//21.

Map Returns a Iterator

Many functions that used to return list in python 2 now returns iterator. They include:

The following in python 2 are removed:

To get a list from iterator, call list(iter)

In python 2, map returns a list.

x = map(lambda x:x+1, [3,4,5])
print x # [4, 5, 6]

In python 3, map return a iterator.

x = map(lambda x:x+1, [3,4,5])
print(x) # <map object at 0x10403a3c8>

xrange gone

Python 2 have range and xrange.
The range returnes a list, xrange returnes a iterator object.

Python 3 only have range, it returns a iterator object.

x = range(3)

print x
# [0, 1, 2]

print type(x)
# <type 'list'>

y = xrange(3)

print y
# xrange(3)

print type(y)
# <type 'xrange'>
x = range(3)

print(x)
# range(0, 3)

# convert it to list
print(list(x))
# [0, 1, 2]

print(type(x))
# <class 'range'>

file() function gone

file() function is gone. use open() instead.

raw_input gone

Python 2 have raw_input() and input()

Python 3 only have input()

.next() method gone

Python 2 have .next() method and next() function.

Python 3 only have next() function.

x = iter([3,4,5])
print( next(x) ) # 3

Leaked Var in List Comprehension

In python 2, variable name in list comprehension will leak if there's a global variable of the same name.

x = 1
[ x+1 for x in [1,2,3]]
print( x ) # 3

Fixed in python 3.

x = 1
[ x+1 for x in [1,2,3]]
print( x ) # 1

Raising Exceptions

raise IOError, "wrong"
raise IOError("wrong")

urllib module rename

The urllib module has been split and renamed to:

Convert python 2 to 3

Use the tool at /usr/bin/2to3. It is installed if you have python3 installed.

2to3 filename

It's helpful for small scripts, or to get an idea which part needs to change.

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

python 2to3 2020-12-17 ZPFx4
python 2to3, 2020-12-17