Python 2 vs Python 3 - Which to Choose?

A common question asked is "Which version of python should I use if I am starting to learn python?"
The answer is doesn't really matter that much, though pick Python 3 as this will be language going forward and will save you relearning habits!

The reason for this is that as a beginner there are only a few key differences you need to consider:

The Print Statement

This is the first difference a user will likely encounter, in python 2 the parentheses for print statements are optional. For python 3 this is required and will raise an error if no brackets are included. It is a good habit to always include them encase you want to run code in 2 and 3.

Python 2
print "hello"
or
print ("hello")

Python 3
print ("hello")

The Input Statement

The name of the input statement has changed from raw_input to input. This example sets a varaible called "name" to the string of the input text.

Python 2
name = raw_input("Please enter your name: ")

Python 3
name = input("Please enter your name: ")

Data Types

Division of integers in python 2 will return an integer. In python 3 division will return a float. This may cause a different behaviour then expected when switching code between 2 and 3.

Example Code
x = 3
y = 11

x / y
Python 2 Returns
0  
Python 3 Returns
0.2727272727272727

Libraries

One of the biggest things that slowed down the move from 2 to 3 for a lot of users was that libraries took time to transition to 3. This give some developers little choice in the decision on which version to use. The situation now is much better with most libraries working in both 2 and 3 or version or equivalent libraries existing on both sides of the version divide. Users do need to take care with library names as they may change slightly between versions. For example the popular tkinter library in 2 and 3 are different due to the need to capitalise the "t" when importing.

Python 2
import Tkinter

Python 3

import tkinter

Progression

This is one of the biggest differences between 2 and 3. Python 3 continues to improve over 2 with each release, speed and resource management improve with each new version which allows for more efficient programs to be created in general.
 



Related Sections

Python - Learn the python from the basics up. This fast track example code course will get you creating powerful python programs in no time.
Tkinter - Learn the key features of the tkinter to allow you to create user interfaces for your python programs.


Related Pages

Pickle - The pickle library allows a program to save data in a binary file.
Python - List Comprehension - Create powerful list comprehension expressions.
Python - Generators - Learn about generator statements.
Python - Data types - The learn about the key datatypes in the python language.
Tkinter - Combo Box - This is a short example showing how to use combo boxes in a GUI.
Tkinter - Radio Buttons - A short example of how to create a GUI with radio buttons.
Tkinter - Canvas - Canvas allows for a range of shapes to be plotted, such as circles, squares, lines