The well-known differences between Python 2.7 and Python 3 is the famous print function. While in some widely-used machine-learning like tensorflow and PyTorch, the print functions are regulated to the same form. At these circumstances, how can we tell the big version quickly from the codes without sometime putting a great effort to install a version of Python 3 only to find that it is run on version 2.7.
Definition of class
the definition of the super inherited function differs:
1 | #!/usr/bin/pyhon3 |
1 | #!/usr/bin/python27 |
rounding operation
The / operation for integers in Python2.7 is by default the rounding operation.
1 | #!/usr/bin/python27 |
In Python 3, you should do this as equivalence:
1 | #!/usr/bin/python3 |
str format
The old style is something like
1 | '%d %d' % (1, 2) |
output:1 2
The new style:
1 | '{} {}'.format(1, 2) |
output:1 2
The new style allows re-arranging:
1 | '{1} {0}'.format(1,2) |
output:2 1
pickle package
Before Python 3, one should import cPickle as pickle. Python3 integrated pickle for object serialization.
Encoding:
py2, default latin1: pickle.load(f)
py3, default utf-8: pickle.load(f, encoding=’latin1’)
xrange
In python2, xrange()
Python3 range()
Comments
shortnamefor Disqus. Please set it in_config.yml.