days
-6
-8
hours
-2
-1
minutes
-3
-5
seconds
0
-2
search
Weighing the scales

Key differences between Python 2 and 3: How to navigate change

Vinodh Kumar

© Shutterstock / Breck P. Kent

 

As every programming language evolves, there are big changes between each major release. In this article, Vinodh Kumar explains some of the big differences between Python 2 and Python 3 with examples to help illustrate how the language has changed.

This tutorial will cover the following topics:

  1. Expressions
  2. Print options
  3. Unequal operations
  4. Range
  5. Automated migration
  6. Performance issues
  7. Some major housekeeping changes
  8. Having problems?

1. Expressions

Expressions represent something, like a number, a string, or an instance of a class. Any value is an expression! Anything that does something is a statement. Any assignment to a variable or function call is a statement. Any value contained in that statement in an expression.

This is what you’d type to get an evaluated expression in Python 2:

X = raw_input ("enter some values)

But in Python 3, you’d have to type this:

X = input ("enter some values") 

So, whatever we enter then for the value is assigned to variable x in both 2 and 3. When I enter 2*6 in Python 2, the result will be 12, which is the evaluated value.

However, when this same program is run in Python 3, the result is string values. In this case, it would look like 2*6 in string format.

Then, how can we get the evaluated expression? Now, we have to use an expression or function called eval. when you write eval before the input, it will turn the expression into an evaluated value.

x= eval(input("enter some values")) = 12 

Detailed expression examples:

Here’s what it would look like in Python 2:

name = input("What is your name? ")
print ("Hello, %s." %name) 

And the output:

python

Here’s how it would look like in Python 3:

name = input("What is your name? ")
print ("Hello, %s." %name) 

And the output:

python

As you can clearly see, there is very little difference between the two.

SEE MORE: Will Python dethrone Java this year? Programming language rankings say yes

2. Print options

In Python 2, print is a statement that does not need a parenthesis. In Python 3, print is a function and the values need to be written in parenthesis.

Python 2

Input:

print "hello world" 

Output:

python

Python 3

Input:

1 != 1.0
print (False) 

Output:

python

3. Unequal operations

Let’s move on to the third difference. When we use an unequal operator in Python 2, we need to use the greater than > or less than < signs. However, in Python 3, there is a general operator. The exclamation mark ! and equal sign = are used to show if things do not equal the same amount.

Python 2 – <> operator is used for not equal
Python 3 –  ! operator is used for not equal

Python 2

Input:

1 <> 1.0
print "False" 

Output:

PYTHON

Python 3

Input:

1 != 1.0
print (False)1 != 1.0
print (False) 

Output:

SEE ALSO: Glances: Keep a good eye on your code with this Python monitoring tool

4. Range

Now, let’s turn to ranges. What are the ranges?

A range is used to generate a list of numbers, which is generally used to iterate over with for loops.

python

Here you can see X equal to Range 10. When we check the variable X, it returned our list type. This means that in Python 2, range is the type of list. When I write X, after that, we get a list of object. which is 0 1 2 3 4 5 6 7 8 9.

python

Now let’s move to the Python 3, when we write x equal to range 5. This value of range 5 is assigned to the variable X; when we check the type for variable X, then it returns a range object itself. This means that in Python 3 range is a range object itself.

Python 2

Input:

print range(0,10,1) 

Output:

 

Python 3

Input:

print(list(range(10))) 

Output:

 

5. Automated migration

So, how do we automate the migration script to move code from Python 2 into 3?

Here, we can test with a simple program like Add 2 Numbers in python.

Python 2

Input:

n1 = 1
n2 = 2
add = float(n1) + float(n2)
print 'sum of {0} and {1} is {2}'.format(n1, n2, add) 

Output:

Now using the 2 to 3 migration we can convert the above code.

Input:

n1 = 1
n2 = 2
add = float(n1) + float(n2)
print('sum of {0} and {1} is {2}'.format(n1, n2, add)) 

Output:

So here we see it can be converted to Python 3 code by 2 to 3 on the command line.

Python provides its own tool called 2to3.py. Which runs a bunch of scripts to translate your python 2 code into 3. While it’s not perfect, but it does an amazing job overall. After converting any code, you should go in and manually fix up any problems.

SEE ALSO: Come, Nagini. We need to verify that Python code

6. Performance

Most of the performance issues have been fixed in this upgrade! When comparing benchmarks between the two versions, the differences are almost negligible.

7. Some major housekeeping changes

Python 2

  • print functional brackets optional.
  • Prefix string with u to make unicode string.
  • Division of integers always return integer – 5/2=2.
  • Raw_input () reads string.
  • input() evaluates data read.
  • generator .next().

Python 3

  • print functional brackets compulsory.
  • String unicode by default.
  • Division of integers may result in float – 5/2=2.5.
  • Raw_input() not available.
  • Input always reads string.
  • Next (generator).
  • Py2 to py3 utility.
  • Dictionary .keys() and .values() returns a view not a list.
  • Can no longer use comparison operators on non natural comparisons.
    Eg. None < None  will raise a TypeError instead of returning false.
  • Percent (%) string formatting operator is  deprecated use the .format() Function or concatenation.

SEE ALSO: Top 5 IDEs and code editors for Python

8. Having problems?

You may encounter an error here and there if you have been working in python 2.x for some time. That’s fine! Just google the problem, it’s almost certain that someone else has also had that problem too when migrating.

Author

Vinodh Kumar

Vinodh Kumar is a postgraduate with a Business Administration background. He is currently working as a content contributor for Mindmajix and loves to write tech related niches. Contact Vinod at vinodkumar.dm1@gmail.com. Find Vinod on LinkedIn and Twitter.