Python3 Vs Python2

If you’re new to Python perhaps you’ve heard there are two major versions out in the wild: Python 2 and Python 3. So which should you learn and why is there a debate about this?

It’s understandable that porting large existing code bases to a new version is a prospect which sends a shiver down many a developer’s spine. Python 3 was released to fix problems present in Python 2. The nature of these changes meant that Python 3 was incompatible with Python 2. It also meant that Python 3 did not carry forward the problems of Python 2. Some aspects of Python 3 have been back ported to Python 2.6 and 2.7 to make the migration to Python 3 easier.Python 2 was supposed to be retired in 2015 but slow adoption of Python 3 led to an extended deadline of 2020. Python 2.7 will receive updates till 2020 for security and bug fixes only.

I am new to Python. Should I learn Python 2 or Python 3?

Python 3 fixes many of the problems that are present in Python 2. Python 3 is also the future of the language since Python 2 will be retired in 2020. It's therefore recommended that beginners learn Python 3. Moreover, if you're going to write new code.

If you've to maintain legacy Python 2 code, it's still best to learn Python the right way from Python 3. Then you can study the differences between Python 2 and 3. The point is that the two versions are much more similar than different.

Users who are new to use Python may get confused about the different versions available in today’s market. Though Python 3 is the latest version, still there are few developers using Python 2. You can see advantages in both the versions and utilize those based on the requirement. Now let us see the details on Python2 Vs Python 3.

Why Python 3?

The answer I give most people is because Python 2 has been end-of-life’d. The people that work directly on Python have decided that there won’t ever be a Python 2.8. They also decided that Python 2.7, from now on, will only get security updates from the Python 3 development branch. No new features unless someone wants to spend the extra required time to back port them. Thankfully, several people are willing to do this. This has given us the amazing __future__ module full of Python 3 goodies, but that’s not the same as using them natively.

Another, somewhat embarrassing reason, is that I hadn’t had a lot of pure Python 3 experience. I wanted to be able to learn more of the newest version of my favorite language. Python 3 scared me when I first looked at it a year or so ago. It seemed like a big change for no real gain and I had visions of having to rewrite everything I had done to support this new interpreter. I was so wrong.

Python 3.0 – Released in the year 2008
Python 3.6 – Released in the year 2016
Python 3.7 under development
Python 3 is to manage the design defects in the earlier versions
Python 3 syntax is simple and easy to understand
Python 3 Print syntax is plainly treated as a function.
Example: print “hello”
In Python 3, dividing two (2) integers result in an integer value
3.0/2.0= 1.5
Libraries are being developed and they are made strict to use with Python 3
Here range does what xrange does in Python 2.7 and xrange function is not available
For-loop variable does not leak into the global namespace
Python 3 has only one input taking method which is input(). raw_input() is not available in Python 3. In case to use raw_input() like Python 2, make use of eval () method
Rules of ordering comparisons in Python 3 have been made simpler


Why you must learn Python 2?

Even though, Python 2 is known to be the old version of open source where you will still need to learn Python 2. For becoming the DevOps engineer and you should also work with the different configurations management tools such as Ansible and puppet. Here, you should also work with both available versions. If the company’s code is written in Python 2, you need to learn to work for it. If the entire development team is perfectly working on the project which depends on particular third-party libraries and the third party software that you are not capable of port and Python 3, so Python 2 is an only available option for the user.

There's ASCII str type and unicode type, but no separate type to handle bytes of data
Two types of integers: C-based integers (int) and Python long integer (long)
Return type of division is int if operands are integers: 5 / 4 gives 1; 4 / 2 gives 2
round(16.5) returns a float of value 16.0
Unorderable types can be compared
print is a statement: print "Hello World!"
range() returns a list of numbers while xrange() returns an object for lazy evaluation
Functions/methods map(), filter(), zip(), dict.items(), dict.keys(), dict.values() return lists
raw_input() returns input as str and input() evaluates the input as a Python expression
Raising exceptions: raise IOError("file error") or raise IOError, "file error"
Handling exceptions: except NameError, err: or except (TypeError, NameError), err:
On generators, a method or function call: g.next() or next(g)
Loop variables in a comprehension leak to global namespace

Which Python Version you should Use?

When it is about the comparison of Python 2 version and the 3 today, Also, Python 3 is an outright winner. The reason is simple that Python 2 will be available after the year 2020. Even the adoption of Mass Python 3 is also a clear direction of the future. After considering declining any kind of support for Python 2 programming language along with the additional benefits from the upgrades to Python 3, it is usually advisable for the developer to choose Python version 3. On the other hand, if the job demands the capabilities of Python 2, this would be just a compelling reason for using this version.

Main differences

While Python 2 and 3 are quite similar, there are notable differences in code handling and syntax. You should not assume that code written in Python 2 will work smoothly in Python 3 or vice versa.

Print

In Python 2, print is a statement not a function. You use it as follows:

print "I'm a print statement in Python 2"

In Python 3, print() is explicitly treated as a function, so we use standard function syntax:

print("I'm a print statement in Python 3")

Note that Python 3’s print() syntax is backwards-compatible with Python 2.7, so a Python 3 print() function will run in either version.


No comments:

Post a Comment

INSTAGRAM FEED

@vipulpedia