Python Strings: Part One

Python strings

Introduction to Python Strings

A string in Python is an ordered collection of characters used to store and represent text-based information. From a functional perspective, strings can be used to represent just about anything that can be encoded as text. They can also be used to hold the absolute binary values values of bytes and multibyte Unicode text.

You may have used strings in other languages, and Python’s strings serve the same role as character arrays in languages such as C. In C, we might see a statement such as this:

char ch = ‘a’;

If we want to have a string, we would use something like this:

char *str = “Some arbitrary string”;

or:

char str[] = “Some arbitrary string”;

But in either case, our string is actually an array of characters. Python has no distinct type for individual characters; instead you just use one-character strings. Also, unlike in C, strings in Python are a somewhat higher-level tool and come with a powerful set of processing tools.

Python strings are categorized as immutable sequences, meaning that the characters they contain have a left-to-right positional order and that they cannot be changed in place. In fact, strings are a subset of the larger class of objects called sequences.

There are many ways to write strings in Python. This is a valid Python string:

a = ‘string’

But then again, so is this:

a = “string”

You can also use triple quotes:

a = ”’…string…”’
a = “””…string…”””

Around Python strings, single and double quote characters are interchangeable. That is, string literals can be written enclosed in either two single or two double quotes – the two forms work the same and return the same type of object. The reason for supporting both is that it allows you to embed a quote character of the other variety inside a string without escape it with a backslash. You may embed a single quote character in a string enclosed in double quote characters, and vice versa:

>>> ‘string”s’, “string’s”
(‘string”s’, “string’s”)

Incidentally, Python automatically concatenates adjacent string literals in any expression, although it is almost as simple to add a + operator between them to invoke concatenation explicitly:

>>> a = “Some ” ‘arbitrary’ ” string”
>>> a
‘Some arbitrary string’

Note that adding commas between these strings would result in a tuple, not a string. Also notice in all of these outputs that Python prefers to print strings in single quotes, unless they embed one. You can also embed quotes by escaping them with backslashes:

>>> ‘string\’s’, “string\”s”
(“string’s”, ‘string”s’)

External Links:

Strings at docs.python.org

Python Strings at Google for Developers

Python strings tutorial at afterhoursprogramming.com

Python Exceptions: Part Six

exceptionsAs a special case for debugging purposes, Python includes the assert statement; it can be thought of as a conditional raise statement. A statement of the form:

	assert , <test> <data>

works like the following code:

	if __debug__:
		if not :
			raise AssertionError()

In other words, if the test evaluates to false, Python raises an exception: the data item is used as the exception’s constructor argument, if a data item is provided. Like all exceptions, the AssertionError exception will kill your program if it’s not caught with a try, in which case the data item shows up as part of the error message. Otherwsie, AssertionError exceptions can be caught and handled like any other exception.

As an added feature, assert statements may be removed from a compiled program’s byte code if the -0 Python command-line flag is used, thus optimizing the program (similar to assert statements in C/C++). AssertionError is a built-in exception, and the __debug__ flag is a built-in name that is automatically set to True unless the -0 flag is used. You can use a command line like python -0 code.py to run in optimized mode and disable asserts.

Assertions are typically used to verify program conditions during development. When displayed, their error message text automatically includes source code line information and the value listed in the assert statement.

As an example, consider a function to convert from Fahrenheit to Celsius. We’ll make it bail out if it sees a temperature less than absolute zero:

def FahrenheitToCelsius(ftemp):
	assert (ftemp >= -460), "Less than absolute zero!"
	return ((ftemp-32)*(5.0/9.0))

FahrenheitToCelsius(32)
FahrenheitToCelsius(55)
FahrenheitToCelsius(-500)

When the above code is executed, it produces the following result:

0.0
12.777777777777779
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in 
    FahrenheitToCelsius(-500)
  File "<pyshell#8>", line 2, in FahrenheitToCelsius
    assert (ftemp >= -460), "Less than absolute zero!"
AssertionError: Less than absolute zero!

It is important to keep in mind that assert is mostly intended for trapping user-defined constraints and not for catching actual programming errors. Because Python traps programming errors itself, there is usually no need to code asserts to catch things like out-of-bounds indexes, type mismatches, and zero divides. Such asserts are generally unnecessary. Because Python raises exceptions on errors automatically, you can let it do the job for you.

With/As Clauses

Python 2.6 and above introduced a new exception-related statement: the with, and its optional as clause. This statement is designed to work with context manager objects, which support a new method-based protocol. The with/as statement is designed to be an alternative to common try/finally statements. Like try/finally, with/as is intended for specifying termination-time or cleanup activity that must run regardless of whether an exception occurs in a processing step. Unlike try/finally, the with statement supports a richer object-based protocol for specifying both entry and exit actions around a block of code.

The basic format of the with statement looks like this:

	with expression [as variable]:
		with-block

The expression here is assumed to return an object that supports the context management protocol. This object may also return a value that will be assigned to the name variable if the optional as clause is present.

Note that the variable is not necessarily assigned the result of the expression. The result of the expression is the object that supports the context protocol, and the variable may be assigned something else intended to be used inside the statement. The object returned by the expression may then run startup code before the with-block is started, as well as termination code after the block is done, regardless of whether the block raised an exception or not.

Some built-in Python objects have been augmented to support the context management protocol, and so can be used with the with statement. For example, file objects have a context manager that automatically closes the file after the with block regardless of whether an exception is raised:

	with open(r'file.txt') as myfile:
		for line in myfile:
			print(line)

Here, the call to open returns a simple file object that is assigned to the name myfile. We can use myfile with the usual file tools. In this case, the file iterator reads line by line in the for loop.

But this object also supports the context management protocol used by the with statement. After this with statement has run, the context management machinery guarantees that the file object referenced by myfile is automatically closed, even if the for loop raised an exception while processing the file.

Although file objects are automatically closed on garbage collection, it is not always easy to know when that will occur. The with statement in this role is an alternative that allows us to be sure that the close will occur after execution of a specific block of code. We can accomplish a similar effect with the more general and explicit try/finally statement, but it requires four lines of code instead of one:

	myfile = open(r'file.txt')
	try:
		for line in myfile:
			print(line)
	finally:
		myfile.close()

The lock and condition synchronization objects they define may also be used with the with statement, because they support the context management protocol:

	lock = threading.lock()
	with lock:
	# critical code
	...access shared resources here...

Here, the context management machinery guarantees that the lock is automatically acquired before the block is executed and released on the block is complete, regardless of exception outcomes.

External Links:

Assertions in Python at www.tutorialspoint.com

Python Exceptions: Part Five

exceptions

The Raise Statement

One of the statements not yet covered in this series on exceptions is the raise statement. To trigger excepts explicitly in Python, you can code raise statements. Their general form is simple – a raise statement consists of the word raise, optionally followed by the class to be raised or an instance of it:

	raise  # Raise an instance of a class
	raise  # Make and raise instance of a class
	raise  # Re-raise the most recent exception

The first raise form presented here is the most common one. We provide an instance directly, either created before the raise or within the raise statement itself. If we pass a class instead, then Python will call the class with no constructor arguments to create an instance to be raised. This form is equivalent to adding parenthesis after the class reference. The last form re-raises the most recently raised exception. It is commonly used in exception handlers to propagate exceptions that have been caught.

With built-in exceptions, the following two forms are equivalent:

	raise IndexError
	raise IndexError()

Both examples raise an instance of the exception class named, although the first creates the instance implicitly. We can also create the instance ahead of time, because the raise statement accepts any kind of object reference:

	my_exc = IndexError()
	raise my_exc

	my_excs = [IndexError, TypeError]
	raise my_excs[0]

When an exception is raised, Python sends the raised instance along with the exception. If a try includes an except name as X: clause, the variable X will be assigned the instance provided in the raise:

	try:
		...
	except IndexError as X: 
		...

The as is optional in the try handler. If it is omitted, the instance is simply not assigned to to a name. Including it allows the handler to access both data in the instance and methods in the exception class.

This model works the same for user-defined exceptions coded in classes:

	class MyExc(Exception): pass
	...
	raise MyExc('wooble')
	...
	try:
		...
	except MyExc as X:
		print(X.args)

Regardless of how exceptions are named, they are always identified by instance objects, and at most one is active at any given time. Once caught by an except clause anywhere in the program, an exception dies unless it is re-raised by another raise statement or error.

A raise statement that does not include an exception name or extra data value simply re-raises the current exception. This form is typically used if you need to catch and handle an exception, but you don’t want the exception to die in your code. Running a raise this way re-raises the exception and propagates it to a higher handler.

Finally, Python (3.0 and above) also allows an optional from clause:

	raise exception from otherexception

When the from is used, the second expression specifies another exception class or instance to attach to the raised exception’s __cause__ atrribute. If the raised exception is not caught, Python prints both exceptions as part of the standard error message:

>>> try:
... 	1/0
...	except Exception as E:
...	raise TypeError('Not good') from E

When an exception is raised inside an exception handler, a similar procedure is followed implicitly. The previous exception is attached to the new exception’s __context__ attribute and is again displayed in the standard error message if the exception goes uncaught.

External Links:

Handling Exceptions at Python Wiki Built-in Exceptions at docs.python.org

Python Exceptions: Part Four

exceptionsPython Exceptions: try/except/finally

In all versions of Python prior to Release 2.5, there were two types of try statements. You could either use a finally to ensure that cleanup code was always run, or write except blocks to catch and recover from specific exceptions and optionally specify an else clause to be run if no exceptions occurred. In other words, the finally clause could not by mixed with except and else.

That has changed with Python 2.5 and later. Now, the two statements have merged; we can mix finally except and else clauses in the same statement:

try:
	statements
except Exception1:
	handler1
except Exception2:
	handler2
else:
	else_block

The code in this statement’s main-action block is executed first, as usual. If that code raises an exception, all the except blocks are tested, one after another, looking for a match to the exception raised. If the exception raised is Exception1, the handler1 block is executed; if it’s Exception2, handler2 is run, etc. If no exception is raised, the else-block is executed.

No matter what’s happened previously, the finally-block is executed once the main action block is complete and any raised exceptions have been handled. In fact, the code in the finally-block will be run even if there is an error in an exception handler or the else-block and a new exception is raised.

As always, the finally clause does not end the exception. If an exception is active when the finally-block is executed, it continues to be propagated after the finally-block runs, and control jumps somewhere else in the program. If no exception is active when the finally is run, control resumes after the entire try statement.

The effect here is that the finally-block is always run, regardless of whether [1] an exception occurred in the main action and was handled; [2] an exception occurred in the main action and was not handled; no exceptions occurred in the main action, and/or [4] a new exception was triggered in one of the handlers. Again, the finally serves to specify cleanup actions that must always occur on the way out of the try, regardless of what exceptions have been raised or handled.

When try, except, else and finally are combined like this, the order must be like this:

	try -> except -> else -> finally

where the else and finally are optional, and there may be zero or more except blocks, but there must be at least one except if an else appears. The try statement essentially consists of two parts: excepts with an optional else, and/or the finally.

Because of these rules, the else can appear only if there is at least one except, and it is always possible to mix except and finally, regardless of whether an else appears to though the except can omit an exception name to catch everything and run a raise statement to re-raise the current exception. If you violate any of these rules, Python will raise a syntax error exception before your code runs.

Finally, prior to Python 2.5, it is actually possible to combine finally and except clauses in a try by syntactically nesting a try/except in the try block of a try/finally statement. The following has the same effect as the new merged form:

try:
	try:
		main-action
	except Exception1:
		handler1
	except Exception2:
		handler2
	...
	else
		no-error
	finally:
		cleanup

Again, the finally block is always run on the way out, regardless of what happened in the main action and regardless of any exception handlers run in the nested try. Since an else always requires an except, this nested form even sports the same mixing constraints of the unified statement form outlined in the preceding section. But this nested equivalent is more obscure and requires more code than the new merged form. Mixing finally into the same statement makes your code easier to write and read and is thus the generally preferred technique.

External Links:

Handling Exceptions at Python Wiki Built-in Exceptions at docs.python.org

Python Exceptions: Part Three


exceptionsIn the previous articles, we covered the try and except clauses. In this article, we will start out by looking at the else clause.

Python Exceptions: The Else Clause

Although the purpose of the else clause may not be readily apparent, it is necessary because without it, there is no way to tell whether the flow of control has proceeded past a try statement because no exception was raised, or because an exception occurred and was handled – at least not without setting and checking Boolean flags. Much like the way else clauses in loops make the exit cause more apparent, the else clause provides syntax in a try that makes what has happened obvious and unambiguous. For example:

try:
# Run some code
except SomeError:
# Handle the exception
else
# No exception has occurred

You can do something similar by moving the else code into the try block. This can lead, however, to incorrect exception classifications. If the “no exception occurred” action triggers a SomeError, it will register as a failure of the try block and erroneously trigger the exception handler below the try. By using an explicit else clause instead, you will make the logic more obvious and guarantee that except handlers will run only for real failres in the code you are wrapping in a try, not for failures in the else case’s action.

The other variant of the try statement is a specialization that has to do with finalization actions. If a finally clause is included in a try, Python will always run its block of statements “on the way out” of the try statement, whether an exception occured while the try block was running or not. Its general form is:

try:

finally:

With this variant, Python begins by running the statement block associated with the try header line. What happens next depends on whether an exception occurs during the try block. If no exception occurs while the try block is running, Python jumps back to run the finally block and then continues execution past below the try statement. If on the other hand an exception does occur during the try block’s run, Python still comes back and runs the finally block, but it then propagates the exception up to a higher try or the top-level default handler; the program does not resume execution below the try statement. That is, the finally block is run even if an exception is raised, but unlike an except, the finally does not terminate the exception – it continues being raised after the finally block runs.

The try/finally form is useful when you want to be completely sure that an action will happen after some code runs, regardless of the exception behavior of the program. In practice, it allows you to specify cleanup actions that always must occur (such as file closes).

Note that the finally clase cannot be used in the same try statement as except and else in Python 2.4 and earlier, so the try/finally blocks are best thought of as a distinct statement form if you are using an older version of Python. In Python 2.5 and later, however, finally can appear in the same statement as except and else, so today there is essentially a single try statement with many optional clauses. Whichever version you use, though, the finally clause still serves the same purpose: to specify cleanup actions that must always be run, regardless of any exceptions.

External Links:

Handling Exceptions at Python Wiki

Built-in Exceptions at docs.python.org

Python Exceptions: Part Two

exceptionsIn the previous article, we covered some of the basics of exception handling in Python. In this article, we will go into a bit more detail on exception processing, and explore the details behind the try, raise, assert, and with statements.

Python Exceptions: Try, Raise, and Assert

We will start with the try statement, introduced in the previous article. The try statement is a compound statement which starts with a try header line, followed by a block of usually indented statements, then one or more except clauses that identify exceptions to be caught, and an optional else clause at the end. The words try, except and else are associated by indenting them to the same level. For example:

try
	statements
except name1:
	statements
except name2 as something:
	statements
except:
	statements
else:
	statements

Here, the block under the try header represents the main action of the statement. This is the code you are trying to run. The except clauses define handlers for exceptions raised during the try block. The else clause, if coded, provides a handler to be run if no exceptions occur.

When a try statement is entered, Python marks the current program context so it can return to it if an exception occurs. The statements nested under the try header are run first. What happens next is dependent on whether exceptions are raised while the try block’s statements are running. If an exception does occur while the try block’s statements are running, Python jumps back to the try and runs the statements under the first except clause that matches the raised exception. Control resumes below the entire try statement after the except block runs, unless the except block raises another exception. But if an exception happens in the try block and no except clause matches, the exception is propagated up to the last matching try statement that was entered in the program, or (if it’s the first such statement) to the top level of the process. If it is at the top level of the process, Python will stop the program and print a default error message. Finally, if no exception occurs while the statements under the try header run, Python runs the statements under the else line, if there are any, and control then resumes below the entire try statement.

except clauses catch exceptions that occur only within the statements in the associated try block. As such, they are considered focused exception handlers. However, since the try block’s statements can call functions coded elsewhere in a program, the source of an exception may be outside the try statement itself.

There are several clauses you can use after the try header, but you have to use at least one. In addition, you can code else only if there is at least one else. There can be several except clauses, but there can only be one else and one finally. Through Python 2.4, the finally clasue must appear alone, without an else or except. As of Python 2.5, however, a finally can appear in the same statement as except and else.

Because Python looks for a match within a given try by inspecting the except caluses from top to bottom, the parenthesized version has the same effect as listing each exception in its own except clause, but with the benefit of only having to code the statement body once.

The empty except clause is a sort of wildcard feature; it catches everything. Therefore, it allows your handlers to be as general or specific as you like. This may be more convenient than listing all possible exceptions in a try. But empty except clasues raise some design issues. Although they are convenient, they may catch unexpected system calls unrelated to your code. For example, even system exit calls in Python trigger exceptions, and you usually want them to pass. For that reason, Python 3.0 introduced an alternative. Catching an exception named Exception has almost the same effect as an empty except, but ignores exceptions related to system exits. Other system calls, however, may still trigger exceptions.

External Links:

Handling Exceptions at Python Wiki

Built-in Exceptions at docs.python.org

Python Exceptions: Part One

exceptionsExceptions are events that can modify the flow of control through a program; they are triggered automatically on errors, and can be triggered and intercepted by your Python code. Python has the following statements to deal with exceptions:

  • try/except: Catch and recover from exceptions raised by Python (or by you)
  • try/finally: Perform cleanup actions, whether exceptions occur or not.
  • raise: Trigger an exception manually in your code.
  • assert: Conditionally trigger an exception in your code.
  • with/as: Implement context managers in Python 2.6/3.0.

Exceptions let us jump out of large chunks of a program (whatever is included in the “try” block). The try statement works as follows:

  • First, the try clause is executed.
  • If no exception occurs, the except clause is skipped and execution of the try statement is finished.

To provide an example, let’s assume we have a character string:

>>> myStr = ‘Hello, world!’
>>> print(myStr[3])
l

If we reference an index within the bounds of the string, then the code works as it should, and we get no error. But if we specify an index outside of the bounds of the string, we get a different result:

Traceback (most recent call last):
File “<pyshell#3>”, line 1, in
print(myStr[13])
IndexError: string index out of range

Handling Exceptions

In our programs, we can let an error occur, in which case the program stops running, or we can try to handle it. The error generated by trying to access myStr[13] was an IndexError, so we will handle the IndexError exception in our code:

def testException():
	myStr = 'Hello, world!'
	try:
		print(myStr[13])
		print('No errors so far.')
	except IndexError:
		print('Index error.')
	print('Continuing...')

In this code, we have a try/except block. In try, we attempt to print out myStr[13]. If there is no error, we will move on to the next line (and print out ‘No errors no far.’). If an IndexError exception is raised, we will jump immediately to the except statement. The last line (print out ‘Continuing…’) will execute whether or not an exception was raised. Not surprisingly, we get the following output when we run the function:

>>> testException()
Index error.
Continuing…

Note that if the program generated an exception that was not an IndexError, the exception would be unhandled and the program would terminate with an error. However, a try statement may have more than one except clause, to specify handlers for different exceptions. At most one handler will be executed. Handlers only handle exceptions that occur in the corresponding try clause, not in other handlers of the same try statement. An except clause may also name multiple exceptions as a parenthesized tuple. For example:

except (IndexError, ValueError, RuntimeError):
pass

In this case, the last except clause may omit the exception name or names to serve as a wildcard. You can use this to print an error message and re-raise the exception to allow the caller to handle the exception as well. For example, we could rewrite the above function:

def testException():
    myStr = 'Hello, world!'
    try:
        print(myStr[12])
        print('No errors so far.')
        a = int(myStr[12])
    except IndexError:
        print('Index error.')
    except ValueError:
        print('Value error.')
    print('Continuing...')

If we execute this function, we get the following result:

>>> testException()
!
No errors so far.
Value error.
Continuing…

The first print() call in the try block will not raise an exception, but trying to convert myStr[12] to an integer will generate a ValueError exception.

You can also add a finally block to your code. The finally block is inserted after the try block and is executed whether or not an exception is generated (and whether or not said exception is handled). For example, we might have the following function:

def testException():
    myStr = 'Hello, world!'
    try:
        print(myStr[12])
        print('No errors so far.')
        b.append(myStr[12])
    except IndexError:
        print('Index error.')
    except ValueError:
        print('Value error.')
    finally:
        print('Finally block reached.')
    print('Continuing...')

Here, the line with the b.append() call will generate an unhandled exception because b was not previously defined as a list. Thus, it will not reach the last line of the program, but it will execute what is in the finally block:

!
No errors so far.
Finally block reached.
Traceback (most recent call last):
File “N:\Users\David\workspace\exception\exception.py”, line 16, in
testException()
File “N:\Users\David\workspace\exception\exception.py”, line 7, in testException
b.append(myStr[12])
NameError: global name ‘b’ is not defined

Exception handling in Python is not unlike try/catch blocks in C++ and try/catch/finally blocks in Java. It provides a useful alternative to the old C approach, which was to propagate error codes:

int functionCall()
{
	if (doSomething() == ERROR_CODE)
		return ERROR_CODE;
}

void main()
{
	if (functionCall() == ERROR_CODE)
		badResult();
	else
		goodResult();
}

In this case, a lot of our code is devoted to error handling. If we had a try/except block, however, we could just wrap the code we think might cause an error in a try block.

We are not quite done examining exception handling in Python, but hopefully this will serve as a good introduction.

External Links:

Handling Exceptions at Python Wiki

Built-in Exceptions at docs.python.org

matplotlib: Part One

matplotlib

matplotlib is a plotting library for the Python programming language and its numerical mathematics extension NumPy. It provides an object-oritented API for embedding plots into applications using general-purpose GUI toolkits.

Since it is designed to resemble MATLAB, the pylab interface makes matplotlib easy to learn for experienced MATLAB users. Some of the advantages of matplotlib over MATLAB include the following:

  • It is based on Python, a full-featured modern object-oriented programming language suitable for large-scale software development.
  • Free, open source (therefore, there are no license servers)
  • Native SVG support

Unfortunately, matplotlib was designed to work with Python, so it is hard or impossible to use in other languages. Also it heavily relies on packages such as NumPy and SciPy, so there are a number of dependencies.

Installing matplotlib

matplotlib is easy to install in Linux from the repositories. In Debian, Ubuntu, Mint and many other distributions, you just need to type:

sudo apt-get install python-matplotlib

In Fedora, Redhat and CentOS, it’s:

sudo yum install python-matplotlib

Under Windows, in addition to the standard Python installation, you will also need to install compatible versions of setuptools, NumPy, python-dateutil, pytz, pyparsing and six in addition to matplotlib. NumPy depends on files installed with Microsoft Visual C++ 2008 (for Python 2.6 to 3.2) or Microsoft Visual C++ 2010 (for Python 3.3 and 3.4), so you will need to have Visual C++ installed. If not, it may be easier to install one of the scipy-stack compatible Python distributions such as Python(x,y), Enthought Canopy, or Continuum Anaconda, which have matplotlib and many of its dependencies, plus other useful packages, preinstalled.

Installation of Python(x,y) is easy. Download the current version from the official pythonxy download site and run the installer (the installation notes also recommend that you uninstall any other Python distribution before installing it). The installer will present you with a few basic options, after which installation will proceed. Once installation is complete, you can utilize Python(x,y). This package includes a Python interpreter called iPython (you can access it by typing “ipython” at the command line). I chose to use Eclipse instead. Eclipse automatically detected the installation of Python(x,y), and asked if I wanted to use it as the default Python interpreter. I selected this option and proceeded.

We need some code to test matplotlab, so I wrote the following:

'''A short bit of code to test matplotlib
and display a graph'''
import random, pylab

def mpltest():
    dicerolls = []
    for i in range(10):
        x = int(random.random()*6+1)
        y = int(random.random()*6+1)
        dicerolls.append(x+y)
    pylab.plot(dicerolls,'ro')
    pylab.title('Result of dice rolls')
    pylab.xlabel('Trial #')
    pylab.ylabel('Outcome')
    pylab.show()

mpltest()

In mpltest, the first few lines just create a list called dicerolls and use iteration to add data for the dice rolls to the list. The next few lines introduce pylab and some of its functions. pylab.plot will cause the data to be plotted. The second parameter, ‘ro’, will cause only red circles to be shown (“red only”); since each dice roll is independent of the others, we don’t want a line to be drawn connecting the points. pylab.title allows the graph to have a title, and pylab.xlabel and pylab.ylabel creates labels for the x and y-axis, respectively. Nothing actually appears on the screen, though, until we call pylab.show, which we do on the last line of the function call.

It would be nice to graph a continuous function as well, so let’s graph the growth of $10,000 if we have 7 percent interest, compounded yearly:

def interest():
    principal = 10000.0
    interestRate = 0.07
    years = 10
    iValues = []
    for i in range(years+1):
        principal += principal*interestRate
        iValues.append(principal)
    pylab.plot(iValues)
    pylab.title('7% Growth, Compounded Annually')
    pylab.xlabel('Years of Compounding')
    pylab.ylabel('Value of Principal ($)')
    pylab.show()
    
interest()

The format of the function is similar to the first one, although we have replaced the dice rolls with the growth of principal over time. In pylab.plot, we want to have a line connecting the points, so the second parameter is omitted.

We can see the results in the screen capture on the side of this article. matplotlib does seem to work as advertised. We will post more articles about matplotlib in the future, but hopefully this will serve as a useful introduction.

External Links:

matplotlib at Wikipedia

The official matplotlib site

Pyplot tutorial at matplotlib.org

Python (x,y) download site

Python Sets: Part Two

Python setsIn part one of this series, we took a first look at sets, a relatively new core data type in Python. In this article, we will continue cover set methods, as well as a new literal format for sets.

In addition to expressions, the set object provides methods that correspond to the set operations. For example, the set add method inserts one item; update provides an in-place union, and remove deletes an item by value. Run a dir call on any set instance or the set type name to see all the available methods. Assuming we have mySet and otherSet from the previous article:

>>> mySet = set(‘hello’)
>>> otherSet = set([‘a’,’b’,’c’,’d’,’e’])

then we can perform these operations:

>>> si = mySet.intersection(otherSet)
>>> si
{‘e’}
si.add(‘MORE’)
si
{‘MORE’, ‘e’}
>>> si.update(set([‘X’,’Y’]))
>>> si
{‘X’, ‘MORE’, ‘e’, ‘Y’}
>>> si.remove(‘e’)
si
{‘X’, ‘MORE’, ‘Y’ }

As iterable containers, sets can also be used in operations such as len, for loops, and list comprehensions. Because they are unordered, however, they do not support sequence operations like indexing and slicing.

>>> for item in set('aeiou'):
        print(item * 4)

uuuu
iiii
oooo
aaaa
eeee

Although these set expressions generally require two sets, they often work with any iterable types, such as lists, tuples, and ranges. For example:

mySet.union([‘a’,’b’,’c’])
{‘b’, ‘l’, ‘c’, ‘o’, ‘a’, ‘e’, ‘h’}
mySet.intersection((‘e’,’l’,’p’))
{‘e’, ‘l’}

Using Curly Braces

Python 3.0 adds a new set literal form, using the curly braces formerly reserved for dictionaries. In 3.0 and above, the following are equivalent:

set([‘a’,’b’,’c’])
{‘a’, ‘b’, ‘c’}

Because sets are unordered, unique, and immutable, a set’s items behave much like a dictionary’s keys, and therefore the new syntax makes sense. Regardless of how a set is made, 3.0 and above displays sets using the new literal format. The set built-in is still required in 3.0 to create empty sets and to build sets from existing iterable objects, but the new literal format is convenient for initializing sets of known structure:

>>> {‘a’,’b’,’c’}
{‘c’, ‘b’, ‘a’}

>>> S = {‘a’,’e’,’i’,’o’,’u’}
>>> S.add(‘MORE’)
>>> S
{‘MORE’, ‘i’, ‘o’, ‘a’, ‘e’, ‘u’}

All the set processing operations discussed in the previous article work the same in 3.0 and above, but the result sets print differently:

>>> S1 = {‘a’,’b’,’c’}
>>> S1 & {‘a’,’c’}
{‘c’,’a’}
>>> {‘a’,’e’,’i’,’o’,’u’} | S1
{‘b’, ‘i’, ‘o’, ‘c’, ‘a’, ‘e’, ‘u’}
>>> S1 – {‘a’,’b’}
{‘c’}
>>> S1 > {‘a’,’c’}
True

Note that {} is still a dictionary in Python. Empty sets must be created with the set built-in, and print the same way:

>>> S1 – {‘a’,’b’,’c’}
set()
>>> type({})
<class ‘dict’>

>>> mySet = set()
>>> mySet.add(‘a’)
>>> mySet
{‘a’}

As in Python 2.6, sets created with 3.0 literals support the same methods, some of which allow general iterable operands that expressions do not:

>>> {‘a’,’b’,’c’} | {‘c’,’d’}
{‘c’, ‘b’, ‘a’, ‘d’}
>>> {‘a’,’b’,’c’} | [‘c’,’d’]
Traceback (most recent call last):
File “<pyshell#34>”, line 1, in
{‘a’,’b’,’c’} | [‘c’,’d’]
TypeError: unsupported operand type(s) for |: ‘set’ and ‘list’

>>> {‘a’,’b’,’c’}.union([‘c’,’d’])
{‘c’, ‘b’, ‘a’, ‘d’}
>>> {‘a’,’b’,’c’}.union([‘c’,’d’])
{‘c’, ‘b’, ‘a’, ‘d’}
>>> {‘a’,’b’,’c’}.union(set([‘c’,’d’]))
{‘c’, ‘b’, ‘a’, ‘d’}
>>> {‘a’,’b’,’c’}.intersection((‘c’,’e’,’f’))
{‘c’}
>>> {2,3,4}.issubset(range(-5,5))
True
>>>

External Links:

Set, frozenset at docs.python.org – Official documentation on sets and frozensets for Python 3.4

Python Sets: Part One

Python sets

No, not that kind of set.

One of the core Python data types that we did not mention in earlier articles that perhaps deserves some attention is the set. Sets are a recent addition to Python; they are neither mappings (dictionaries) nor sequences (strings, lists and typles). Sets are created by calling the built-in set function or using new set literals and expressions in 3.0, and they support the usual mathematical set operations.

Introduction to Sets

Creating a set can be done two different ways:

>>> mySet = set(‘hello’)
>>> otherSet = {‘a’,’b’,’c’,’d’,’e’}

len returns the number of unique set items, so we get:
>>> len(otherSet)
5
But if we run the same operation on mySet, we get:
>>> len(mySet)
4

>>> ‘h’ in mySet
True
>>> ‘i’ in mySet
False
>>> mySet.isdisjoint(otherSet)
False
>>> newSet = set(‘he’)
newSet.issubset(mySet)
True

Mathematical set operations are generally valid on sets:

>>> mySet, otherSet
({‘o’, ‘e’, ‘l’, ‘h’}, {‘e’, ‘d’, ‘b’, ‘a’, ‘c’})

>>> mySet & otherSet
{‘e’}

>>> mySet | otherSet
{‘e’, ‘h’, ‘o’, ‘d’, ‘b’, ‘c’, ‘a’, ‘l’}
>>> mySet – otherSet
{‘o’, ‘l’, ‘h’}

There are a few other operations. <= tests whether ever element in the left operand set is in the right operand set. For example: >>> newSet <= mySet
True

But what if we want to return False if the sets are equal? Then we use <:
>> newSet < mySet

True

>>> exactcopy = set(mySet)
>>> exactcopy < mySet

False

>>> exactcopy <= mySet

True

We can flip the operand around, and check to see if the left operand set is a superset of the right operand set:

>>> mySet > newSet
True
>>> mySet >= newSet
True

Sets are mutable; you can add and remove items with the add and remove methods:

>>> otherSet.add(‘f’)
{‘e’, ‘f’, ‘d’, ‘b’, ‘c’, ‘a’}
>>> otherSet.remove(‘c’)
>>> otherSet
{‘e’, ‘f’, ‘d’, ‘b’, ‘a’}

You can also iterate over a set. For example:
>>> for s in mySet:
print(s)

o
e
l
h

Since sets are mutable, there are some things we cannot do with them: for example, we cannot use them as dictionary keys. But objects of type frozenset are immutable. Therefore:

>>> unchangeableSet = frozenset(‘abc’)

Although we can’t add and remove items, we can perform the usual set operations on frozensets, or a combination of sets and frozensets:

>>> mySet | unchangeableSet
{‘o’, ‘e’, ‘b’, ‘h’, ‘c’, ‘a’, ‘l’}

For an example of using frozenset to create keys for a dictionary, here’s a sample:

>>> keySet = frozenset(‘abc’)
>>> names = [ ‘Able’, ‘Baker’, ‘Charlie’ ]
>>> myDict = { }
>>> i = 0
>>> for s in keySet:
myDict[s] = names[i]
i += 1

>>> print(myDict)
{‘a’: ‘Able’, ‘b’: ‘Baker’, ‘c’: ‘Charlie’}

Here, we created an immutable set called keySet, and a list of items to put in our dictionary. We iterate through the set, mapping items in the list to keys in keySet. When we print out the results, we see that each item was successfully mapped to a key.

In the next article, we will continue our look at sets.

External Links:

Set, frozenset at docs.python.org – Official documentation on sets and frozensets for Python 3.4