Child pages
  • Python Tips and Quirks
Skip to end of metadata
Go to start of metadata

This is a place to record our thoughts on issues with Python that might trip up students, as we learn it ourselves.

  • loading files into the interpreter: the python.org tutorial doesn't say how to do this for quite a while, so I poked around a bit and discovered the "import" command.  But "import" seems to work only once -- if you change the input file and "import" again, it seems to keep the old definition.  So maybe that's not the right command to load a program file.  Or I'm doing something wrong... --mdj
    • import just imports the name(s), you can reload a module with the reload() function.  Ex: import foo, edit foo.py, then you can do reload(foo) in your interpreter --djw
  • very very confusing:  the Python concept that ranges are zero-indexed, but don't include the last-indexed number in the specified range  --mdj
    • Confusing, perhaps, but at least consistent for most uses.  range(5) gives [0, 1, 2, 3, 4]; x[:5] gives [x[0], x[1], x[2], x[3], x[4]], whereas range(0,5,2) gives [0,2,4] and x[0:5:2] gives [x[0], x[2], x[4]].  I think of range(low, high) as [low, high) in math notation. --wcm
  • in general, zero-indexing and the range() function will probably lead to a lot of student errors --mdj
  • True, False, 1, 0; ability to rebind constants True/False... --mdj
    • ... but at least there's no assignment in conditionals, so you can't easily accidentally rebind True to 0 or something. --wcm
  • distinguishing when two list objects refer to the same exact structure vs. copies/subcopies could be tricky (especially since we probably want to avoid talking about pointers/memory locations) -- the id() function will be a useful tool for illustrating this (and for students in debugging). --mdj
    • You probably need to talk about references from day 1, it'll be more confusing if you try to avoid it --djw
  • It may be informative to look at the changes in Python 3.0 to see what things even the Python people thought were confusing --djw
    • The numeric model is changing in 3.0; int/int division will be "true" division and return a floating-point approximation.  Is it better to start students on a 1/2 == 0.5 model or a 1/2 == 0 one? --wcm
    • Similarly, print is a statement in 2.6, and a function in 3.0. --wcm
    • range() in 3.0 is what xrange() is in 2.6; it doesn't return a list but a generator.  This makes demonstrating it in the interpreter a little more work: --wcm
      •  >>> xrange(5)
        xrange(5)
        >>> print xrange(5)
        xrange(5)
        >>> for i in xrange(5):
        ...     print i
        ...
        0
        1
        2
        3
        4
        
    • Strings (unicode characters) and bytes (8-bit numbers) are completely distinct in 3.0, whereas in 2.6 they're kind of a hopeless mishmash. --wcm
    • True, False, and None are reserved in 3.0.  No more of this: --wcm
      • >>> True = 0
        >>> if True:
        ...     print "foo"
        ...
        >>>
        
  • Integer division and modulus division are defined for floats, and actually work kind how of you might expect, but that's still weird. --djw
  • When talking about I/O, people usually say the input() function is for numbers, and raw_input() is for strings.  The first part isn't technically true, input() allows the user to type any valid Python expression, including identifiers, function names, lambdas, list/dictionary literals, etc.  Watch out for this.  --djw
  • No labels