Tag Archives: Python 2 Vs Python 3

PYTHON 2 VS PYTHON 3

Python 2 Vs Python 3

Python is an extremely readable and adaptable programming language. The name was inspired by the British comedy group Monty Python; it was a major foundational goal of the Python development team to make the language fun and easy to use. It is easy to set up, and written in a relatively straightforward style with immediate feedback on errors, Python is a great choice for beginners.

Before going into the potential opportunities let’s see the key programmatic differences between Python 2 and Python 3, let’s start with the background of most recent major releases of Python.

PYTHON 2

Python 2 is a transparent and inclusive language development process than earlier versions of Python with the implementation of PEP (Python Enhancement Proposal). Python 2 has much more programmatic features including a cycle-detecting garbage collector to automate memory management, increased Unicode support to standardize characters, and list comprehensions to create a list based on existing lists. As Python 2 continued to develop, more features were added, including unifying Python types and classes into one hierarchy in Python version 2.2.

PYTHON 3

Python 3 is contemplated as the future of Python and is the version of the language that is currently in development. Python 3 was released in late 2008 to address and amend intrinsic design flaws of previous versions of the language. The focus of Python 3 development was to clear the codebase and remove redundancy. Major modifications to Python 3.0 includes, changing the print statement into a built-in function, improved the way integers are divided, and provides more Unicode support.

PYTHON 2.7

Following the 2008 release of Python 3.0, Python 2.7 was published on July 3, 2010 and planned as the last of the 2.x releases. The main intention behind Python 2.7 was to make it easier for Python 2.x users to port features to Python 3 by providing some measures of compatibility between the two. This compatibility support includes enhanced modules for version 2.7 like unittest to support test automation, argparsefor parsing command-line options, and more convenient classes in collections.

THE DIFFERENCES BETWEEN PYTHON 2 & PYTHON 3:

While Python 2.7 and Python 3 share many identical capabilities, there should not be any thought of interchangeable. Though a user can write code and useful programs in either version, it is worth in understanding that there will be some considerable differences in code syntax and handling.

PRINT

In Python 2, print is considered as a statement instead of a function, which is a typical area of confusion as many other actions in Python requires arguments inside the parentheses to execute. If the user wants the console to print out “The Shark is my favourite sea creature” in Python 2 the user can do it with the following print statement:

Print “The Shark is my favourite sea creature”

In Python 3, print() is explicitly treated as a function, so to print out the same string above, the user can easily do it with the simple syntax of a function:

Print(“The Shark is my favourite sea creature”)

This change made Python’s syntax more uniform and also made it easier to change between different print functions.

DIVISION WITH INTEGERS

In Python 2, any number that the user types without decimals is treated as the programming type called integer. While in the beginning this seems like an easy way to handle programming types, when the user tries to divide integers together then the user expects to get an answer with decimal places (called a float), as in:

5 / 2 = 2.5

However, in Python 2 integers were strongly typed and would not change to a float with decimal places even in cases that would make instinctive sense.

When the two numbers on either side of the division “/” symbol are integers, Python 2 will do floor division so that the quotient x is the number which is returned is the largest integer less than or equal to x. This means that when you write 5 / 2 to divide the two numbers, Python 2.7 returns the largest integer less than or equal to 2.5, in this case, 2:

a = 5 / 2

print a

OUTPUT
2
UNICODE SUPPORT

When programming languages handle the string type i.e., a sequence of characters which can do it in a different way so that computers can convert numbers to letters and other symbols.

Python 2 uses the ASCII alphabet by default, so when you type “Hello” Python 2 will handle the string as ASCII. Limited to a couple of hundred characters at best in various extended forms, ASCII is not a very flexible method for encoding characters, especially non-English characters.

Python 3 uses Unicode by default, which saves the programmers development time, and the programmer can easily type and display many more characters directly into the program. Because Unicode supports a linguistic character.

CONCLUSION

Python is a flexible and well-documented programming language to learn, whether you choose to work with Python 2 or Python 3, one will be able to work on exciting software projects.

Though there are several key differences, it is not difficult to move from Python 3 to Python 2 with a few twists, and you will often find that Python 2.7 can easily run Python 3 code.

It is important to keep in mind that most of the developers are focused on Python 3, the language will become more refined and in-line with the evolving needs of programmers, and less support will be given to Python 2.7.