Lessons‎ > ‎

Python

Python is a scripting language, so there is no need to compile your program and there is no "build" process.  An excellent resource for experienced developers to learn Python is the free online book: Dive Into Python.

We won't have time in this course to give you proper instruction in the Python programming language.  For those not familiar with Python, you should be able to pick up most of the concepts you need as we walk through the example programs presented in this course.  But there are a few Python tidbits we'll cover here, before diving into real examples:

If you are new to Python, you may enjoy using the built-in Python IDE, IDLE, to execute Python code in a command console:



There are a number of surprising attributes to the Python programming language:
  • Python is not type safe - like JavaScript, python variables can contain any data type.
  • One of the strangest concepts for new Python developers is that block structure is controlled by indentation level!

    x = 3

    if x < 10:
        print "small"
        print "number"
    else:
        print "large"
        print "number"


  • Python uses exception handling extensively - (exception are "raise"-ed, not "throw"n).
  • Python uses sequence (list and tuple) and map (dict) data types extensively.
  • Loops in Python are not the typical for (i = 0; i < max; i++) variety.  Instead you loop over the members of a sequence:

    for x in range(0,100):
        print x

  • Python programmers often use list comprehensions which are one-liner expressions for lists.

    l1 = []
    for x in range(0,10):
        l1.append(x)

    # is the same as....

    l2 = [x for x in range(0,10)]

  • If you want your Python programs to be read/used by other Python programmers, it's strongly recommended that you follow the Standard Python Coding Style.  In particular, use names like these:
    • function_names, method_names
    • module_names
    • ClassNames, ExceptionNames
    • packages
    • _local_names
    • __private_attributes
    • __magic_python_names__ (never invent your own)
    • CONSTANTS, CONSTANT_NAMES


Comments