Python 2 vs Python 3: Key Differences and Upgrades

Rumman Ansari   Software Engineer   2024-07-23 09:25:39   5720  Share
Subject Syllabus DetailsSubject Details
☰ TContent
☰Fullscreen

Table of Content:

Difference between Python2 & Python3

Print:

  • Python 2 treats “print” as statement rather a function.
  • Python 3 explicitly treats “print” as a function.

Integer Division:

  • Python 2 treats numbers without any digits. (Output of expression 3 / 2 is 1, not 1.5). To get the result 1.5, you would have to write 3.0 / 2.0.
  • Python 3 evaluates 3 / 2 as 1.5 by default, which is more intuitive for new programmers.

List Comprehension Loop Variables:

Common name for the variables that is iterated over in a list comprehension as a global variable get interchanged. This is fixed in Python 3.

Unicode Strings:

By default Python 3 stores strings as Unicode unlike Python 2.

Raising Exceptions:

Python 3 requires different syntax for raising exceptions.

  • Python 2:raise IOError, “some error message”
  • Python3: raise IOError(“some error message”)