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.
- Curly braces aren't used to denote block statements. Instead indentation is used.
- Line breaks are used to denote the end of a statement, as opposed to a semi-colon.
- 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
- Python uses indentation to denote block statements rather than the common convention of curly braces ({ }).
Similarly, ends of statements are denoted by a new line rather than a semi-colon.
- Web pages written in Python have the .py file extension rather than the .html extension.
- Jython is an implementation of Python written in Java. In Jython, any Java class can be imported and used. Additional Python interpreters written in other languages include: IronPython and Boo (.NET/Mono), and PyPy (the Python language written in Python rather than C).
- Strings in Python are immutable meaning any operation performed on a string does not change the original string, but returns an entirely new string. Fans of Python say that this is an advantage because it eliminates the
chance that strings used in one part of the program will be modified unexpectedly by another part of the program (our book describes this situation as a 'side-effect').
Installing the compiler
- 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.
- 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.
- 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