Python Tutorial

Python Variable

Python Operators

Python Sequence

Python String

Python Flow Control

Python Functions

Python Class and Object

Python Class Members (properties and methods)

Python Exception Handling

Python Modules

Python File Operations (I/O)

The Difference Between Python 3 And Python 2

Python 2 and Python 3 are two major versions of the Python programming language. Python 2 was introduced in 2000 and was the most widely used version of Python for many years, while Python 3 was introduced in 2008 as a major update to the language.

Here are some of the key differences between Python 2 and Python 3:

  1. Print Statement: In Python 2, the print statement is used as follows: print "Hello, World!". Whereas, in Python 3, the print function is used as follows: print("Hello, World!").

  2. Division: In Python 2, the / operator performs integer division if both operands are integers, whereas in Python 3, it always performs floating-point division. In Python 2, to perform floating-point division, one of the operands must be a floating-point number or use the float() function. In Python 3, to perform integer division, use the // operator.

  3. Unicode: Python 2 treats strings and bytes as separate types, and strings are represented as ASCII by default. In Python 3, strings are represented as Unicode by default, and bytes and strings are distinct types.

  4. Range and xrange: In Python 2, range() generates a list of integers, while xrange() generates an iterator. In Python 3, the xrange() function no longer exists, and the range() function behaves like xrange() did in Python 2.

  5. Exceptions: In Python 2, the raise statement can be used with a tuple of the form (type, value, traceback) to raise an exception with a custom message and traceback. In Python 3, the raise statement can be used with an exception instance or an exception class, and the traceback argument is no longer supported.

  6. Input: In Python 2, the input() function evaluates the user input as a Python expression, whereas the raw_input() function reads the input as a string. In Python 3, the input() function reads the input as a string, and the raw_input() function no longer exists.

  7. Function arguments: In Python 2, function arguments are passed by reference by default, which can lead to unexpected behavior when mutable objects are modified inside the function. In Python 3, function arguments are passed by value by default, but mutable objects can still be modified inside the function if necessary.

Overall, Python 3 is considered to be a more modern and consistent version of the language, with better support for Unicode and more consistent syntax. However, many existing Python 2 codebases and libraries still exist and may require migration to Python 3 in order to take advantage of its features.