Python 2.x is legacy, Python 3.x is the present and future of the language



Feature Python 2.x Python 3.x
print print "a string" print("a string")
integer division 3 / 2 # 1 3 // 2 # 1
float division 3.0 / 2 # 1.5 3.0 / 2 # 1.5
user input raw_input("> ") input("> ")


Feature Python 2.x Python 3.x
iterating through dict for key in d.iterkeys(): for key in d:
iterating through dict for value in d.itervalues(): for value in d.values():
iterating through dict for (key, value) in d.iteritems(): for (key, value) in d.items():
dict as a list keylist = d.keys() keylist = list(d)
dict as a list valuelist = d.values() valuelist = list(d.values())


Feature Python 2.x Python 3.x
xrange for i in xrange(10): for i in range(10):
range for i in range(10): for i in list(range(10)):
map new = map(f, old) new = list(map(f, old))
filter new = filter(f, old) new = list(filter(f, old))


Feature Python 2.x Python 3.x
raising exceptions raise ValueError, "something is wrong" raise ValueError("something is wrong")
catching exceptions except ValueError, e: except ValueError as e: