Thanks to Dr. Alan McLeod for the content of this page.
Some significant changes were made in Python between versions 2 and 3. This list will be useful to you if you have programmed in Python before or need to translate Python 2 code examples. If you are starting out with Python 3, you don't need to read this! Note that this list is not complete; it only emphasizes those changes that are of concern to CISC 101 students. For more information consult the What's New in Python 3.0 and What's New In Python 3.1 sections in the Python documentation.
Possibly The Most Significant
print is now a built-in function (a "BIF"): print(). You can use "end=..." and "sep=..." keyword parameters - supply multiple output values using commas.
Comparison
- Use != to test for inequality, not <>.
- Comparison operators will return a TypeError exception if values being compared don't have a meaningful basis for comparison (like comparing numbers against strings).
Types
- There is no long type anymore, just the int type, which behaves like a long used to behave (no limits). The l or L postfix no longer works. (This change has slowed Python down a bit...)
- Sets are new - {..., ..., ..., ...}. Use the set() BIF to create a set from a list, for example.
Numerical
- Division - / - always returns a float, even if both numbers are ints. To get true integer division use - // - which is called "floor division".
- To indicate an octal literal use "0o" instead of just "0" as the prefix. Hex literals still use "0x".
- Binary literals can be created using the "0b" prefix.
- The bin() BIF returns a binary representation of the supplied base 10 argument as a string. The oct() and hex() BIFs operate similarly.
Strings
- The string formatting operator, %, is depreciated and will be removed in Python version 3.1. Use the str.format() method instead (see PEP3101: A New Approach To String Formatting in the Python documentation for more info on using .format()).
- u"...." no longer provides a Unicode string literal. str values are now Unicode to start with. So, Unicode characters can be used directly in string literals and even in variable names. Use b"...." to create a byte type literal value. byte values can only be ASCII characters. The "\u" escape sequence does not work in a byte type literal or a raw literal (r"....").
Other
- input() behaves like raw_input() and raw_input() no longer exists. input() strips the trailing newline.
- range() works like xrange() and xrange() no longer exists. range() returns a range object, which can be converted to a list using the list() BIF.
- The io module is standard for file I/O. open() actually invokes io.open().
- The Tkinter module is now named tkinter.
Third-Party Modules
Many third-party modules are not yet compatible with Python 3, but the ones used in CISC 101 are OK.