division
- Python 2 (By default integer division)
assert 1 / 2 == 0
- Python 3 (By default true division)
assert 1 / 2 == 0.5
- Remedy for Python 2 (forward compatible to Python 3)
from __future__ import division
- Python 2 (no parentheses)
print 'I am a print function'
- Python 3
print('I am a print function')
- Remedy
from __future__ import print_function
range and xrange
- Python 2 (
xrange
runs identity check)
assert xrange(5) != xrange(5)
- Python3 (
range
runs equality check)
assert range(5) == range(5)
input and raw_input
- Python 2 (
input()
depends on input type)
a = input('a') # ->> 123
print type(a) # <type 'int'>
a = input('a') # ->> '123'
print type(a) # <type 'str'>
- Python 2 (
raw_input()
) and Python 3 (input()
)
Always returnstr
output.
PREVIOUSLinux