[an error occurred while processing this directive]
Python, a reference to Monty Python's Flying Circus, is an open-source scripting language that is extremely flexible and powerful. Python is a server-side language, meaning that python programs run on the web server in response to a user request to produce an output web page.
This document should provide enough information to get started writing Python programs, but you may need to consult the online Python documentation (available at http://www.python.org/doc/) to find out about primitive procedures in Python that may be useful for your projects.
Python is a universal programming language — this means it can express every computation just like Java and Scheme. The syntax of Python is somewhat similar to C++ or Java.
Python has become very popular because of its simplicity, elegance, extensive libraries, and integration into popular web servers. In addition, because Python is interpreted and dynamically types, it is much quicker to write simple programs in Python than in a compiled, statically-typed language like Java.
Python is already installed on the ITC and CS lab machines. It is to your advantage to do the assignments in Small Hall and ask questions of your fellow students and the ACs. You may also find it helpful, though, to have Python installed on your own machine. To install Python on your own machine:
Nonterminal ::= ReplacementThis means whenever Nonterminal appears, it can be replaced by Replacement. There can be several rules with the same nonterminal on the left hand side. For example, the rules:
State ::= Virginiawould mean that the expression State State could be any of four possible strings: Virginia Virginia, Virginia Maryland, Maryland Virginia, or Maryland Maryland.
State ::= Maryland
This form of description is powerful, since the rules can be recursively defined. For example,
StateList ::=means whenever StateList appears we can either replace it with empty, or replace it with a State followed by a StateList. This describes a list of zero or more State elements.
StateList ::= State StateList
PythonCode ::= StatementsIn Python, unlike C++ or Java, the whitespace (such as new lines) has meaning. Statements cannot be separated into multiple lines, and only one statement may appear on a single line. Indentation within a line also matters. Instead of using parentheses to provide code structure, Python uses the indentation to group statements into blocks.
Statements ::= Statement <newline> Statements
Instructions ::=
Python has many different kinds of statements. The three you will use most are assignment, applications and function definitions:
Statement ::= AssignmentStatement
Statement ::= ApplicationStatement
Statement ::= FunctionDefinition
Also note that the coment (statements that are there for notes and clarifications but not to be executed) are denoted by # (everything after the # until the end of the line is a comment).
AssignmentStatement ::= Variable = Expression
To evaluate an AssignmentStatement, Python evaluates the Expression and places the value in the place named Variable.
four = 4
two = four / 2
bigger = (four * four) < 17
print "four: %d two: %d bigger: %d" % (four, two, bigger)
four: 4 two: 2 bigger: 1
Python considers the empty string, 0, and the empty list to be false, and (almost) everything else to be true.
The print procedure takes a string as input followed by a % and a list of values (in parentheses). The values are matched to the % codes in the string. Different % codes are used for printing different types of values. Here, %d is used to print an integer. Essentially, it provides an easy way to convert to strings other types of inputs. (Some other string formatting codes are: %s - string or any object, %c - character, %i - integer, %f - floating point decimal). The types can be combined together. For example:
"%d --
%s -- %f" % (2, "hello", 3.14)
will output: 2 -- hello -- 3.14
In Python, we apply a function to operands by following the name of the function with a comma-separated list of arguments surrounded by parentheses (just as in Java):
ApplicationStatement ::= Name ( Arguments )
Arguments ::=
Arguments ::= MoreArguments
MoreArguments ::= Argument ,MoreArguments
MoreArguments ::= Argument
Argument ::= Expression
Python functions may return one or more results. When multiple results are returned, the call site can bind them to different variables listed in parentheses.
Python only has a few primitive functions, but a very extensive standard library.
FunctionDefinition ::= def Name ( Parameters ): <newline> Statements
Parameters ::=
Parameters ::= MoreParameters
MoreParameters Parameter ,MoreParameters
MoreParameters ::= Parameter
Parameter ::= Variable
For example,
def square (x):
return x * x
def quadratic (a, b, c, x):
return a * square (x) + b * x + c
print quadratic (2,3,7,4)
51
Note the identation in that code segment. It is extremely important to preserve the identation as it is what is going to indicate where the function body ends. The return statement is the similar to the return in Java, except multiple values can be returned:
ReturnStatement ::= return ExpressionList
ExpressionList ::= Expression
ExpressionList :: Expression, ExpressionList
It is used to return a value from a procedure. When execution encounters a return statement, the listed expressions are evaluated and their values are returned to the caller.
Python also provides several statements similar to control structures in Java. Three useful ones are if and while and for which are described below.
Statement ::= if Expression: <newline> Statements
Python also supports alternative clauses but uses else to distinguish them:
Statement ::= if (Expression) : #if test
Statements1
elif : #else if in Python
Statements2
else :
Statements3
Statement ::= while Expression:The indentation of the statements determines the statements that are in the loop body.
Statements
Here is an example that will print out the first 10 Fibonacci numbers:
i = 1; a = 1; b = 1; while (i <= 10): print "Fibonacci %s = %s" % (i,b); next = a + b; a = b; b = next; i = i + 1; print "Done."
Statement ::= for i in range(n):
Statements
The indentation of the statements determines the statements that are looped. For the value of n either an integer can be used or abother function, such as the length of a string (which we obtain in Python using len(Expression). You can define a string by putting it it single quotes.
Also you can use a for loop to iterate over the items in a collection object (see the section on Lists).
Python has latent (invisible) types that are checked dynamically (we will cover what this means later in the class). The four types you will find most useful are numbers, strings, lists, and dictionaries.
Python does not do exact arithmetic. Instead of using fractions, everything is treated as either an integer or a floating point number. Here are some examples:
four = 4
pi = 3.14159
nothalf = 1/2 #(evaluates to 0)
half = 1.0/2.0 #(evaluates to .5)
What happened when we defined nothalf?
Python makes a distinction between integer math and floating point math. This normally isn't that important; when we do math between the same types, the result is the same type, and when we do math with a floating point number and an integer, the type returned is a floating point number. However, the result of division of an integer by another integer is not necessarily an integer. Python will silently discard the fractional part of the answer. By writing the operands with decimal points, we ensure that the operation will return a floating point number.
In Python strings need quotation marks. You can use single quotes ('), double quotes ("), or triple quotes ("""). However, there is a very important difference between triple quotes and other quotes; when you use triple quotes you can break lines in the string, but when you use single or double quotes you can not.
You can concatenate (run together) two strings by using the plus sign (+):
name = "Spot"If you want a literal quote to appear in a string you print, use \":
print "See " + name + " run."
print "My name is \"Spot\"."The triple quote, """ is an easy way to print large chunks of text that can include double quotes and new lines. For example,
print """ "I find that the harder I work, the more luck I seem to have." --- Thomas Jefferson """will print out
"I find that the harder I work, the more luck I seem to have."
--- Thomas Jefferson
lst = [10, 20, 30]
print lst[0]
10
print lst[2]
30
Indices in Python are much more powerful than that, however. We can "slice" lists to only look at a subset of the list data.
lst = [10, 20, 40, "string", 302.234]
print lst[0:2]
[10, 20]
print lst[:3]
[10, 20, 40]
print lst[1:]
[20, 40, 'string', 302.23399999999998]
(Note that decimal numbers are not exact in Python! We put 302.234 in the list, but the printed value is 302.23399999999998.)
Finally, if we want to iterate over an entire list, we use the for statement:
Statement ::= for VaribleList in ExpressionList : StatementsFor example,
def sum (lst): total = 0 for el in lst: total = total + el return total print sum([1, 2, 3, 4])
10
A dictionary is a list of (key, value) pairs. In Python, the key can be any "immutable" object (tuples, strings, numbers), and the value can be any Python object. We can initialize a dictionary using the initialization expression:
DictionaryInitializationStatment ::= Variable = { InitializationExpressions }
InitializationExpressions ::=
InitializationExpressions ::= InitializationExpression, InitializationExpressions
InitializationExpression ::= Expression : Expression
The expression before the : is the key, and the following expression is the associated value.
We can set the value associated with a key using a set statement:
SetStatement ::= Expression [ Expression ] = ExpressionWe get the value associated with a key using:
FetchExpression ::= Expression [ Expression ]The first expression in a fetch expression must evaluate to a dictionary.
Here are some examples:
yellow = { 'red': 255, 'green': 255, 'blue': 0 } print '(%s, %s, %s)' % (yellow['red'], yellow['green'], yellow['blue'])(255, 255, 0)
yellow['red'] = 230 print '(%s, %s, %s)' % (yellow['red'], yellow['green'], yellow['blue'])(230, 255, 0)
Of course, we can put arrays in arrays, just like we can make lists of lists in Scheme. To do something to everything in the array, we first have to use the built-in "keys" method of the dictionary. For example, to reset yellow, we would do:
for key in yellow.keys():
yellow[key] = 0
Python provides classes as a way to package procedures and state, similar to Java classes.
ClassDefinition ::= class Name:
FunctionDefinitions
These function definitions will form the "methods" of the class. There are lots of special methods, and we can also create our own. The only special method we'll worry about now is the __init__ method that is used when we create an instance of an object.
For example, here is the Timer class from Problem Set 1:
import time # this imports the time library module class Timer: def __init__ (self): self.running = 0 self.startTime = 0 self.endTime = 0 def start (self): self.running = 1 self.startTime = time.clock() def stop (self): self.endTime = time.clock () self.running = 0 # pre: The timer must not be running. # post: Returns the elapsed time (between the start and stop events) # in seconds. def elapsed (self): assert (not self.running) return self.endTime - self.startTime
To create an instance of a class we use the class name (this invokes the special __init__ method:
The syntax for calling methods is similar to Java:import Timer timer = Timer.Timer ()
will invoke the start method on the Timer object we created.timer.start()