Net-informations.com

Difference between Python 2 and Python 3

Programming languages constantly evolve as developers extend the functionality of the language and iron out quirks that cause problems for developers. If you are new to Python language , you might be confused about the different versions that are available. Python 1.x refers to the first set of releases of the Python. This was long ago and nobody uses or cares about these anymore. Python 2.x refers the second line of releases of Python. Python Python 2.7.17 (19 October 2019) is the latest release in this line of releases. Python 3.x refers to the third major line of releases of Python. Python Python 3.8.2 (24 February 2020) is the latest release in this line of releases.


Important differences between Python 2.x and Python 3.x

Backward Compatibility

Python has a rule that all 2.x versions will be backward compatible . The same rule applies to Python 3.x versions. However, Python does not guarantee backward compatibility between versions. Python 3 introduced several changes to the actual structure and syntax of the Python language. The whole Python community was pretty much sceptical when they received Python 3.x. Python 3.0 is fundamentally different to previous Python releases because it is the first Python release that is not compatible with older versions . Most of the things written in Python 2.x were not compatible with Python 3.x, as it did not support backward compatibility. Many applications and frameworks needed to rewritten completely due to this, hence, it was very difficult to port to Python 3.x from Python 2.x . Programmers who first learned to program in Python 2.x sometimes find the new changes difficult to adjust to, but newcomers often find that the new version of the language makes more sense.

Python 2.x Vs. Python 3.x

  1. specifically Python 2.7 vs 3.5

At present, there is no confusion about which Python version to use (undoubtedly it's Python 3.x). There are still many developers out there who continue using python 2.x because some older libraries and packages only work in Python 2. Since Python 3.x is the future, many of today's developers are creating libraries strictly for use with Python 3.x . Here are some key differences between Python 2.x and Python 3.x that can make the new version of the language less confusing for new developers to learn:

  1. Using the __future__ module
  2. The print function
  3. Integer Division
  4. List Comprehension Loop Variables
  5. Unicode
  6. xrange function
  7. The next() function and .next() method
  8. Raising Exceptions
  9. Handling exceptions
  10. For-loop variables and the global namespace leak
  11. Parsing user inputs via input()
  12. Returning iterable objects instead of lists
  13. Comparing unorderable types

The key differences between Python 2.7.x and Python 3.x

Using the __future__ module

The __future__ statement forces Python interpreters to use newer features of the language. It is a directive to the compiler that a particular module should be compiled using syntax or semantics that will be available in a specified future release of Python . The idea of __future__ module is to help migrate to Python 3.x. If you are planning to have Python 3.x support in you 2.x code, you can use _future_ imports in our code.

If you actually want to import the __future__ module, just do:

For example:

will allow you to use print as a function:

__future__ statements need to be near the top of the file because they change fundamental things about the language, and so the compiler needs to know about them from the beginning.

The print function

In Python 2, "print" is treated as a statement rather than a function . However, parentheses work in Python 2.x if a space is added after print keyword because the interpreter evaluates it as an expression. But in Python 3.x it is compulsory to use parenthesis otherwise it will raise error .

  1. Python 2.x supports both syntaxes , print and print()
  2. Python 3.x supports only print()

Integer Division

In python 2, if you perform division on two integers then the output will be an integer too. But in python 3, output will be accurate, so the result can be in float too.

In Python 2.x, division defaults to integer division if both operands are integer:

In Python 3.x:

If you want integer division in Python 3.x, you can use the floor division operator (//), which has been around since long before Python 3:

List Comprehensions

Python 2.x

For List Comprehensions, lesser parenthesis than Python 3.x.

Python 3.x

You need to use extra pair of parenthesis in Python 3.x.

Unicode


python 2 vs python 3 unicode vs ascii

In Python 2.x strings are stored internally as 8-bit ASCII — a method of encoding English characters with an assigned number while in Python 3.x implicit str type is Unicode . This is important because Unicode is more versatile than ASCII. While functional, ASCII remains 7-bit, meaning it does not encompass a broad range of symbols. Unicode ( Unicode Transformation Format – 8-bit ) strings can store foreign language letters, Roman letters and numerals, symbols, emoji, etc., offering you more choices. You can still label your Unicode strings prefix with a "u" if you want to make sure your Python 3 code is compatible with Python 2 .










net-informations.com (C) 2020    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About