Monday, 6 March 2017

Python Glossy*

Glossary
Quick reference guide to Python.

">>>"
The default Python prompt of the interactive shell.

Often seen for code examples which can be executed interactively in the
interpreter.
abs
Return the absolute value of a number.
argument
Extra information which the computer uses to perform commands.
argparse
Argparse is a parser for command-line options, arguments and subcommands.
assert
Used during debugging to check for conditions that ought to apply
assignment
Giving a value to a variable.
block
Section of code which is grouped together
break
Used to exit a for loop or a while loop.
class
A template for creating user-defined objects.
compiler
Translates a program written in a high-level language into a low-level language.
continue
Used to skip the current block, and return to the "for" or "while" statement
conditional statement
Statement that contains an "if" or "if/else".
debugging
The process of finding and removing programming errors.
def
Defines a function or method
dictionary
A mutable associative array (or dictionary) of key and value pairs.

Can contain mixed types (keys and values). Keys must be a hashable type.
distutils
Package included in the Python Standard Library for installing, building and
distributing Python code.
docstring
A docstring is a string literal that occurs as the first statement in a module,
function, class, or method definition.
__future__
A pseudo-module which programmers can use to enable new language features which
are not compatible with the current interpreter.
easy_install
Easy Install is a python module (easy_install) bundled with setuptools that lets
you automatically download, build, install, and manage Python packages.
evaluation order
Python evaluates expressions from left to right. Notice that while evaluating
an assignment, the right-hand side is evaluated before the left-hand side.
exceptions
Means of breaking out of the normal flow of control of a code block in order to
handle errors or other exceptional conditions
expression
Python code that produces a value.
filter
filter(function, sequence) returns a sequence consisting of those items from the
sequence for which function(item) is true
float
An immutable floating point number.
for
Iterates over an iterable object, capturing each element to a local variable for
use by the attached block
function
A parameterized sequence of statements.
function call
An invocation of the function with arguments.
garbage collection
The process of freeing memory when it is not used anymore.
generators
A function which returns an iterator.
high level language
Designed to be easy for humans to read and write.
IDLE
Integrated development environment
if statement
Conditionally executes a block of code, along with else and elif
(a contraction of else-if).
immutable
Cannot be changed after its created.
import
Used to import modules whose functions or variables can be used in the current
program.
indentation
Python uses white-space indentation, rather than curly braces or keywords,
to delimit blocks.
int
An immutable integer of unlimited magnitude.
interactive mode
When commands are read from a tty, the interpreter is said to be in interactive
mode.
interpret
Execute a program by translating it one line at a time.
IPython
Interactive shell for interactive computing.
iterable
An object capable of returning its members one at a time.
lambda
They are a shorthand to create anonymous functions.
list
Mutable list, can contain mixed types.
list comprehension
A compact way to process all or part of the elements in a sequence and return
a list with the results.
literals
Literals are notations for constant values of some built-in types.
map
map(function, iterable, ...) Apply function to every item of iterable and return
a list of the results.
methods
A method is like a function, but it runs "on" an object.
module
The basic unit of code reusability in Python. A block of code imported by some
other code.
object
Any data with state (attributes or value) and defined behavior (methods).
object-oriented
allows users to manipulate data structures called objects in order to build and
execute programs.
pass
Needed to create an empty code block
PEP 8
A set of recommendations how to write Python code.
Python Package Index
Official repository of third-party software for Python
Pythonic
An idea or piece of code which closely follows the most common idioms of the
Python language, rather than implementing code using concepts common to other
languages.
reduce
reduce(function, sequence) returns a single value constructed by calling the
(binary) function on the first two items of the sequence,
then on the result and the next item, and so on.
set
Unordered set, contains no duplicates
setuptools
Collection of enhancements to the Python distutils that allow you to more easily
build and distribute Python packages
slice
Sub parts of sequences
str
A character string: an immutable sequence of Unicode codepoints.
strings
Can include numbers, letters, and various symbols and be enclosed by either
double or single quotes, although single quotes are more commonly used.
statement
A statement is part of a suite (a "block" of code).
try
Allows exceptions raised in its attached code block to be caught and handled by
except clauses.
tuple
Immutable, can contain mixed types.
variables
Placeholder for texts and numbers.

The equal sign (=) is used to assign values to variables.
while
Executes a block of code as long as its condition is true.
with
Encloses a code block within a context manager.

yield
Returns a value from a generator function.
Zen of Python
When you type "import this", Python’s philosophy is printed out.

Dealing with the imperfect - python (part 16)

Dealing with the imperfect

...or how to handle errors
So you now have the perfect program, it runs flawlessly, except for one detail, it will crash on invalid user input. Have no fear, for Python has a special control structure for you. It's called try and it tries to do something. Here is an example of a program with a problem:
print "Type Control C or -1 to exit"
number = 1
while number != -1:
    number = int(raw_input("Enter a number: "))
    print "You entered:", number
Notice how when you enter @#& it outputs something like:
Traceback (innermost last):
File "try_less.py", line 4, in ?
    number = int(raw_input("Enter a number: "))</source>

ValueError: invalid literal for int(): @#&
As you can see the int() function is unhappy with the number @#& (as well it should be). The last line shows what the problem is; Python found a ValueError. How can our program deal with this? What we do is first: put the place where the errors occurs in a try block, and second: tell Python how we want ValueErrors handled. The following program does this:
print "Type Control C or -1 to exit"
number = 1
while number != -1:
    try:
        number = int(raw_input("Enter a number: "))
        print "You entered:", number
    except ValueError:
        print "That was not a number."
Now when we run the new program and give it @#& it tells us "That was not a number." and continues with what it was doing before.
When your program keeps having some error that you know how to handle, put code in a try block, and put the way to handle the error in the except block.
Here is a more complex example of Error Handling.
# Program by Mitchell Aikens 2012
# No copyright.
import math

def main():
    try:
        epact()
    except ValueError:
        print "Error. Please enter an integer value."
        year = 0
        epact()
    except NameError:
        print "Error. Please enter an integer value."
        year = 0
        epact()
    except SyntaxError:
        print "Error. Please enter an integer value."
        year = 0
        epact()
    finally:
        print "Program Complete"

def epact():

    year = int(input("What year is it?\n"))
    C = year/100
    epactval = (8 + (C/4) - C + ((8*C + 13)/25) + 11 * (year%19))%30
    print "The Epact is: ",epactval

main()
The program above uses concepts from previous lessons as well as the current lesson. Let's look at the above program in sections.
After we define the function called "main", we tell it that we want to "try" function named "epact." The interpreter then goes to the the line year = int(input("What year is it?\n")). The interpreter takes the value entered by the user and stores it in the variable named "year".
If the value entered is not an integer or a floating point number (which would be converted to an integer by the interpreter), an exception would be raised.
Let's look at some possible exceptions. the program above does not have an except clause for every possible exception, as there are numerous types or exceptions.
If the value entered for year is an alphabetical character, a NameError exception is raised. In the program above, this is caught by the except NameError: line, and the interpreter executes the print statement below the except NameError:, then it sets the value of "year" to 0 as a precaution, clearing it of any non-numeric number. The interpreter then jumps back to the first line of the epact() function, and the process restarts.
The process above would be the same for the other exceptions we have. If an exception is raised, and there is an except clause for it in our program, the interpreter will jump to the statements under the appropriate except clause, and execute them.
The finally statement, is sometimes used in exception handling as well. Think of it as the trump card. Statements underneath the finally clause will be executed regardless of if we raise and exception or not. The finally statement will be executed after any try or except clauses prior to it.
In the case of the program above, our exception handlers serve as a type of recursion or loop. When we raise an exception that is handled, the statements underneath our handlers take us back to the beginning of the epact function. This forces the user to keep entering a value, until a valid numeric value is entered. This means our finally statement won't execute until a valid value is entered, because the except statements send us back tot he beginning of the function, which makes it possible to raise yet another exception, which will be handled, and will send us back to the beginning and...well, you get the idea.
Below is a simpler example where we are not looped, and the finally clause is executed regardless of exceptions.
#Program By Mitchell Aikens 2012
#Not copyright.

def main():
    try:
        number = int(input("Please enter a number.\n"))
        half = number/2
        print "Half of the number you entered is ",half
    except NameError:
        print "Error."
    except ValueError:
        print "Error."
    except SyntaxError:
        print "Error."
    finally:
        print "I am executing the finally clause."

main()
If we were to enter an alphabetic value for number = int(input("Please enter a number.\n")), the output would be as follows:
Please enter a number.
t
Error.
I am executing the finally clause.
Exercises
Update at least the phone numbers program (in section File IO) so it doesn't crash if a user doesn't enter any data at the menu

File IO - python (part 15)

File IO

Here is a simple example of file IO (input/output):
# Write a file
out_file = open("test.txt", "w")
out_file.write("This Text is going to out file\nLook at it and see!")
out_file.close()

# Read a file
in_file = open("test.txt", "r")
text = in_file.read()
in_file.close()

print text
The output and the contents of the file test.txt are:
This Text is going to out file
Look at it and see!
Notice that it wrote a file called test.txt in the directory that you ran the program from. The \n in the string tells Python to put a newline where it is.
A overview of file IO is:
Get a file object with the open function.
Read or write to the file object (depending on how it was opened)
Close it
The first step is to get a file object. The way to do this is to use the open function. The format is file_object = open(filename, mode) where file_object is the variable to put the file object, filename is a string with the filename, and mode is "r" to read a file or "w" to write a file (and a few others we will skip here). Next the file objects functions can be called. The two most common functions are read and write. The write function adds a string to the end of the file. The read function reads the next thing in the file and returns it as a string. If no argument is given it will return the whole file (as done in the example).
Now here is a new version of the phone numbers program that we made earlier:
def print_numbers(numbers):
    print "Telephone Numbers:"
    for x in numbers.keys():
        print "Name:", x, "\tNumber:", numbers[x]
    print

def add_number(numbers, name, number):
    numbers[name] = number

def lookup_number(numbers, name):
    if name in numbers:
        return "The number is " + numbers[name]
    else:
        return name + " was not found"

def remove_number(numbers, name):
    if name in numbers:
        del numbers[name]
    else:
        print name," was not found"

def load_numbers(numbers, filename):
    in_file = open(filename, "r")
    while True:
        in_line = in_file.readline()
        if not in_line:
            break
        in_line = in_line[:-1]
        name, number = in_line.split(",")
        numbers[name] = number
    in_file.close()

def save_numbers(numbers, filename):
    out_file = open(filename, "w")
    for x in numbers.keys():
        out_file.write(x + "," + numbers[x] + "\n")
    out_file.close()

def print_menu():
    print '1. Print Phone Numbers'
    print '2. Add a Phone Number'
    print '3. Remove a Phone Number'
    print '4. Lookup a Phone Number'
    print '5. Load numbers'
    print '6. Save numbers'
    print '7. Quit'
    print

phone_list = {}
menu_choice = 0
print_menu()
while True:
    menu_choice = input("Type in a number (1-7): ")
    if menu_choice == 1:
        print_numbers(phone_list)
    elif menu_choice == 2:
        print "Add Name and Number"
        name = raw_input("Name: ")
        phone = raw_input("Number: ")
        add_number(phone_list, name, phone)
    elif menu_choice == 3:
        print "Remove Name and Number"
        name = raw_input("Name: ")
        remove_number(phone_list, name)
    elif menu_choice == 4:
        print "Lookup Number"
        name = raw_input("Name: ")
        print lookup_number(phone_list, name)
    elif menu_choice == 5:
        filename = raw_input("Filename to load: ")
        load_numbers(phone_list, filename)
    elif menu_choice == 6:
        filename = raw_input("Filename to save: ")
        save_numbers(phone_list, filename)
    elif menu_choice == 7:
        break
    else:
        print_menu()

print "Goodbye"
Notice that it now includes saving and loading files. Here is some output of my running it twice:
1. Print Phone Numbers
2. Add a Phone Number
3. Remove a Phone Number
4. Lookup a Phone Number
5. Load numbers
6. Save numbers
7. Quit

Type in a number (1-7): 2
Add Name and Number
Name: Jill
Number: 1234
Type in a number (1-7): 2
Add Name and Number
Name: Fred
Number: 4321
Type in a number (1-7): 1
Telephone Numbers:
Name: Jill     Number: 1234
Name: Fred     Number: 4321

Type in a number (1-7): 6
Filename to save: numbers.txt
Type in a number (1-7): 7
Goodbye
1. Print Phone Numbers
2. Add a Phone Number
3. Remove a Phone Number
4. Lookup a Phone Number
5. Load numbers
6. Save numbers
7. Quit

Type in a number (1-7): 5
Filename to load: numbers.txt
Type in a number (1-7): 1
Telephone Numbers:
Name: Jill     Number: 1234
Name: Fred     Number: 4321

Type in a number (1-7): 7
Goodbye
The new portions of this program are:
def load_numbers(numbers, filename):
    in_file = open(filename, "r")
    while True:
        in_line = in_file.readline()
        if not in_line:
            break
        in_line = in_line[:-1]
        name, number = in_line.split(",")
        numbers[name] = number
    in_file.close()
def save_numbers(numbers, filename):
    out_file = open(filename, "w")
    for x in numbers.keys():
        out_file.write(x + "," + numbers[x] + "\n")
    out_file.close()
First we will look at the save portion of the program. First it creates a file object with the command open(filename, "w"). Next it goes through and creates a line for each of the phone numbers with the command out_file.write(x + "," + numbers[x] + "\n"). This writes out a line that contains the name, a comma, the number and follows it by a newline.
The loading portion is a little more complicated. It starts by getting a file object. Then it uses a while True: loop to keep looping until a break statement is encountered. Next it gets a line with the line in_line = in_file.readline(). The readline function will return a empty string when the end of the file is reached. The if statement checks for this and breaks out of the while loop when that happens. Of course if the readline function did not return the newline at the end of the line there would be no way to tell if an empty string was an empty line or the end of the file so the newline is left in what readline returns. Hence we have to get rid of the newline. The line in_line = in_line[:-1] does this for us by dropping the last character. Next the line name, number = in_line.split(",") splits the line at the comma into a name and a number. This is then added to the numbers dictionary.
Exercises
Now modify the grades program from section Dictionaries so that is uses file IO to keep a record of the students.

Revenge of the Strings - python (part 14)

Revenge of the Strings

And now presenting a cool trick that can be done with strings:
def shout(string):
    for character in string:
        print "Gimme a " + character
        print "'" + character + "'"
shout("Lose")

def middle(string):
    print "The middle character is:", string[len(string) / 2]

middle("abcdefg")
middle("The Python Programming Language")
middle("Atlanta")
And the output is:
Gimme a L
'L'
Gimme a o
'o'
Gimme a s
's'
Gimme a e
'e'
The middle character is: d
The middle character is: r
The middle character is: a
What these programs demonstrate is that strings are similar to lists in several ways. The shout() function shows that for loops can be used with strings just as they can be used with lists. The middle procedure shows that that strings can also use the len() function and array indexes and slices. Most list features work on strings as well.
The next feature demonstrates some string specific features:
def to_upper(string):
    ## Converts a string to upper case
    upper_case = ""
    for character in string:
        if 'a' <= character <= 'z':
            location = ord(character) - ord('a')
            new_ascii = location + ord('A')
            character = chr(new_ascii)
        upper_case = upper_case + character
    return upper_case

print to_upper("This is Text")
with the output being:
THIS IS TEXT
This works because the computer represents the characters of a string as numbers from 0 to 255. Python has a function called ord() (short for ordinal) that returns a character as a number. There is also a corresponding function called chr() that converts a number into a character. With this in mind the program should start to be clear. The first detail is the line: if 'a' <= character <= 'z': which checks to see if a letter is lower case. If it is then the next lines are used. First it is converted into a location so that a = 0, b = 1, c = 2 and so on with the line: location = ord(character) - ord('a'). Next the new value is found with new_ascii = location + ord('A'). This value is converted back to a character that is now upper case.
Now for some interactive typing exercise:
>>> # Integer to String
>>> 2
2
>>> repr(2)
'2'
>>> -123
-123
>>> repr(-123)
'-123'
>>> `123`
'123'
>>> # String to Integer
>>> "23"
'23'
>>> int("23")
23
>>> "23" * 2
'2323'
>>> int("23") * 2
46
>>> # Float to String
>>> 1.23
1.23
>>> repr(1.23)
'1.23'
>>> # Float to Integer
>>> 1.23
1.23
>>> int(1.23)
1
>>> int(-1.23)
-1
>>> # String to Float
>>> float("1.23")
1.23
>>> "1.23"
'1.23'
>>> float("123")
123.0
>>> `float("1.23")`
'1.23'
If you haven't guessed already the function repr() can convert a integer to a string and the function int() can convert a string to an integer. The function float() can convert a string to a float. The repr() function returns a printable representation of something. `...` converts almost everything into a string, too. Here are some examples of this:
>>> repr(1)
'1'
>>> repr(234.14)
'234.14'
>>> repr([4, 42, 10])
'[4, 42, 10]'
>>> `[4, 42, 10]`
'[4, 42, 10]'
The int() function tries to convert a string (or a float) into a integer. There is also a similar function called float() that will convert a integer or a string into a float. Another function that Python has is the eval() function. The eval() function takes a string and returns data of the type that python thinks it found. For example:
>>> v = eval('123')
>>> print v, type(v)
123 <type 'int'>
>>> v = eval('645.123')
>>> print v, type(v)
645.123 <type 'float'>
>>> v = eval('[1, 2, 3]')
>>> print v, type(v)
[1, 2, 3] <type 'list'>
If you use the eval() function you should check that it returns the type that you expect.
One useful string function is the split() method. Here's an example:
>>> "This is a bunch of words".split()
['This', 'is', 'a', 'bunch', 'of', 'words']
>>> text = "First batch, second batch, third, fourth"
>>> text.split(",")
['First batch', ' second batch', ' third', ' fourth']
Notice how split() converts a string into a list of strings. The string is split by whitespace by default or by the optional argument (in this case a comma). You can also add another argument that tells split() how many times the separator will be used to split the text. For example:
>>> list = text.split(",")
>>> len(list)
4
>>> list[-1]
' fourth'
>>> list = text.split(",", 2)
>>> len(list)
3
>>> list[-1]
' third, fourth'
Slicing strings (and lists)
Strings can be cut into pieces — in the same way as it was shown for lists in the previous chapter — by using the slicing "operator" [:]. The slicing operator works in the same way as before: text[first_index:last_index] (in very rare cases there can be another colon and a third argument, as in the example shown below).
In order not to get confused by the index numbers, it is easiest to see them as clipping places, possibilities to cut a string into parts. Here is an example, which shows the clipping places (in yellow) and their index numbers (red and blue) for a simple text string:

0
1
2
...
-2
-1









text = "
S
T
R
I
N
G
"











[:








:]
Note that the red indexes are counted from the beginning of the string and the blue ones from the end of the string backwards. (Note that there is no blue -0, which could seem to be logical at the end of the string. Because -0 == 0, (-0 means "beginning of the string" as well.) Now we are ready to use the indexes for slicing operations:
text[1:4] → "TRI"
text[:5] → "STRIN"
text[:-1] → "STRIN"
text[-4:] → "RING"
text[2] → "R"
text[:] → "STRING"
text[::-1] → "GNIRTS"
text[1:4] gives us all of the text string between clipping places 1 and 4, "TRI". If you omit one of the [first_index:last_index] arguments, you get the beginning or end of the string as default: text[:5] gives "STRIN". For both first_index and last_index we can use both the red and the blue numbering schema: text[:-1] gives the same astext[:5], because the index -1 is at the same place as 5 in this case. If we do not use an argument containing a colon, the number is treated in a different way: text[2] gives us one character following the second clipping point, "R". The special slicing operation text[:] means "from the beginning to the end" and produces a copy of the entire string (or list, as shown in the previous chapter).
Last but not least, the slicing operation can have a second colon and a third argument, which is interpreted as the "step size": text[::-1] is text from beginning to the end, with a step size of -1. -1 means "every character, but in the other direction". "STRING" backwards is "GNIRTS" (test a step length of 2, if you have not got the point here).
All these slicing operations work with lists as well. In that sense strings are just a special case of lists, where the list elements are single characters. Just remember the concept ofclipping places, and the indexes for slicing things will get a lot less confusing.
Examples
# This program requires an excellent understanding of decimal numbers
def to_string(in_int):
    """Converts an integer to a string"""
    out_str = ""
    prefix = ""
    if in_int < 0:
        prefix = "-"
        in_int = -in_int
    while in_int / 10 != 0:
        out_str = chr(ord('0') + in_int % 10) + out_str
        in_int = in_int / 10
    out_str = chr(ord('0') + in_int % 10) + out_str
    return prefix + out_str

def to_int(in_str):
    """Converts a string to an integer"""
    out_num = 0
    if in_str[0] == "-":
        multiplier = -1
        in_str = in_str[1:]
    else:
        multiplier = 1
    for x in range(0, len(in_str)):
        out_num = out_num * 10 + ord(in_str[x]) - ord('0')
    return out_num * multiplier

print to_string(2)
print to_string(23445)
print to_string(-23445)
print to_int("14234")
print to_int("12345")
print to_int("-3512")
The output is:
2
23445
-23445
14234
12345
-3512

More on Lists - python (part 13)

More on Lists

We have already seen lists and how they can be used. Now that you have some more background I will go into more detail about lists. First we will look at more ways to get at the elements in a list and then we will talk about copying them.
Here are some examples of using indexing to access a single element of a list:
>>> some_numbers = ['zero', 'one', 'two', 'three', 'four', 'five']
>>> some_numbers[0]
'zero'
>>> some_numbers[4]
'four'
>>> some_numbers[5]
'five'
All those examples should look familiar to you. If you want the first item in the list just look at index 0. The second item is index 1 and so on through the list. However what if you want the last item in the list? One way could be to use the len() function like some_numbers[len(some_numbers) - 1]. This way works since the len() function always returns the last index plus one. The second from the last would then be some_numbers[len(some_numbers) - 2]. There is an easier way to do this. In Python the last item is always index -1. The second to the last is index -2 and so on. Here are some more examples:
>>> some_numbers[len(some_numbers) - 1]
'five'
>>> some_numbers[len(some_numbers) - 2]
'four'
>>> some_numbers[-1]
'five'
>>> some_numbers[-2]
'four'
>>> some_numbers[-6]
'zero'
Thus any item in the list can be indexed in two ways: from the front and from the back.
Another useful way to get into parts of lists is using slicing. Here is another example to give you an idea what they can be used for:
>>> things = [0, 'Fred', 2, 'S.P.A.M.', 'Stocking', 42, "Jack", "Jill"]
>>> things[0]
0
>>> things[7]
'Jill'
>>> things[0:8]
[0, 'Fred', 2, 'S.P.A.M.', 'Stocking', 42, 'Jack', 'Jill']
>>> things[2:4]
[2, 'S.P.A.M.']
>>> things[4:7]
['Stocking', 42, 'Jack']
>>> things[1:5]
['Fred', 2, 'S.P.A.M.', 'Stocking']
Slicing is used to return part of a list. The slicing operator is in the form things[first_index:last_index]. Slicing cuts the list before the first_index and before thelast_index and returns the parts inbetween. You can use both types of indexing:
>>> things[-4:-2]
['Stocking', 42]
>>> things[-4]
'Stocking'
>>> things[-4:6]
['Stocking', 42]
Another trick with slicing is the unspecified index. If the first index is not specified the beginning of the list is assumed. If the last index is not specified the whole rest of the list is assumed. Here are some examples:
>>> things[:2]
[0, 'Fred']
>>> things[-2:]
['Jack', 'Jill']
>>> things[:3]
[0, 'Fred', 2]
>>> things[:-5]
[0, 'Fred', 2]
Here is a (HTML inspired) program example (copy and paste in the poem definition if you want):
poem = ["<B>", "Jack", "and", "Jill", "</B>", "went", "up", "the",
        "hill", "to", "<B>", "fetch", "a", "pail", "of", "</B>",
        "water.", "Jack", "fell", "<B>", "down", "and", "broke",
        "</B>", "his", "crown", "and", "<B>", "Jill", "came",
        "</B>", "tumbling", "after"]

def get_bolds(text):
    true = 1
    false = 0
    ## is_bold tells whether or not the we are currently looking at
    ## a bold section of text.
    is_bold = false
    ## start_block is the index of the start of either an unbolded
    ## segment of text or a bolded segment.
    start_block = 0
    for index in range(len(text)):
        ## Handle a starting of bold text
        if text[index] == "<B>":
            if is_bold:
                print "Error: Extra Bold"
            ## print "Not Bold:", text[start_block:index]
            is_bold = true
            start_block = index + 1
        ## Handle end of bold text
        ## Remember that the last number in a slice is the index
        ## after the last index used.
        if text[index] == "</B>":
            if not is_bold:
                print "Error: Extra Close Bold"
            print "Bold [", start_block, ":", index, "]", text[start_block:index]
            is_bold = false
            start_block = index + 1

get_bolds(poem)
with the output being:
Bold [ 1 : 4 ] ['Jack', 'and', 'Jill']
Bold [ 11 : 15 ] ['fetch', 'a', 'pail', 'of']
Bold [ 20 : 23 ] ['down', 'and', 'broke']
Bold [ 28 : 30 ] ['Jill', 'came']
The get_bold() function takes in a list that is broken into words and tokens. The tokens that it looks for are <B> which starts the bold text and </B> which ends bold text. The function get_bold() goes through and searches for the start and end tokens.
The next feature of lists is copying them. If you try something simple like:
>>> a = [1, 2, 3]
>>> b = a
>>> print b
[1, 2, 3]
>>> b[1] = 10
>>> print b
[1, 10, 3]
>>> print a
[1, 10, 3]
This probably looks surprising since a modification to b resulted in a being changed as well. What happened is that the statement b = a makes b a reference to a. This means that b can be thought of as another name for a. Hence any modification to b changes a as well. However some assignments don't create two names for one list:
>>> a = [1, 2, 3]
>>> b = a * 2
>>> print a
[1, 2, 3]
>>> print b
[1, 2, 3, 1, 2, 3]
>>> a[1] = 10
>>> print a
[1, 10, 3]
>>> print b
[1, 2, 3, 1, 2, 3]
In this case b is not a reference to a since the expression a * 2 creates a new list. Then the statement b = a * 2 gives b a reference to a * 2 rather than a reference to a. All assignment operations create a reference. When you pass a list as an argument to a function you create a reference as well. Most of the time you don't have to worry about creating references rather than copies. However when you need to make modifications to one list without changing another name of the list you have to make sure that you have actually created a copy.
There are several ways to make a copy of a list. The simplest that works most of the time is the slice operator since it always makes a new list even if it is a slice of a whole list:
>>> a = [1, 2, 3]
>>> b = a[:]
>>> b[1] = 10
>>> print a
[1, 2, 3]
>>> print b
[1, 10, 3]
Taking the slice [:] creates a new copy of the list. However it only copies the outer list. Any sublist inside is still a references to the sublist in the original list. Therefore, when the list contains lists, the inner lists have to be copied as well. You could do that manually but Python already contains a module to do it. You use the deepcopy function of the copymodule:
>>> import copy
>>> a = [[1, 2, 3], [4, 5, 6]]
>>> b = a[:]
>>> c = copy.deepcopy(a)
>>> b[0][1] = 10
>>> c[1][1] = 12
>>> print a
[[1, 10, 3], [4, 5, 6]]
>>> print b
[[1, 10, 3], [4, 5, 6]]
>>> print c
[[1, 2, 3], [4, 12, 6]]
First of all notice that a is a list of lists. Then notice that when b[0][1] = 10 is run both a and b are changed, but c is not. This happens because the inner arrays are still references when the slice operator is used. However with deepcopy c was fully copied.
So, should I worry about references every time I use a function or =? The good news is that you only have to worry about references when using dictionaries and lists. Numbers and strings create references when assigned but every operation on numbers and strings that modifies them creates a new copy so you can never modify them unexpectedly. You do have to think about references when you are modifying a list or a dictionary.
By now you are probably wondering why are references used at all? The basic reason is speed. It is much faster to make a reference to a thousand element list than to copy all the elements. The other reason is that it allows you to have a function to modify the inputed list or dictionary. Just remember about references if you ever have some weird problem with data being changed when it shouldn't be.

Modules - Python (part 12)

Using Modules 

Here's this chapter's typing exercise (name it cal.py (import actually looks for a file named calendar.py and reads it in. If the file is named calendar.py and it sees a "import calendar" it tries to read in itself which works poorly at best.)):

import calendar
year = input("Type in the year number: ")
calendar.prcal(year)
And here is part of the output I got:

Type in the year number: 2001

                                 2001                                 

       January                  February                    March     

Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su      Mo Tu We Th Fr Sa Su
1  2  3  4  5  6  7                1  2  3  4                1  2  3  4    
8  9 10 11 12 13 14       5  6  7  8  9 10 11       5  6  7  8  9 10 11
15 16 17 18 19 20 21      12 13 14 15 16 17 18      12 13 14 15 16 17 18    
22 23 24 25 26 27 28      19 20 21 22 23 24 25      19 20 21 22 23 24 25    
29 30 31                  26 27 28                  26 27 28 29 30 31       
(I skipped some of the output, but I think you get the idea.) So what does the program do? The first line import calendar uses a new command import. The command import loads a module (in this case the calendar module). To see the commands available in the standard modules either look in the library reference for python (if you downloaded it) or go to http://docs.python.org/library/. If you look at the documentation for the calendar module, it lists a function called prcal that prints a calendar for a year. The line calendar.prcal(year) uses this function. In summary to use a module import it and then use module_name.function for functions in the module. Another way to write the program is:

from calendar import prcal

year = input("Type in the year number: ")
prcal(year)
This version imports a specific function from a module. Here is another program that uses the Python Library (name it something like clock.py) (press Ctrl and the 'c' key at the same time to terminate the program):

from time import time, ctime

prev_time = ""
while True:
    the_time = ctime(time())
    if prev_time != the_time:
        print "The time is:", ctime(time())
        prev_time = the_time
With some output being:

The time is: Sun Aug 20 13:40:04 2000
The time is: Sun Aug 20 13:40:05 2000
The time is: Sun Aug 20 13:40:06 2000
The time is: Sun Aug 20 13:40:07 2000

Traceback (innermost last):
File "clock.py", line 5, in ?
    the_time = ctime(time())

KeyboardInterrupt
The output is infinite of course so I canceled it (or the output at least continues until Ctrl+C is pressed). The program just does a infinite loop (True is always true, so while True: goes forever) and each time checks to see if the time has changed and prints it if it has. Notice how multiple names after the import statement are used in the line from time import time, ctime.

The Python Library contains many useful functions. These functions give your programs more abilities and many of them can simplify programming in Python.

Exercises

Rewrite the High_low.py program from section Decisions to use a random integer between 0 and 99 instead of the hard-coded 78. Use the Python documentation to find an appropriate module and function to do this.

Dictionaries - python (part 11)

Dictionaries 

This chapter is about dictionaries. If you open a dictionary, you should notice every entry consists of two parts, a word and the word's definition. The word is the key to finding out what a word means, and what the word means is considered the value for that key. In Python, dictionaries have keys and values. Keys are used to find values. Here is an example of a dictionary in use:
def print_menu():
    print '1. Print Dictionary'
    print '2. Add definition'
    print '3. Remove word'
    print '4. Lookup word'
    print '5. Quit'
    print

words = {}
menu_choice = 0
print_menu()

while menu_choice != 5:
    menu_choice = input("Type in a number (1-5): ")
    if menu_choice == 1:
        print "Definitions:"
        for x in words.keys():
            print x, ": ", words[x]
        print
    elif menu_choice == 2:
        print "Add definition"
        name = raw_input("Word: ")
        means = raw_input("Definition: ")
        words[name] = means
    elif menu_choice == 3:
        print "Remove word"
        name = raw_input("Word: ")
        if name in words:
            del words[name]
            print name, " was removed."
        else:
            print name, " was not found."
    elif menu_choice == 4:
        print "Lookup Word"
        name = raw_input("Word: ")
        if name in words:
            print "The definition of ", name, " is: ", words[name]
        else:
            print name, "No definition for ", name, " was found."
    elif menu_choice != 5:
        print_menu()
And here is my output:
1. Print Dictionary
2. Add definition
3. Remove word
4. Lookup word
5. Quit

Type in a number (1-5): 2
Add definition
Word: Python
Definition: A snake, a programming language, and a British comedy.
Type in a number (1-5): 2
Add definition
Word: Dictionary
Definition: A book where words are defined.
Type in a number (1-5): 1
Definitions:
Python: A snake, a programming language, and a British comedy.
Dictionary: A book where words are defined.

Type in a number (1-5): 4
Lookup Word
Word: Python
The definition of Python is: A snake, a programming language, and a British comedy.
Type in a number (1-5): 3
Remove Word
Word: Dictionary
Dictionary was removed.
Type in a number (1-5): 1
Definitions:
Python: A snake, a programming language, and a British comedy.
Type in a number (1-5): 5
This program is similar to the name list from the earlier chapter on lists (note that lists use indexes and dictionaries don't). Here's how the program works:
First the function print_menu is defined. print_menu just prints a menu that is later used twice in the program.
Next comes the funny looking line words = {}. All that line does is tell Python that words is a dictionary.
The next few lines just make the menu work.
for x in words.keys():
    print x, ": ", words[x]
This goes through the dictionary and prints all the information. The function words.keys() returns a list that is then used by the for loop. The list returned by keys() is not in any particular order so if you want it in alphabetic order it must be sorted. Similar to lists the statement words[x] is used to access a specific member of the dictionary. Of course in this case x is a string.
Next the line words[name] = means adds a word and definition to the dictionary. If name is already in the dictionary means replaces whatever was there before.
if name in words:
    del words[name]
See if name is in words and remove it if it is. The expression name in words returns true if name is a key in words but otherwise returns false. The line del words[name]removes the key name and the value associated with that key.
if name in words:
    print "The definition of ", name, " is: ", words[name]
Check to see if words has a certain key and if it does prints out the definition associated with it.
Lastly if the menu choice is invalid it reprints the menu for your viewing pleasure.
A recap: Dictionaries have keys and values. Keys can be strings or numbers. Keys point to values. Values can be any type of variable (including lists or even dictionaries (those dictionaries or lists of course can contain dictionaries or lists themselves (scary right? :-) )). Here is an example of using a list in a dictionary:
max_points = [25, 25, 50, 25, 100]
assignments = ['hw ch 1', 'hw ch 2', 'quiz   ', 'hw ch 3', 'test']
students = {'#Max': max_points}

def print_menu():
    print "1. Add student"
    print "2. Remove student"
    print "3. Print grades"
    print "4. Record grade"
    print "5. Print Menu"
    print "6. Exit"

def print_all_grades():
    print '\t',
    for i in range(len(assignments)):
        print assignments[i], '\t',
    print
    keys = students.keys()
    keys.sort()
    for x in keys:
        print x, '\t',
        grades = students[x]
        print_grades(grades)

def print_grades(grades):
    for i in range(len(grades)):
        print grades[i], '\t', '\t',
    print

print_menu()
menu_choice = 0
while menu_choice != 6:
    print
    menu_choice = input("Menu Choice (1-6): ")
    if menu_choice == 1:
        name = raw_input("Student to add: ")
        students[name] = [0] * len(max_points)
    elif menu_choice == 2:
        name = raw_input("Student to remove: ")
        if name in students:
            del students[name]
        else:
            print "Student:", name, "not found"
    elif menu_choice == 3:
        print_all_grades()
    elif menu_choice == 4:
        print "Record Grade"
        name = raw_input("Student: ")
        if name in students:
            grades = students[name]
            print "Type in the number of the grade to record"
            print "Type a 0 (zero) to exit"
            for i in range(len(assignments)):
                print i + 1, assignments[i], '\t',
            print
            print_grades(grades)
            which = 1234
            while which != -1:
                which = input("Change which Grade: ")
                which = which - 1
                if 0 <= which < len(grades):
                    grade = input("Grade: ")
                    grades[which] = grade
                elif which != -1:
                    print "Invalid Grade Number"
        else:
            print "Student not found"
    elif menu_choice != 6:
        print_menu()
and here is a sample output:
1. Add student
2. Remove student
3. Print grades
4. Record grade
5. Print Menu
6. Exit

Menu Choice (1-6): 3
       hw ch 1         hw ch 2         quiz            hw ch 3         test
#Max    25              25              50              25              100

Menu Choice (1-6): 5
1. Add student
2. Remove student
3. Print grades
4. Record grade
5. Print Menu
6. Exit

Menu Choice (1-6): 1
Student to add: Bill

Menu Choice (1-6): 4
Record Grade
Student: Bill
Type in the number of the grade to record
Type a 0 (zero) to exit
1   hw ch 1     2   hw ch 2     3   quiz        4   hw ch 3     5   test
0               0               0               0               0
Change which Grade: 1
Grade: 25
Change which Grade: 2
Grade: 24
Change which Grade: 3
Grade: 45
Change which Grade: 4
Grade: 23
Change which Grade: 5
Grade: 95
Change which Grade: 0

Menu Choice (1-6): 3
       hw ch 1         hw ch 2         quiz            hw ch 3         test
#Max    25              25              50              25              100
Bill    25              24              45              23              95

Menu Choice (1-6): 6
Heres how the program works. Basically the variable students is a dictionary with the keys being the name of the students and the values being their grades. The first two lines just create two lists. The next line students = {'#Max': max_points} creates a new dictionary with the key {#Max} and the value is set to be [25, 25, 50, 25, 100], since thats what max_points was when the assignment is made (I use the key #Max since # is sorted ahead of any alphabetic characters). Next print_menu is defined. Next the print_all_grades function is defined in the lines:
def print_all_grades():
    print '\t',
    for i in range(len(assignments)):
        print assignments[i], '\t',
    print
    keys = students.keys()
    keys.sort()
    for x in keys:
        print x, '\t',
        grades = students[x]
        print_grades(grades)
Notice how first the keys are gotten out of the students dictionary with the keys function in the line keys = students.keys(). keys is a list so all the functions for lists can be used on it. Next the keys are sorted in the line keys.sort() since it is a list. for is used to go through all the keys. The grades are stored as a list inside the dictionary so the assignment grades = students[x] gives grades the list that is stored at the key x. The function print_grades just prints a list and is defined a few lines later.
The later lines of the program implement the various options of the menu. The line students[name] = [0] * len(max_points) adds a student to the key of their name. The notation [0] * len(max_points) just creates a list of 0's that is the same length as the max_points list.
The remove student entry just deletes a student similar to the telephone book example. The record grades choice is a little more complex. The grades are retrieved in the linegrades = students[name] gets a reference to the grades of the student name. A grade is then recorded in the line grades[which] = grade. You may notice that gradesis never put back into the students dictionary (as in no students[name] = grades). The reason for the missing statement is that grades is actually another name forstudents[name] and so changing grades changes student[name].
Dictionaries provide a easy way to link keys to values. This can be used to easily keep track of data that is attached to various keys