Python Gotchas

Home       Contact Info      Updated: 2008-06-03

Gotchas that still apply to Python 3.x have been migrated to the Python Conquers the Universe blog.

Gotchas that have not been migrated do not apply to Python 3.x.

Integer division

For a long time, this was the feature of Python that (along with case-sensitivity) most frequently bit students who were learning Python as their first programming language.

The gotcha was this — in versions of Python prior to 2.2, if you divided an integer by an integer, you got an integer. If the division produced a remainder, the result was truncated downward. (Note that this was true truncation, not rounding toward zero.)

print 5/3     # produces 1
print (-5)/3  # produces -2
print 5/3.0   # produces 1.66666666667, because 3.0 is a float, not an integer

This behavior was called "classic" division, and is something that Python inherited from the C programming language.

What one wants, of course, is for Python division to behave more intuitively, like "true" division:

print 5/3      # produces  1.66666666667
print (-5)/3   # produces -1.66666666667

Starting in Python 2.2, the process of slowly changing the behavior of the division operator began.

from __future__ import division

In Python version 2.3, Python continued to use old-style division by default, but also issued a warning whenever division was applied to two integers. The warning was intended to help programmers to find code that's affected by the change and fix it. The fix — depending on what you wanted your program to do — meant either changing the / division operator to the // floor operator, or adding the from __future__ import division statement to your module.

In Python version 3, the new "true division" behavior for the division operator became standard, and you no longer need the from __future__ import division statement.

A comma at the end of a print statement prevents a newline from being written... but appears to write a trailing space.

It doesn't really write a trailing space -- it just causes an immediately subsequent print statement to write a leading space!

The Python Language Reference Manual says, about the print statement,

A "\n" character is written at the end, unless the print statement ends with a comma.

But it also says that if two print statements in succession write to stdout, and the first one ends with a comma (and so doesn't write a trailing newline), then the second one prepends a leading space to its output. (See section "6.6 The print statement" in the Python Reference Manual. Thanks to Marcus Rubenstein and Hans Meine for pointing this out to me.)

So

for i in range(10): print "*",
print

produces

* * * * * * * * * *

If you want to print a string without any trailing characters at all, your best bet is to use sys.stdout.write()

import sys
for i in range(10): sys.stdout.write("*")
sys.stdout.write("\n")

produces

**********


Creative Commons Licence This work is licensed under the Creative Commons Attribution 2.0 License You are free to copy, distribute, and display the work, and to make derivative works (including translations). If you do, you must give the original author credit. The author specifically permits (and encourages) teachers to post, reproduce, and distribute some or all of this material for use in their classes or by their students.