Sunday, October 26, 2008

Python Tutorial

I use the Python programming language for the majority of my coding tasks these days. Note the title of this blog.

In Dr. Tim Finin's Principles of Programming Languages course (CMSC 331), I will be giving a guest lecture tutorial on Python. Most of these students have a semester or two of Java and C experience from introductory computer science classes, so I tried to keep it relatively simple.

Since I have this tutorial written up I will hopefully keep it updated as I come across interesting things. Just a few days ago I learned about the back-ticks around something to convert to a string. E.g., `12345`[2] returns '3'. I saw this in one of my student's code for one of their coding assignments; I should read more code other than my own.

Link: "Brief Introduction to Python"

3 comments:

  1. Hi,
    I thought I'd go through your tutorial on Python and make some notes if thats OK.

    "No ++ / --":
    I think this was the same kind of decision as Python not allowing assignments in if conditionals. There is a a high instance of problems caused by confusion over the misuse of increment/decrement operators in languages like C. They can make it harder to read a program.

    "Slow in comparison to compiled languages" - (Here comes the dynamic language supporters usual reply): Yes, but often getting something right is much more important and often never requires the speep that Python gives you. People have prototyped in Python and shipped the prototype as it fits all quality metrics in a far shorter development time. I recently had a case of that myself here.

    "Whitespace for indentation": Arguably a positive feature. It should read "whitespace for block delineation" or something. Run a pretty printer on say a C source file and it will try and indent it so that the visible level of indentation mirrors the logical structure of the code. Code is hard to read if the indentation does not follow the logic. Python recognises this and goes further in saying that the common use of block start and end delimeters such as {/} or begin/end are superfluous and only help in writing misleading code.

    You seem to have missed the importance of docstrings in Python.

    range "(good for indexing)": True, but in idiomatic Python you are much more likely to iterate over the members of a collection object rather than create an index using range:
    for x in my_list:
    rather than:
    for i in range(len(my_list)):
    ....x = my_list[i]

    "Functions work in python much like they do any other language": those with only Java and C experience might miss out on the extended argument passing of Python functions if not made aware.

    "Classes are more basic than what you will be use to from java": In what way? There maybe less to learn to become equally proficient but I see this as a good thing. Note that Python is dynamic, meaning that, if you wanted to, you can modify classes and instances at run-time. Its not necessarily a X language does classes better than Y. Python does things differently and if you try and constrain Python to how you use OO in Java, you won't get the best from it.

    "from os import *": It's a bad Python habit. You want to warn students off this, as maintenance can suffer.

    "We can raise our own exceptions": It could be but shouldn't be a string. best to use an exception type.

    "Numbers": There are decimals too, which are good for financial calculations.

    "Tuples": If each position in a sequence has explicit meaning then you should use a tuple, e.g. a point on a surface might be the tuple (x,y). coordinates of a car would more likely be a list of points.

    "Strings": Misses triple quoted strings.

    Filter and map, although present, have declined in use due to the later addition of the powerful list comprehension and generator comprehension features.

    One-liners in Python: Difficult to do. Usually people write multi-line scripts in an editor. In a shell such as bash which has multi-line string delimeters you might be able to use a multi-line single quote for bash and embed a multi-line python program using Pythons -c argument and restricting your Python to double quotes if needed.

    Have fun with Python.

    - Paddy.

    ReplyDelete
  2. Ouch!
    My notes are longer than your blog entry :-)

    ReplyDelete
  3. Backticks are a syntactic relic in Python, considered extremely poor style. "`foo`" is a synonym for "repr(foo)", and you should always prefer the latter.

    ReplyDelete