Python 3, Python 2, what's the difference?


Python 3 is the latest version of Python, it is not backwards compatible with Python 2.

It adds great features like:

  • Unicode strings
  • Cleaned up standard modules
  • f-strings

Related course: Complete Python Programming Course & Exercises

Why shouldn't I use it, yet?

You should use Python 3.

Python 2 no longer receives official support. There are no updates or bugfixes for updates, it is end-of-life.

Python 2 is obsolete, so go for Python 3.0 or newer.

To check your current Python version, you can use the command

python --version

Python 2.6

Python 2.6 (when ran with the -3 flag) will give warnings about things you do which are deprecated in Python 3 and are not trivially fixed by running the '2to3' tools.

If you don't get any warnings running your applications and modules on Python 2.6 it is safe to assume that they will work on Python 3 after you run the '2to3' tools on them.

To be sure, just run it with python3.

Difference between Python2 and Python3

the print function

Python's print declaration has been replaced by the print() function, which means we have to add parentheses.

Python 2

import platform
print 'Python', platform.python_version()
print 'Hello, World!'
print('Hello, World!')
print "text", ; print 'print more text on the same line'

run result:

Python 2.7.6
Hello, World!
Hello, World!
text print more text on the same line

Python 3

import platform
print('Python', platform.python_version())
print('Hello, World!')
print("some text,", end=""")
print(' print more text on the same line')

run result:

Python 3.4.1
Hello, World!
some text, print more text on the same line

get python version

the input function

Parse user input by calling input().

Fortunately, the problem of storing the user's input as a str object has been solved in Python 3.

To avoid the dangerous behavior of reading non-string types in Python 2, we had to use raw_input() instead.

Python 2, Python 2.7.6

>>>> my_input = input('Enter number: ')

Enter number: 123

>>> type(my_input)
<type 'int'>

>>>> my_input = raw_input('Enter number: ')

Enter number: 123

>>> type(my_input)
<type 'str'>

Python 3, Python 3.4.1

>>>> my_input = input('Enter number: ')

Enter number: 123

>>> type(my_input)
<class 'str'>

python 3 uses input() instead of raw_input()

rounding numbers

In Python 3 / indicates true division, % indicates surplus, // indicates floor division (result is rounded).

In Python 2 / indicates result based on division by decimal places, // also indicates floor division)

Python 2

print 'Python', python_version()
print '3 / 2 =', 3 / 2
print '3 // 2 =', 3 // 2
print '3 / 2.0 =', 3 / 2.0
print '3 // 2.0 =', 3 // 2.0

run result:

Python 2.7.6
3 / 2 = 1
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

Python 3

print('Python', python_version())
print('3 / 2 =', 3 / 2)
print('3 // 2 =', 3 // 2)
print('3 / 2.0 =', 3 / 2.0)
print('3 // 2.0 =', 3 // 2.0)

run result:

Python 3.4.1
3 / 2 = 1.5
3 // 2 = 1
3 / 2.0 = 1.5
3 // 2.0 = 1.0

xrange

In Python 3, range() is implemented like xrange() so that a dedicated xrange() function no longer exists.

In Python 3, xrange() throws a naming exception.

Python 3.7.7 (default, Mar 13 2020, 10:23:39) 
[GCC 9.2.1 20190827 (Red Hat 9.2.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> xrange()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'xrange' is not defined
>>>

So you can do this:

>>> list(range(0,4))
[0, 1, 2, 3]