Fundamentals 10 min read

What Changed in Python 3.0? Key Differences and Migration Tips

This article explains the major changes introduced in Python 3.0—including the new print() function, unified Unicode strings, altered division behavior, updated exception syntax, removal of xrange, revised literal formats, module renames, and data‑type updates—while offering guidance for migrating existing Python 2 code.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
What Changed in Python 3.0? Key Differences and Migration Tips

Python 3.0 Overview

Python 3.0, also known as Python 3000 or Py3k, is a major upgrade that is not backward‑compatible.

It was designed without backward compatibility, so many scripts written for earlier versions cannot run unchanged.

Python 2.6 serves as a transitional version that supports most Python 2.x syntax while allowing some Python 3.0 features.

New code should target Python 3.0 syntax.

Only a few third‑party libraries (e.g., Twisted, py2exe, PIL) lack Python 3.0 support; most are being ported.

Key Changes

print function

The print statement is replaced by the print() function. Python 2.6/2.7 support it partially.

print "fish"
print("fish")  # note the space after print in the statement form
print("fish")  # print() cannot take other arguments

Python 2.6 can also use the future import:

from __future__ import print_function
print("fish", "panda", sep=', ')

Unicode

Python 2 has separate str (ASCII) and unicode types. Python 3 unifies text as Unicode (UTF‑8) strings and introduces a distinct bytes type.

>> 中国 = 'china'
>>> print(中国)  # works in Python 3
>>> str = "我爱北京天安门"
>>> str = u"我爱北京天安门"  # Python 2 representation
>>> str = "我爱北京天安门"  # Python 3 representation

Division operators

Python 2 uses / for integer division that truncates the decimal part, while // performs floor division. In Python 3, / always returns a float; // still performs floor division.

# Python 2.x
1/2   # 0
1/2.0 # 0.5

# Python 3.x
1/2   # 0.5

Use math.trunc() to discard the fractional part explicitly.

Exception syntax

The syntax changes from except exc, var to except exc as var. Multiple exceptions can be caught with except (Exc1, Exc2) as var. Python 2.6 already supports the new form.

xrange removal

xrange()

from Python 2 is removed; range() in Python 3 behaves like the old xrange() (lazy evaluation). Attempting to call xrange() in Python 3 raises a NameError.

Octal and binary literals

Octal literals must be written as 0o777 (the old 0777 is invalid). Binary literals use the 0b prefix. The bin() function converts an integer to its binary string.

Other module renames (PEP 8)

_winreg

winreg
ConfigParser

configparser
copy_reg

copyreg
Queue

queue
SocketServer

socketserver
repr

reprlib Modules such as StringIO moved to io, and several legacy modules were merged or removed. The exec statement became the exec() function.

Data types

Python 3 drops the long type; int now has unlimited precision.

Introduces bytes type; conversion between str and bytes uses .encode() and .decode(). dict.keys(), .items(), .values() return view objects; the old iterkeys() etc. are removed. dict.has_key() is removed; use the in operator.

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.

UnicodeExceptionslanguage migrationPython 3divisionsyntax changesxrange
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

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.