Fundamentals 8 min read

Key Differences Between Python 2 and Python 3

This article outlines the historical development of Python, explains why Python 2 reached end‑of‑life in 2020, and details the major syntactic and functional differences between Python 2 and Python 3, including encoding defaults, exception handling, range functions, print statements, and input behavior, concluding with guidance on choosing a version.

360 Quality & Efficiency
360 Quality & Efficiency
360 Quality & Efficiency
Key Differences Between Python 2 and Python 3

Python, created in the early 1990s, has grown into a mature language whose popularity surged with the rise of big data. Python 2, first released in 2001 and stabilized around 2006‑2008, dominated early development, while Python 3 debuted in 2008 but only became stable around 2014.

Officially, Python 2 reached end‑of‑life in 2020, and Python 3.x (often called Python 3000 or Py3k) introduced many non‑backward‑compatible changes, prompting developers to migrate.

Encoding differences : Python 2 defaults to ASCII, causing frequent Unicode errors when handling Chinese or other non‑ASCII characters. Python 3 defaults to UTF‑8, providing native Unicode strings and a separate bytes type, simplifying string handling.

Example of encoding handling in Python 3:

# source file is UTF‑8 by default
s = "中文变量"
print(s)

Exception syntax : Python 2 uses except Exception, e:, while Python 3 requires except Exception as e:. Moreover, only objects derived from BaseException can be raised in Python 3.

Exception handling example:

# Python 2
try:
    raise Exception, "error"
except Exception, e:
    print(e)
    return False

# Python 3
try:
    raise Exception("error")
except Exception as e:
    print(e)
    return False

Range functions : Python 2 provides xrange() for lazy iteration and a faster range() that returns a list. Python 3 removes xrange and makes range() behave like xrange, returning a lazy sequence.

Print statement : In Python 2, print is a statement (e.g., print "text") and also works as a function in later 2.x versions. Python 3 makes print a function, requiring parentheses.

Input handling : Python 2 distinguishes input() (evaluates the input) and raw_input() (returns a string). Python 3 removes raw_input; input() always returns a string.

Conclusion: The core programming concepts are consistent across versions, so once you master Python, switching between 2 and 3 is quick. Choose Python 2 only if legacy third‑party libraries or enterprise environments require it; otherwise, adopt Python 3 for future‑proof development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Pythonprogrammingencodingsyntaxpython2python3
360 Quality & Efficiency
Written by

360 Quality & Efficiency

360 Quality & Efficiency focuses on seamlessly integrating quality and efficiency in R&D, sharing 360’s internal best practices with industry peers to foster collaboration among Chinese enterprises and drive greater efficiency value.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.