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 "<stdin>", line 1
print "Hello world"
^^^^^^^^^^^^^^^^^^^
SyntaxError: Missing parentheses in call to 'print'.
Did you mean print(...)?
>>> print("Hello world")
Hello world |
>>> 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) <zip object at 0x000001F70D6391C0> >>> result[1] #Cannot index into iterable Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'zip' object is not subscriptable >>> list(result)[1] (2, 'b') |
>>> 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 "<stdin>", line 1, in <module>
AttributeError: module 'types' has no attribute 'StringType'
>>> if isinstance(var,str):
... print("The Python 3 way")
...
The Python 3 way
|
>>> 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 "<stdin>", line 1, in <module>
TypeError: '>' not supported between instances of 'str' and 'int' |
