Key Differences Between Python 2 and Python 3
1. Print Statement vs. Print Function
In Python 2, the print keyword is a statement and does not require parentheses. In Python 3, print is a built-in function and must be called with parentheses.
Python 2 Example:
print "Hello, World!"
print("Hello, World!") # Also works
Python 3 Example:
print("Hello, World!")
# print "Hello, World!" # This line would cause a SyntaxError
2. Input Function Behavior
Python 2 has input() and raw_input(). The input() function evaluates user input as Python code, while raw_input() returns a string.
Python 3 only has input(), which always returns user input as a string.
Python 2 Example:
user_data = raw_input("Enter text: ") # Returns a string
result = input("Enter an expression: ") # Evaluates the expression
Python 3 Example:
user_data = input("Enter text: ") # Always returns a string
3. Integer Types
Python 2 distinguishes between int (32-bit) and long (unlimited precision) integers. Python 3 consolidates both into a single int type with unlimited precision.
Python 2 Example:
print(type(42)) # <type 'int'>
print(type(2**100)) # <type 'long'>
Python 3 Example:
print(type(42)) # <class 'int'>
print(type(2**100)) # <class 'int'>
4. Divition Operator
In Python 2, the / operator performs floor division (integer division) when both operands are integers. True division requires at least one float operand.
In Python 3, / always performs true division (returning a float), while // is used for floor division.
Python 2 Example:
print(7 / 2) # Output: 3
print(7.0 / 2) # Output: 3.5
Python 3 Example:
print(7 / 2) # Output: 3.5
print(7 // 2) # Output: 3
5. Inequality Operators
Python 2 supports two inequality operators: != and <>. Python 3 only supports !=.
Python 2 Example:
print(3 != 4) # True
print(3 <> 4) # True
Python 3 Example:
print(3 != 4) # True
# print(3 <> 4) # This line would cause a SyntaxError
6. Range and Xrange Functions
Python 2's range() returns a list, while xrange() returns a memory-efficient iterator.
Python 3's range() returns an iterator-like object, and xrange() is removed.
Python 2 Example:
print(range(3)) # [0, 1, 2]
print(xrange(3)) # xrange(3)
Python 3 Example:
print(range(3)) # range(0, 3)
# print(xrange(3)) # NameError: name 'xrange' is not defined
7. Exception Handling Syntax
Python 2 uses a comma to capture an exception instance, while Python 3 uses the as keyword.
Python 2 Syntax:
try:
# ...
except ValueError, err:
print(err)
Python 3 Syntax:
try:
# ...
except ValueError as err:
print(err)
8. String Literals and Encoding
Python 2 source code uses ASCII as the default encoding for string literals unless explicitly declared otherwise. Python 3 source code uses UTF-8 by default.
Python 2 Example (Header Declaration Required):
# -*- coding: utf-8 -*-
9. Boolean Constants
In Python 3, True and False are reserved keywords and cannot be reassigned. In Python 2, they are built-in names that can be overridden (though this is not recommended).
10. String Formatting
Python 3 introduced f-strings, a concise way to embed expressions inside string literals. This feature is not available in Python 2.
Python 3 Example:
name = "Alice"
print(f"Hello, {name}!") # Output: Hello, Alice!
11. Package Structure
In Python 2, an __init__.py file is mandatory for a directory to be recognized as a package. In Python 3, it's optional (though often still used).
12. String Module Constants
Python 2's string.letters, string.lowercase, and string.uppercase constants, wich were locale-dependent, are replaced in Python 3 with the ASCII-specific string.ascii_letters, string.ascii_lowercase, and string.ascii_uppercase.
Python 3 Usage:
import string
print(string.ascii_letters) # abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
13. Class Definition
Python 2 has both "old-style" (classic) and "new-style" classes, distinguished by inheriting from object. Python 3 only has "new-style" classes, which inherit from object by default.
Python 2 Classic Class:
class LegacyClass:
pass # Old-style class
Python 2 New-style and Python 3 Class (All are new-style):
class ModernClass:
pass # Implicitly inherits from object in Python 3
class ModernClassExplicit(object):
pass # Explicit inheritance