What is the difference between Python 2 and Python 3

Python 2 and Python 3 are two major versions of the Python programming language, and they have several differences that developers need to be aware of. This content will highlight some of the key differences and provide examples for clarity.

Key Differences

  • Print Statement vs Print Function: In Python 2, print is a statement, whereas in Python 3, it is a function.
  • Integer Division: In Python 2, dividing two integers results in integer division. In Python 3, it results in float division.
  • Unicode: In Python 3, all strings are Unicode by default, while in Python 2, they are ASCII by default.
  • Input Function: In Python 2, raw_input() is used to get input as a string, while in Python 3, input() serves this purpose.

Example Code


# Python 2 example
print "Hello, World!"  # This is a print statement

# Python 3 example
print("Hello, World!")  # This is a print function
    

Python 2 Python 3 differences programming print statement print function integer division Unicode input function