Python Strings: Part Four

Python stringsIn the previous articles, we introduced Python strings and covered escape sequences, raw strings and triple-quoted strings. Now we can cover some basic string operations. Strings can be concatenated using the + operator and repeated using the * operator:

>>> len('string')
>>> 'str' + 'ing'
>>> 'repeat' * 4

Formally, adding two string objects creates a new string object, with the contents of its operands joined. Repetition is like adding a string to itself a number of times. In both cases, Python lets you create arbitrarily-sized strings. There is no need to pre-declare anything in Python, including the sizes of data structures such as strings. The len built-in function returns the length of a string, or any object with a length.

Repetition comes in handy in a number of contexts. For example, if you want to print out 80 asterisks, just do this:

>>> print('*' * 80)

Notice that we are using the same + and * operators that perform addition and multiplication when using numbers, so we are using operator overloading. Python does the correct operation because it knows the types of the objects being added and multiplied. But there’s a limit to what you can do with operator overloading in Python. For example, Python does not allow you to mix numbers and strings in + expressions. ‘repeat’ + 3 will raise an error instead of automatically converting 3 to a string.

You can also iterate over strings in loops using for statements and test membership for both characters and substrings with the in expression operator, which is essentially a search. For substrings, in is much like the str.find() method, but it returns a Boolean result instead of the substring’s position. For example:

>>> mystr = "repeat"
>>> for c in mystr:
	print(c, ' ')

r e p e a t
>>> "p" in mystr
>>> "y" in mystr
>>> 'straw' in 'strawberry'

The for loop assigns a variable to successive items in a sequence and executes one or more statements for each item. In effect, the variable c becomes a cursor stepping across the string here.

External Links:

Strings at

Python Strings at Google for Developers

Python strings tutorial at

Python Exceptions: Part Five


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:

	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')
	except MyExc as X:

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

matplotlib: Part Two


In the previous article, I introduced matplotlib and some basic pylab statements that can be used to produce graphs. In this article, we’ll use matplotlib and some basic computation to understand experimental data.

numpy Arrays

One of the examples used previously was using pylab to plot a graph of compound interest. matplotlib would be fairly useless, however if it was only used to graph data for such functions, and especially if the plot function only accepted list input. One alternate way of providing input to the plot method is to use numpy.arange():

import random, pylab
import numpy

t = numpy.arange(0,10,.25)

In this code fragment, we use numpy.arange, and provide as input three arguments: the minimum, the maximum, and the interval. We supply to plot() three arguments: t, t (again), and ‘bo’ for blue circles. The result is a linear graph where y = x, and the circles appear at intervals of .25.

You can perform some mathematical operations on numpy arrays. For example:


multiplies the array by a factor of 2, so we get a graph where y = 2*x. We could also square or cube a numpy array, like so:


Since having two functions represented by blue dots might be confusing, you probably want to use something else:


The plot() method can also accept as input the return value of a function. For example, assume that we have a function to return the cosine of a number:

def func(t):
return numpy.cos(2*numpy.pi*t)

Then we can plot the function with pylab:


Invoking pylab.figure() is optional because figure(1) will be created by default. The pylab.subplot() method specifies numrows, numcols, and fignum where fignum ranges from 1 to numrows*numcols. The comma in the subplot command are optional if numrows*numcols < 10. Thus, subplot(211) is identical to subplot(2,1,1). If you want to place axes manually (not on a rectangular grid, use the pylab.axes() method, which allows you to specicy a location as axes([left, bottom, with, height]) where all values are in fractional (0 to 1) coordinates.

Another useful method is pylab.savefig, which allows us to save the graph. For example:


allows us to save the graph as cos.png, with a resolution of 200 dots per inch (dpi). You can also save the graph by clicking on the disk icon on the menu at the top of the graph when the graph displays. Allowed formats include PNG, JPEG, TIFF, SVG and PDF.

External Links:

matplotlib at Wikipedia

The official matplotlib site

Pyplot tutorial at

Python (x,y) download site

Python Programming: Part One

Python programmingPython, as mentioned earlier, is an interpreted language. An interpreter is a program that executes other programs, and when you are doing Python programming, the Python interpreter reads your program and carries out the instructions it contains.

In its simplest form, a python program is just a text file containing Python statements. For example, the initial “Hello, world” program from the previous article is a very simple Python script, but it passes for a fully functional Python program:

print(‘Hello, world!’)

This program contains a single Python print statement, which simply prints a string to the output stream, and thus is a very simple example of Python programming. We can also output the results of numeric expressions if we want:


This will print an “8” to the output stream.

Python Programming: Running Python Programs

Perhaps the easiest way to begin Python programming is to type in programs at Python’s interactive command line. There are many ways to do this: start it in an IDE, from a system console, and so on. Assuming that Python is installed on your system (we covered installation in a previous article), the simplest and most platform-neutral way to start the Python interpreter is usually just to type python at your operating system’s prompt, without any other arguments. The notion of a system shell prompt is cross-platform, but how you access it varies by platform:

  • In Windows, you can type python in a DOS console window (the Command Prompt), found in the Accessories section of the Start -> Programs menu, by right mouse-clicking on the window icon on the lower left corner of the screen in Windows 8, or in the Start -> Run dialog box (run “cmd.exe”).
  • On Unix/Linux/Mac OS X, you might type this command in a shell or terminal window.
  • On handheld devices, you generally click the Python icon in the home or application window to launch an interactive session.

If you have not set your shell’s PATH environment variable to include Python’s install directory, you may need to type the full path to the Python executable on your machine (e.g. c:\Python34\python in Windows for version 3.4). The installer should update the path, however, and I have not had to type the full path under Windows or Linux. Of course, you can always use the change directory command to go to the Python install directory.

In Windows, you can also begin interactive sessions for Python programming by starting IDLE’s main window or by selecting the “Python (command line)” menu option from the Start button menu (in Windows 7 and earlier). I made a desktop shortcut for Python in Windows 8. Both the command line Python interpreter and IDLE spawn a Python interactive prompt with equivalent functionality.

When coding at the Python interactive prompt, we can type as many Python commands as we want, and each command will be run immediately after it is entered:
>>> print(‘Hello, world!’)
Hello, world!
>>> a = 3+4
>>> print(a)
The first command prints: Hello, world!. The second command assigns the sum of 3+4 to the variable a, and the third command prints out the value of a. Note that because the interactive session automatically prints the results of expressions you type, so you do not even need to say “print” at the prompt:
>>> a

We did not do much with the code in this article, but hopefully it served as an adequate introduction to Python programming, and the main point we want to remember here is that the interpreter executes the code entered on each line immediately, when the [ENTER] key is pressed. There is no need to create a source code file, and no need to run the code through a compiler and linker first, as you would do with a language such as C or C++. As we will see in future articles, you can also run multi-line statements at the interactive prompt. Such statements run immediately after you’ve enter all of its lines and pressed [ENTER] twice to add a blank line.

External Links:

The official Python site – contains many useful resources for Python programming.

How to Install Python

Install Python

Running the Python command line.

If you want to run a Python script and you don’t have Python on your system already, fear not. There are a number of options available to install Python, covering a wide variety of different platforms.

How to Install Python: From

Probably the easiest way to install Python is to go to the official Python site’s download page ( Select the release version you want to download, and select the appropriate operating system. The source is also available as a gzipped tarball. If you are running Linux, you may already have Python installed. Ubuntu, for example, includes Python by default. If not, you should be able to install Python from the repositories by typing:

sudo apt-get install python

When prompted, type the superuser password, and installation will begin.

Install Python

IDLE, the official Python GUI.

Once Python is installed, you should be able to invoke the Python interpreter at the command line by typing “python“. Alternatively, you can create a shortcut for Python on your desktop and click on the icon. Clicking on “pythonw” should start IDLE, the Python GUI. You may, however, have problems running IDLE on certain versions of Windows. I myself could not start pythonw under Windows 8.1. If you have the same problem, you might consider using an alternative method for starting IDLE:

  1. Start the Python command line. You can do this [1] by launching a command prompt and typing “python“; [2] by double-clicking “python.exe” in the File Explorer; [3] by right-mouse clicking on the Windows logo in the lower left corner of the desktop, selecting “Run” and launching python.exe from “Run“, or [4] creating a desktop icon for Python and clicking on it.
  2. In the Python interpreter, type “import idlelib.idle” and press <ENTER>. IDLE should now launch.

Whether you are using the basic Python command interpreter or IDLE, you can start typing in Python code. Typing and executing the Python Hello World program is almost trivial:

>>> print(‘Hello, world!’)
Hello, world!

As you can see, the Python interpreter runs the program without the need to compile the code, one of the advantages of using Python. Using IDLE provides the following advantages over the standard Python shell:

  • The ability to save and load Python source code files
  • The ability to cut, copy and paste text, and search
  • The ability to debug programs, using IDLE’s debugger and stack viewer

How to Install Python: The Eclipse IDE

Install Python

Using the PyDev plugin in the Eclipse IDE to write and run a Python script.

Many users will likely find the stock Python installation more than adequate. For those looking for a more elegant solution, however, I suggest the Eclipse IDE. Eclipse is an integrated development environment originally developed by IBM Canada, and currently being developed by the Eclipse Foundation. It contains a base workspace and an extensible plug-in system for customizing the environment. The base system is only useful for Java development, but with plug-ins, you can extend Eclipse to develop applications in other programming languages, including C/C++, COBOL, JavaScript, Perl, PHP – and Python.

The official Eclipse website can be found at; here you can download the latest version. As of this writing, the current version is 4.4, and there are versions for Windows (32 and 64-bit), Linux and Mac OS X. Under Linux, you can also install Eclipse from the repositories by typing:

sudo apt-get install eclipse

and typing the superuser password when prompted.

Once Eclipse is installed, you still need to install the Python plugin. I use the PyDev Python IDE for Eclipse, which you can find at: Some of the features of PyDev include:

  • Python, Jython and IronPython support
  • Django integration
  • Code completion
  • A debugger
  • Code analysis

To install PyDev, launch Eclipse, and from the top menu, navigate to Help -> Install New Software to launch the Install New Software dialog box. In the “Work with: ” edit box at the top, type:

PyDev and PyDev Extensions –

and press the “Add” button. “PyDev” and “PyDev Mylyn Integration (optional)” should appear in the list box. Check the software packages you want to install (PyDev is required if you want to use PyDev to use Python with Eclipse; Mylyn is an optional application lifecycle management framework that serves as a task management tool for Eclipse). When you have made your selection, press the “Next” button; it should take a minute for Eclipse to calculate file sizes and dependencies. Review the information on the next page and press “Next” again. On the subsequent page, click on the radio button to agree to the software license terms and press “Finish“. PyDev should now be installed.

Now, you should be able to start a Python project in Eclipse by navigating to File -> New -> PyDev Project. You need to give your project a name, and select a grammar version (default is 2.7), but it should be easy to create a project. Once you have created a project, you can right-mouse click on the project, and select New -> File to create a new Python source code file that will be added to the project.

Although I covered how to install Python using the stock Python installation as well as Eclipse in this article, I barely scratched the surface in terms of what is available for Python development. I prefer Eclipse, but it may behoove you to do your own research and find the tools which best fit your needs.

External Links:

The official Python site

The official Eclipse site

Are Python Scripts Programs?

Python scriptWe have often heard Python referred to as a scripting language; in fact, often the terms “Python program” and “Python script” used interchangeably. So is Python a true scripting language?

Python Script: What a Scripting Language Is

In order to answer this question, we must first determine what a scripting language is. A scripting language is a language that incorporates support for scripts, and by scripts we mean programs written for a run-time environment (as opposed to programs that are compiled) and that automate the execution of tasks that could otherwise be executed one-by-one by a human operator. Examples of environments that incorporate support for scripting include software applications (programs like Excel and mIRC come to mind), web pages within a web browser, the shells of operating systems, and embedded systems. A scripting language can be viewed as a domain-specific language for a specific environment. Scripting languages can also be viewed as very high-level programming language (as opposed to a low-level programming language such as Assembly), as they operate at a high level of abstraction, or as control languages, particularly for job control languages on mainframes. Thus, we have at least two somewhat distinct ideas of what a scripting language is: [1] a run-time language that only works in a specific environment, such as a software application, or [2] a “glue” or control layer used to control and direct other application components. These categories are not mutually exclusive, but hopefully, the distinction will serve its purpose. To cover the first case, Python programs can indeed serve the role of a domain-specific language, and therefore such a program could be called a Python script. Python programs can be launched from console command lines and perform such tasks as processing text files and launching other programs, which is what we would expect a shell script to be able to do. But this is just one of dozens of common Python application domains, and Python is more than just a shell-script language. To cover the second case, Python programs are indeed often used as the control layer. To test hardware devices, Python programs may call out to components that give low-level access to a device; thus, changes made to the low-level drivers need not affect the control layer. In addition, programs may run Python code at certain points to support end-user product customization. This way, software can be tailored to a customer’s needs without having to recompile and ship the entire system’s source code. However, this is just a common Python role and a subset of what Python can do. Although we could call such code a Python script, Python is more than just a control language. Another sense in which we think of a scripting language is as a simple language for quickly coding tasks. In this sense, perhaps we can think of Python as a scripting language and a program as a Python script. It allows much faster program development than compiled languages like C/C++. But again, Python is not just for simple tasks, and programs can scale up in sophistication as required. That brings us back to the question posed in the beginning of this article: is Python a scripting language? Clearly it can be used as a scripting language, and it supports a rapid and flexible mode of development, but it is much more than just a scripting language. In future articles, I will explore some of the basics of writing a Python script, while also unleashing some of the more powerful functionality of this programming language.

External Links:

Python documentation at