PYTHON

Python 2 vs Python 3


Python 2 vs Python 3:

Python 2 and Python 3 are two major versions of the Python programming language. While they share many similarities, there are several key differences that distinguish them.

Key Differences Between Python 2 and Python:

1. Print Statement vs Print Function:

  • Python 2: print "Hello, World!" (print is a statement)
  • Python 3: print("Hello, World!") (print is a function)

2.Integer Division:

  • Python 2: Division of two integers truncates the decimal part (e.g., 5 / 2 yields 2).
  • Python 3: Division of two integers returns a float (e.g., 5 / 2 yields 2.5). For integer division, use // (e.g., 5 // 2 yields 2).

3.Input Function:

  • Python 2: input() evaluates the input as code. raw_input() reads input as a string.
  • Python 3: input() reads input as a string. There is no raw_input().

4.Range Function:

  • Python 2: range() returns a list. xrange() returns an iterator.
  • Python 3: range() returns an iterator. xrange() is removed.

5.Exception Handling Syntax:

  • Python 2: except Exception, e: (comma syntax)
  • Python 3: except Exception as e: (as syntax)

 


PYTHON