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

Author Topic: Key differences between Python 2 and 3: How to navigate change  (Read 1297 times)

Offline Abdus Sattar

  • Sr. Member
  • ****
  • Posts: 483
  • Only the brave teach.
    • View Profile
    • https://sites.google.com/diu.edu.bd/abdussattar/
Key differences between Python 2 and 3: How to navigate change
 August 22, 2018  Vinodh Kumar

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:

Expressions
Print options
Unequal operations
Range
Automated migration
Performance issues
Some major housekeeping changes
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:

1   X = raw_input ("enter some values)
But in Python 3, you’d have to type this:

1   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.

1   x= eval(input("enter some values")) = 12
Detailed expression examples:

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

1
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:

1
2
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:

1
print "hello world"
Output:

python

Python 3

Input:

1
2
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
2
1 <> 1.0
print "False"
Output:

PYTHON

Python 3

Input:

1
2
3
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:

1
print range(0,10,1)
Output:



 

Python 3

Input:

1
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:

1
2
3
4
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:

1
2
3
4
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.

Source: http://snip.ly/mg8vuc/#https://jaxenter.com/machine-learning-travel-industry-147766.html
Abdus Sattar
Assistant Professor
Department of CSE
Daffodil International University(DIU)
Mobile: 01818392800
Email: abdus.cse@diu.edu.bd
Personal Site: https://sites.google.com/diu.edu.bd/abdussattar/
Google Scholar: https://scholar.google.com/citations?user=DL9nSW4AAAAJ&hl=en