Note: References to Monty Python are officially encoraged!!
You can invoke python by typing 'python' or better by writing a script. At the command line, the normal history and editing keys work.
The command line arguments are given to the script by 'sys.argv[...], with the command name at 0.
Errors are shown by printing a 'stack trace' and returning a non-zero exit status.
The comment char is '#'.
Numbers
Math works in the obvious way, using integer math if all arguments
are ints. '**' is exponentiation.
Assignment works in the obvious way 'a = 4' is fine, no semicolon needed.
Also, x = y= z = 3 works.
Imaginary numbers are assigned like this 'a=(1+2j)'. Note that
it's a 'j' not an 'i', and that 'j' by itself not a variable. Therefore,
you must say '1j' and not 'j'.
a.real and a.imag are the components of a. This only works on
numbers of type complex, and not on reals or ints.
Mutliple assignment works like this: a,b = 1,2. The right hand side is fully evaluated before the left hand side is changed. So how do you write swap?
Strings
Can be enclosed by single or double quotes.
Extended strings can be enclosed by tripple-quotes. In this case leading whitespace is significant.
""" Hi Mom.
A New Line.""""
Plus '+' concatenates strings. Times '*' repeats strings.
Strings can be accessed like arrays of chars. a[3:5] is the third thru fifth chars of a. Indexing starts at zero. Index values that are ommitted, or out of range are adjusted to be the end of the string. Note that a[1:2] yeilds ONE char, not two. Negative indexes count from the right edge of the string, not the left.
len returns the length of a string: len(a)
Lists
Lists can be assigned like this: a=[1,3,2,4]
List elements can be accessed using array notation, and changed that way also.
len returns the length of a list: len(a).
Lists of lists can be created: l=[1,2,[3,4],5]. Elements within included lists can be accessed like this. l[2][1] = 4.
Boolean Expressions
Primatives: <, <=, ==, >=, >, and, or , not.
Weird examples: b < c < d
a == b or 4
You can compare any to things. It's O.K. to compare a string to
a list, or two lists, for example. The outcome is deterministic but
arbitrary: the types are ordered by their name. Thus, a list is always
smaller than a string, a string is always smaller than a tuple, etc. Mixed
numeric types are compared according to their numeric value, so 0 equals
0.0, etc.
Control Structure Examples
If
>>> #
[Code which sets 'x' to a value...]
>>> if x < 0:
...
x = 0
...
print 'Negative changed to zero'
... elif x == 0:
...
print 'Zero'
... elif x == 1:
...
print 'Single'
... else:
...
print 'More'
...
For Loops
For loops don't iterate over numbers, but over sequences.
Don't mess with the loop sequence.
Break and continue do the obvious things.
Else clauses for for loops execute on loop exhaustion,
but not on break.
>>> # Measure some strings:
... a = ['cat', 'window', 'defenestrate']
>>> for x in a:
... print
x, len(x)
...
Example iterating over ints
>>> for x in range(5,10):
... print
x
Example 'else' clause
... for
x in range(2, n):
...
if n % x == 0:
...
print n, 'equals', x, '*', n/x
...
break
... else:
...
print n, 'is a prime number'
...
Pass Statements
The pass statement does nothing. It can be used when
a statement is required syntactically but the program requires no action.
For example:
>>> while 1:
...
pass # Busy-wait for keyboard interrupt
...
Example Program
>>> # Fibonacci
series:
... # the sum of two elements defines
the next
... a, b = 0, 1
>>> while b < 10:
...
print b
...
a, b = b, a+b
...
1
1
2
3
5
8
Defining Functions
Function Returning Values
>>> def fib2(n): # return Fibonacci series up to n ... "Return a list containing the Fibonacci series up to n" ... result = [] ... a, b = 0, 1 ... while b < n: ... result.append(b) # see below ... a, b = b, a+b ... return result ... >>> f100 = fib2(100) # call it >>> f100 # write the result [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |
Function returning void
def fib(n): # write Fibonacci series up to n ... "Print a Fibonacci series up to n" ... a, b = 0, 1 ... while b < n: ... print b, ... a, b = b, a+b ... >>> # Now call the function we just defined: ... fib(2000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 |
Function with Default Arguments
>>> def fib3(n = 10): ... "Fib with default value of ten" ... return fib2(n) ... >>> f() [1, 1, 2, 3, 5, 8] >>> f(100) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |
Function with Keyword Arguments
def parrot(voltage, state='a stiff', action='voom', type='Norwegian Blue'): print "-- This parrot wouldn't", action, print "if you put", voltage, "Volts through it." print "-- Lovely plumage, the", type print "-- It's", state, "!" could be called in any of the following ways: parrot(1000)
but the following calls would all be invalid: parrot()
# required argument missing
|
Questions:
Can substrings be assigned? a[3] = 'a'?
What do negative index string values do?
What are string-int and string/int?
Does slicing work on lists? What about out of bounds slice values?
What happens if the slice indexes are out of order a[3:2].
How do you iterate over the index numbers of a sequence?