What are the differences in between python 2.x and python 3.x versions?

PythonServer Side ProgrammingProgramming

Python 2.x is legacy, Python 3.x is the present and future of the language.

A non-exhaustive list of features which are only available in 3.x releases and are not available in Python 2.x −

  • strings are Unicode by default

  • clean Unicode/bytes separation

  • exception chaining

  • function annotations

  • syntax for keyword-only arguments

  • extended tuple unpacking

  • non-local variable declarations

Some key differences

  • print −  In Python 2, “print” is treated as a statement rather than a function. There is no need to wrap the text you want to print in parentheses, although you can if you want. In Python 3, print is a function, which means you have to pass the items you need to print to the function in parentheses.

  • Variables in List Comprehension − In Python2.x, giving the variable that is iterated over in a list comprehension the same name as a global variable could lead to the value of the global variable being changed.

  • Integer Division − Python 2 treats numbers that you type without any digits after the decimal point as integers, which can lead to some unexpected results during division. For example,

The result of 3/2 in Python 3 and Python 2 are 1.5 and 1 respectively. To do integer division in Python 3, you need to use the // operator. And for fload division in Python 2, you need to add a decimal point and zero after any number in the expression.

There are many more features that have changed from Python 2 and you can use this website as a reference to find out what's new in Python 3, specific to every detail: https://docs.python.org/3/whatsnew/

raja
Published on 05-Jan-2018 17:05:51
Advertisements