In [15]:
import sys
print(sys.version)


2.7.12 (default, Nov 29 2016, 14:36:14) 
[GCC 4.2.1 Compatible Apple LLVM 7.3.0 (clang-703.0.31)]

"Features" of python2 that will break in python3


In [1]:
print "is no longer a statement"


is no longer a statement

In [5]:
type(range(5)) is list


Out[5]:
True

In [9]:
1 / 2 == 0  # WHY??!!


Out[9]:
True

In [16]:
# methods for looping through dictionaries are iterators by default
mydict = {'a': 1, 'b': 2, 'c': 3}
for key, value in mydict.iteritems():
    print key, value


a 1
c 3
b 2

Backwards incompatible features of python3


In [8]:
# is it a feature or a bug?!
'a' < 1


Out[8]:
False

In [4]:
# magic dictionary concatenation
some_kwargs = {'do': 'this', 
               'not': 'that'}
other_kwargs = {'use': 'something', 
                'when': 'sometime'}
{**some_kwargs, **other_kwargs}


  File "<ipython-input-4-c2f69bab01f6>", line 6
    {**some_kwargs, **other_kwargs}
      ^
SyntaxError: invalid syntax

In [5]:
# unpacking magic
a, *stuff, b = range(5)
print(a)
print(stuff)
print(b)


  File "<ipython-input-5-b1fd049aae3b>", line 2
    a, *stuff, b = range(5)
       ^
SyntaxError: invalid syntax

In [17]:
# no unicode variable names
π = 1


  File "<ipython-input-17-26c3a11bfa36>", line 2
    π = 1
    ^
SyntaxError: invalid syntax

In [19]:
# no infix matrix multiplication
import numpy as np
A = np.random.choice(list(range(-9, 10)), size=(3, 3))
B = np.random.choice(list(range(-9, 10)), size=(3, 3))
A @ B


  File "<ipython-input-19-9f9779c5894d>", line 5
    A @ B
      ^
SyntaxError: invalid syntax

Back to the __future__ in python2


In [6]:
# will print tuple and will truncate division operation
print('non-truncated division in a print function: 2/3 =', 2/3)


('non-truncated division in a print function: 2/3 =', 0)

In [7]:
from __future__ import print_function, division
print('non-truncated division in a print function: 2/3 =', 2/3)


non-truncated division in a print function: 2/3 = 0.666666666667