Python Programming Language

Description

Python is an interpreted programming language created by Guido van Rossum in 1990. The Python interpreter is written primarily in C and is distributed as open source by the Python Foundation, a non-profit organization modelled after other non-profit distributors of open source software such as the Apache Foundation and the Mozilla Foundation. Python code can be run either directly within the Python interpreter, as an executable, or within a web browser.

One of the main design goals of Python is readability. For this reason, Python syntax differs from more common C and Java syntax in several respects.

  1. Curly braces aren't used to denote block statements. Instead indentation is used.
  2. Line breaks are used to denote the end of a statement, as opposed to a semi-colon.
  3. Python has only two forms of looping. A for loop (Syntax: for item in iterator:) and a while loop (Syntax: while expression: ).
Python uses the novel approach of making whitespace significant, and defines constructs in a way that reflect good programming practices. Since programmers should be doing things like indenting code within a block statement anyway, this type of syntax should make sense to experienced programmers who have never used Python before. The lack of curly braces and semicolons also makes code easier to read.

Due to the nature of the language, experienced Python users generally reject the use of syntactic sugar, or multiple ways of programming the same thing. Instead, these users promote the idea of creating code which is efficient while still remaining readable to others. This ideology differs with users of Perl, whose opponents derisively refer to as a write-only language.

Fun Facts about Python

Installing the compiler

  1. Download the Python Interpreter for Windows - Browse to Python.org's download page and click the latest stable release listed for the Windows operating system. Once downloaded, a self-installer will open. Just click through the dialogs, selecting the defaults.
  2. Selecting an IDE - The Python interpreter includes a command-line interpreter which can be used in a similar fashion to the Scheme interpreter we saw in class. However to write stand-alone Python scripts it is convenient to use an IDE that integrates text-editing and running the Python script. We used PyScripter in our presentation, but many more IDEs are listed at this Python wiki link.
  3. Setting up the IDE - Follow the link to PyScripter above and select the 'Downloads' page. Download and run the latest version of PyScripter and select the defaults in the self-installer that runs after the download is complete. Upon running PyScripter, you should see the Python Interpreter running in the bottom of the screen, and a blank text editor page open at the top. PyScripter allows you pass commands to the command-line interpreter directly, or create a stand-alone Python script and run that.

Writing and running programs

Sample Python Code

A sample program that reads in Fahrenheit temperatures and converts them to their Celsius equivalents:

import string, sys

# If no arguments were given, print a helpful message
if len(sys.argv)==1:
    print 'Usage: celsius temp1 temp2 ...'
    sys.exit(0)

# Loop over the arguments
for i in sys.argv[1:]:
    try: 
        fahrenheit=float(string.atoi(i))
    except string.atoi_error:
	print repr(i), "not a numeric value"
    else:
	celsius=(fahrenheit-32)*5.0/9.0
	print '%i\260F = %i\260C' % (int(fahrenheit), int(celsius+.5))

The Quicksort algorithm in Python

def qsort(L):
  if L == []: return []
  return qsort([x for x in L[1:] if x< L[0] ]) + L[0:1] + \
         qsort([x for x in L[1:] if x>=L[0] ])

Online References