These are a few illustrative examples of differences between Python 2 and 3.
Back to the main Python 3 Update Wiki
Python 2 | Python 3 |
---|---|
>>> print "Hello world" Hello world >>> print("Hello world") #This also works Hello world | >>> print "Hello world" File " |
>>> 5/4 1 >>> 5//4 1 | >>> 5/4 1.25 >>> 5//4 1 |
>>> easyas = [1,2,3] >>> simpleas = ['a','b','c'] >>> result = zip(easyas,simpleas) #creates a list >>> print result [(1, 'a'), (2, 'b'), (3, 'c')] >>> result[1] (2, 'b') | >>> easyas = [1,2,3] >>> simpleas = ['a','b','c'] >>> result = zip(easyas,simpleas) #creates an iterable >>> print(result) |
>>> import types >>> var = "A string" >>> if type(var)==types.StringType: ... print("It's a string!") ... It's a string! >>> if isinstance(var,str): ... print("Also says its a string") ... Also says its a string | >>> import types #As we'll see, this isn't needed >>> var = "A string" >>> if type(var)==types.StringType: ... print("It's a string!") ... Traceback (most recent call last): File " |
>>> if "1" > 100000: ... print("This is a bad thing Python 2 does") ... This is a bad thing Python 2 does | >>> if "1" > 100000: ... print("Python 3 rightly complains") ... Traceback (most recent call last): File " |