Excerpt for Learning to Program Using Python by Cody Jackson, available in its entirety at Smashwords


Learning to Program Using Python


Cody Jackson


Published by Cody Jackson at Smashwords


Copyright © 2009-2011 Cody Jackson.

This work is licensed under the Creative Commons Attribution-ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California, 94041, USA.


The source code within this text is licensed under the GNU General Public License (GPL). The following license information covers all code contained herein, except those that explicitly state otherwise:


Copyright © 2006-2011 Cody Jackson. This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.


More information about this book, as well as source code files and to contact the author, can be found online at http://python-ebook.blogspot.com.


For those curious, this book was initially drafted on a Mac using Scrivener (http://www.literatureandlatte.com/). It was imported to LYX (http://www.lyx.org) for editing, typesetting, page layout, and other “book writing” tasks. LATEX (http://www.latex-project.org/) was used to create the PDF and HTML versions of this book.


Contents

1 Introduction

2 How is Python Different?

3 Comparing Programming Languages

4 The Python Interpreter

5 Types and Operators

6 Strings

7 Lists

8 Dictionaries

9 Tuples

10 Files

11 Statements

12 Documenting Your Code

13 Making a Program

14 Exceptions

15 Object Oriented Programming

16 More OOP

17 Databases

18 Distributing Your Program

19 Python 3

20 Graphical User Interfaces

21 A Simple Graphical Dice Roller

22 What Can wxPython Do?

A String Methods

B List Methods

C Dictionary operations

D Operators

E Sample programs

F GNU General Public License


Part I

The Core Language

Chapter 1

Introduction

I originally wanted to learn Python because I wanted to make a computer game. I had taken several programming classes in college (C, C++, and Java) but nothing really serious. I’m not a Computer Science major and I don’t program on a professional level.


I didn’t really like the low-level work involved with C/C++. Things like pointers, memory management, and other concepts were difficult for me to grasp, much less effectively use. Java, as my first programming class in school, didn’t make any sense. I had never used an object-oriented language before and object-oriented programming (OOP) concepts gave me fits. It probably didn’t help that my Java class wasn’t actually real Java; it was actually Microsoft’s “custom” version: J++. So not only was I learning a language that had little practical use (J++ added and cut many features found in real Java), but the programs didn’t work correctly. Ultimately the class was canceled near the end of the semester and everyone received full credit.


These problems, and issues learning other programming languages, left a bad taste in my mouth for programming. I never thought I learned a language well enough to feel comfortable using it, much less actually enjoy programming. But then I heard about Python on a computer forum, and noticed several other mentions of the language at other sites around the Internet. People were talking about how great the language was for personal projects and how versatile it is. I decided to give programming one more try and see if Python was the language for me.


To give me more incentive to learn the language, I decided to recreate a role playing game from my childhood as a computer game. Not only would I have a reason to learn the language but, hopefully, I would have something useful that I could give to others for their enjoyment.


1.1 Why Python?

Python is regarded as being a great hobbyist language, yet it is also an extremely powerful language. It has bindings for C/C++ and Java so it can be used to tie large projects together or for rapid prototyping. It has a built-in GUI (graphical user interface) library via Tkinter, which lets the programmer make simple graphical interfaces with little effort. However, other, more powerful and complete GUI builders are available, such as Qt and GTK+. IronPython, a Python version for Windows using the .NET framework, is also available for those using Microsoft’s Visual Studio products. Python can also be used in a real-time interpreter for testing code snippets before adding them into a normal “executable".


Python is classified as a scripting language. Generally speaking, this just means that it’s not compiled to create the machine-readable code and that the code is “tied-into” another program as a control routine. Compiled languages, such as C++, require the programmer to run the source code through a compiler before the software is can be used by a computer. Depending on the program’s size, the compilation process can take minutes to hours.


Using Python as a control routine means Python can act as a “glue” between different programs. For example, Python is often used as the scripting language for video games; while the heavy-duty work is performed by pre-compiled modules, Python can act in a call/response fashion, such as taking controller input and passing it to the appropriate module.


Python is also considered a high-level language, meaning it takes care of a lot of the grunt work involved in programming. For example, Python has a built-in garbage collector so you, as a programmer, don’t really need to worry about memory management and memory leaks, a common occurrence when using older languages such as C.


The main emphasis of Python is readable code and enhancing programmer productivity. This is accomplished by enforcing a strict way of structuring the code to ensure the reader can follow the logic flow and by having an “everything’s included” mentality; the programmer doesn’t have to worry about including a lot of different libraries or other source code to make his program work.


One of the main arguments against Python is the use of whitespace. As shown in Chapter 3, many other languages require the programmer to use brackets, typically curly braces, i.e. “{}”, to identify different blocks of code. With Python, these code blocks are identified by different amounts of indentation.

People who have spent a lot of time with “traditional” languages feel the lack of brackets is a bad thing, while others prefer the white space. Ultimately, it comes down to personal preference. I happen to like the lack of brackets because it’s one less thing I have to troubleshoot when there is a coding problem. Imagine that one missing bracket in several dozen to hundreds lines of code is the reason your program won’t work. Now imagine having to go through your code line by line to find the missing bracket. (Yes, programming environments can help but it’s still one extra thing to consider).


1.2 Why Another Tutorial?

Even though there are several great tutorials at the Python web site (http://www.python.org), in addition to many books, my emphasis will be on the practical features of the language, i.e. I won’t go into the history of the language or the esoteric ways it can be used. Though it will help if you have programmed before, or at least can understand programming logic and program flow, I will try to make sure that things start out slow so you don’t get confused.


The main purpose of this book is to teach people how to program; Python just happens to be the language I have chosen to use. As mentioned above, it is a very friendly language which lets you learn how to program without getting in your way. Most people, when they decide to learn programming, want to jump into C, C++, or Java. However, these languages have little “gotchas” that can make learning difficult and dissuade people from continuing with programming. My goal is to present programming in a fun, friendly manner so you will have the desire to learn more.


1.3 Getting Python

As of this revision, Python 3.x has been out for several years. Most of my experience is with Python 2.4 and 2.5, though much of my knowledge was gained from reading books written for version 2.2. As you can see, it doesn’t necessarily mean your knowledge is obsolete when a new version comes out. Usually the newer versions simply add new features, often features that a beginner won’t have a need for.


Python 3.x breaks compatibility with programs written in 2.x versions. However, much of the knowledge you gain from learning a 2.x version will still carry over. It just means you have to be aware of the changes to the language when you start using version 3.0. Plus, the install base of 2.x is quite large and won’t be going away for quite some time. Due to the fact that most Linux distributions (and Mac OS X) still have older Python versions installed by default (some as old as v2.4), many of the code examples in this book are written for Python 2.x. Special note is made of significant changes between 2.x and 3.x in the chapter about 19, but learning either version won’t harm you.


You can download Python from the Python web site (for Windows) or it may already be installed on your system if you’re using a Mac, Linux, or *BSD. However, the Unix-like operating systems, including OS X, may not have the latest version so you may wish to upgrade, at least to version 2.6. Version 2.6 is a modification of 2.5 that allows use of both 2.x code and certain 3.0 functions. Essentially it lets you code in “legacy” style while still being able to use the latest features as desired, and testing which legacy features will be broken when moving to 3.0.


For those interested in using Python via a USB thumbdrive, you may be interested in Portable Python. This is a self-contained Python environment that you can either run from the thumbdrive or install to your computer. This is useful for people who can’t or don’t want to install Python but would still like to use it.


I’ll assume you can figure out how to get the interactive interpreter running; if you need help, read the help pages on the web site. Generally speaking though, you open up a command prompt (or terminal) and type “python” at the prompt. This will open a Python session, allowing you to work with the Python interpreter in an interactive manner. In Windows, typically you just go to the Python file in All Programs and click it.


1.4 Conventions Used in this Book

The latest version of Python is 3.2 while the most current “legacy” version is 2.7. I will use the term 3.x to signify anything in the Python 3 family and 2.x for anything in the Python 2 family, unless explicitly stated.

The term “*nix” is used to refer to any Unix-like language, including Linux and the various flavors of BSD (FreeBSD, OpenBSD, NetBSD). Though Mac OS X is built upon FreeBSD, it is different enough to not be lumped in the *nix label.


Due to the word-wrap formatting for this book, some lines are automatically indented when they are really on the same line. It may make some of the examples confusing, especially because Python uses “white space” like tabs and spaces as significant areas. Thus, a word- wrapped line may appear to be an indented line when it really isn’t. Hopefully you will be able to figure out if a line is intentionally tabbed over or simply wrapped.


Some of the words in the code sections are bolded. These are key words in the Python language; don’t use these words for variable names as it can cause confusion when the program tries to run. If the Python interpreter finds an “overwritten” key word, it will think you are using it in the expected, default manner and cause an error.


Finally all of the major programs in this book can be found at this book’s website: http://python-ebook.blogspot.com.


Chapter 2

How is Python Different?

So what is Python? Chances you are asking yourself this. You may have found this book because you want to learn to program but don’t know anything about programming languages. Or you may have heard of programming languages like C, C++, C#, or Java and want to know what Python is and how it compares to “big name” languages. Hopefully I can explain it for you.


2.1 Python Concepts

If you’re not interested in the hows and whys of Python, feel free to skip to the next chapter. In this chapter I will try to explain to the reader why I think Python is one of the best languages available and why it’s a great one to start programming with.


2.1.1 Dynamic vs. Static Types

Python is a dynamic-typed language. Many other languages are static typed, such as C/C++ and Java. A static typed language requires the programmer to explicitly tell the computer what type of “thing” each data value is. For example, in C if you had a variable that was to contain the price of something, you would have to declare the variable as a “float” type. This tells the compiler that the only data that can be

used for that variable must be a floating point number, i.e. a number with a decimal point. If any other data value was assigned to that variable, the compiler would give an error when trying to compile the program.


Python, however, doesn’t require this. You simply give your variables names and assign values to them. The interpreter takes care of keeping track of what kinds of objects your program is using. This also means that you can change the size of the values as you develop the program. Say you have another decimal number (a.k.a. a floating point number) you need in your program. With a static typed language, you have to decide the memory size the variable can take when you first initialize that variable. A double is a floating point value that can handle a much larger number than a normal float (the actual memory sizes depend on the operating environment). If you declare a variable to be a float but later on assign a value that is too big to it, your program will fail; you will have to go back and change that variable to be a double.


With Python, it doesn’t matter. You simply give it whatever number you want and Python will take care of manipulating it as needed. It even works for derived values. For example, say you are dividing two numbers. One is a floating point number and one is an integer. Python realizes that it’s more accurate to keep track of decimals so it automatically calculates the result as a floating point number. Here’s what it would look like in the Python interpreter.

>>>6.0 / 2

3.0

>>>6 / 2.0

3.0


As you can see, it doesn’t matter which value is on top or bottom; Python “sees” that a float is being used and gives the output as a decimal value.


2.1.2 Interpreted vs. Compiled

Many “traditional” languages are compiled, meaning the source code the developer writes is converted into machine language by the compiler. Compiled languages are usually used for low-level programming

(such as device drivers and other hardware interaction) and faster processing, e.g. video games.

Because the language is pre-converted to machine code, it can be processed by the computer much quicker because the compiler has already checked the code for errors and other issues that can cause the program to fail. The compiler won’t catch all errors but it does help. The caveat to using a compiler is that compiling can be a time consuming task; the actual compiling time can take several minutes to hours to complete depending on the program. If errors are found, the developer has to find and fix them then rerun the compiler; this cycle continues until the program works correctly.


Python is considered an interpreted language. It doesn’t have a compiler; the interpreter processes the code line by line and creates a bytecode. Bytecode is an in-between “language” that isn’t quite machine code but it isn’t the source code. Because of this in-between state, bytecode is more transferable between operating systems than machine code; this helps Python be cross-platform. Java is another language that uses bytecodes.


However, because Python uses an interpreter rather than compiler, the code processing can be slower. The bytecode still has to be “deciphered” for use by the processor, which takes additional time. But the benefit to this is that the programmer can immediately see the results of his code. He doesn’t have to wait for the compiler to decide if there is a syntax error somewhere that causes the program to crash.


2.1.3 Prototyping

Because of interpretation, Python and similar languages are used for rapid application development and program prototyping. For example, a simple program can be created in just a few hours and shown to a customer in the same visit.


Programmers can repeatedly modify the program and see the results quickly. This allows them to try different ideas and see which one is best without investing a lot of time on dead-ends. This also applies to creating graphical user interfaces (GUIs). Simple “sketches” can be laid out in minutes because Python not only has several different GUI libraries available but also includes a simple library (Tkinter) by default.

Another benefit of not having a compiler is that errors are immediately generated by the Python interpreter. Depending on the developing environment, it will automatically read through your code as you develop it and notify you of syntax errors. Logic errors won’t be pointed out but a simple mouse click will launch the program and show you final product. If something isn’t right, you can simply make a change and click the launch button again.


2.1.4 Procedural vs. Object-Oriented Programming

Python is somewhat unique in that you have two choices when developing your programs: procedural programming or object-oriented. As a matter of fact, you can mix the two in the same program.

Briefly, procedural programming is a step-by-step process of developing the program in a somewhat linear fashion. Functions (sometimes called subroutines) are called by the program at times to perform some processing, then control is returned back to the main program. C and BASIC are procedural languages.


Object-oriented programming (OOP) is just that: programming with objects. Objects are created by distinct units of programming logic; variables and methods (an OOP term for functions) are combined into objects that do a particular thing. For example, you could model a robot and each body part would be a separate object, capable of doing different things but still part of the overall object. OOP is also heavily used in GUI development.


Personally, I feel procedural programming is easier to learn, especially at first. The thought process is mostly straightforward and essentially linear. I never understood OOP until I started learning Python; it can be a difficult thing to wrap your head around, especially when you are still figuring out how to get your program to work in the first place.


Procedural programming and OOP will be discussed in more depth later in the book. Each will get their own chapters and hopefully you will see how they build upon familiar concepts.


Chapter 3

Comparing Programming Languages

For the new programmer, some of the terms in this book will probably be unfamiliar. You should have a better idea of them by the time you finish reading this book. However, you may also be unfamiliar with the various programming languages that exist. This chapter will display some of the more popular languages currently used by programmers. These programs are designed to show how each language can be used to create the same output. You’ll notice that the Python program is significantly simpler than the others.

The following code examples all display the song, “99 Bottles of Beer on the Wall” (they have been reformatted to fit the pages). You can find more at the official 99 Bottles website: http://99-bottles- of-beer.net. I can’t vouch that each of these programs is valid and will actually run correctly, but at least you get an idea of how each one looks. You should also realize that, generally speaking, white space is not significant to a programming language (Python being one of the few exceptions). That means that all of the programs below could be written one one line, put into one huge column, or any other combination. This is why some people hate Python because it forces them to have structured, readable code.

3.1 C

/*

* 99 bottles of beer in ansi c

*

* by Bill Wein: bearheart@bearnet .com *

*/

#define MAXBEER (99) void chug(int beers);

main()

{

register beers ;

for(beers =MAXBEER; beers; chug(beers−−))

puts("") ;

puts("\nTime to buy more beer!\n"); exit (0) ;

}

void chug(register beers) {

char howmany[8], *s;

s = beers != 1 ? "s" : "";

printf ("%d bottle%s of beer on the wall ,\n" , beers , s);

printf ("%d bottle%s of beeeeer s);

. . . ,\n" , beers , around ,\n") ;

printf ("Take one down,

pass it

if(−−beers) sprintf(howmany, "%d", beers); else strcpy(howmany, "No more");

s = beers != 1 ? "s" : "";

printf ("%s bottle%s of beer on the wall .\n" ,

howmany , s ) ; }


3.2 C++

//C++ version of 99 Bottles of Beer, object oriented paradigm

//programmer : Tim Robinson timtroyr@ionet .NET

#include <fstream .h>

enum Bottle { BeerBottle };

class Shelf {

unsigned BottlesLeft ;

public :

Shelf ( unsigned bottlesbought )

: BottlesLeft ( bottlesbought ) {}

void TakeOneDown() {

if (! BottlesLeft) throw BeerBottle ;

BottlesLeft −−;

}

operator int () { return BottlesLeft; } };

int main( int , char ** ) {

Shelf Beer(99) ; try {

for (;;) {

char *plural = (int)Beer !=1 ? "s" : "

";

cout << (int)Beer << " bottle" <<

plural

<< " o f b e e r o n t h e w a l l , " << e n d l

;

cout << (int)Beer << " bottle" <<

plural

<< " o f b e e r , " << e n d l ;

Beer . TakeOneDown ( ) ;

cout << "Take one down , pass i t around

, " << e n d l ;

plural = (int)Beer !=1 ? "s":""; cout << (int)Beer << " bottle" <<

plural

<< " of beer on the wall." << endl

; }

}

catch ( Bottle ) {

cout << "Go to the store and buy some more , " << e n d l ;

cout << "99 bottles of beer on the wall." << endl;

} return 0 ;

}


3.3 Java

/**

* Java 5.0 version of the famous "99 bottles of

beer on the wall ".

* Note the use of specific Java 5.0 features and

the strictly correct output. *

* @author kvols

*/

import java.util.*; class Verse {

private final int count ; Verse ( int verse ) { count= 100−verse ;

}

public String toString () {

} }

String c=

"{0,choice,0#no more bottles|1#1

bottle|1<{0} bottles} of beer"; return java . text . MessageFormat . format (

c.replace("n","N")+" on the wall, "+c+ ".\n"+

"{0,choice,0#Go to the store and buy some more"+

"|0<Take one down and pass it around}, "+c . replace ("{0" ,"{1")+

" on the wall.\n", count, (count+99) %100) ;

class Song implements Iterator<Verse> { private int verse=1;

public boolean hasNext ( ) {

return verse <= 100; }

public Verse next () { if (!hasNext())

throw new NoSuchElementException("End of song!");

return new Verse(verse++); }

public void remove ( ) {

throw new UnsupportedOperationException

("Cannot remove verses!");

} }

public class Beer {

public static void main( String [ ] args ) {

Iterable<Verse> song= new Iterable< Verse >() {

public Iterator<Verse> iterator () {

return new Song(); }

// All this work to utilize this feature :

// "For each verse in the song..."

for ( Verse verse : song ) { System . out . println ( verse ) ;

} }

}


3.4 C#

/// ///

Implementation of Ninety−Nine Bottles of Beer Song in C#.

What’s neat is that .NET makes the Binge class a full−fledged component that may be called

from any other .NET component . ///

/// /// /// ///

Paul M. Parks

http ://www. parkscomputing .com/ February 8 , 2002

using System ;

namespace NinetyNineBottles {

/// <summary>

/// References the method of output .

/// </summary>

public delegate void Writer( string format ,

params object[] arg);

/// <summary>

/// References the corrective action to take

when we run out. /// </summary>

public delegate int MakeRun() ;

/// <summary>

/// The act of consuming all those beverages. /// </summary>

public class Binge

{

/// <summary>

/// What we’ll be drinking. /// </summary>

private string beverage ;

/// <summary>

/// The starting count. /// </summary>

private int count = 0;

/// <summary>

/// The manner in which the lyrics are

output .

/// </summary> private Writer Sing ;

/// <summary>

/// What to do when it’s all gone. /// </summary>

private MakeRun RiskDUI;

public event MakeRun OutOfBottles ;

/// <summary>

/// Initializes the binge.

/// </summary>

/// <param name="count">How many we’re

consuming . /// </param>

/// <param name="disasterWaitingToHappen"> /// Our instructions , should we succeed .

/// </param>

/// <param name="writer">How our drinking

song will be heard.</param>

/// <param name="beverage">What to drink

during this binge.</param>

public Binge( string beverage , int count , Writer writer )

{

this.beverage = beverage; this.count = count;

this . Sing = writer ;

}

/// <summary>

/// Let’s get started. /// </summary>

public void Start ()

{

while (count > 0) {

Sing ( @"

{0} bottle {1} of {2} on the wall , {0} bottle {1} of {2}.

Take one down, pass it around,",

count , (count == 1)

? "" : "s", beverage);

count −−;

if (count > 0) {

Sing("{0} bottle{1} of {2} on the wall.",

}

else

count , (count == 1)

? "" : "s", beverage);

} }

}

} }

Sing ( @"

}

else

{

Sing("No more bottles of

{0} on the wall.", beverage , null ) ;

No more bottles of {0} on the wall ,

No more bottles of {0}.", beverage, null);

if (this.OutOfBottles != null) {

count = this . OutOfBottles () ; Sing("{0} bottles of {1} on

the wall.", count, beverage);

{

Sing("First we weep, then we sleep

.");

Sing("No more bottles of {0} on

the wall.", beverage ,

null ) ;

/// <summary>

/// The song remains the same. /// </summary>

class SingTheSong

{

/// <summary>

/// Any other number would be strange. /// </summary>

const int bottleCount = 99;

} }

/// <summary>

/// The entry point. Sets the parameters

of the Binge and starts it.

/// </summary>

/// <param name="args">unused</param> static void Main( string [ ] args )

{

Binge binge =

new Binge("beer" , bottleCount , new Writer(Console.WriteLine));

binge . OutOfBottles += new MakeRun( SevenEleven ) ;

binge . Start () ; }

/// <summary>

/// There ’ s bound to be one /// </summary>

/// <returns>Whatever would

trunk.</returns>

static int SevenEleven () {

nearby .

f i t in the

Console.WriteLine("Go to the store , get some more...");

return bottleCount ; }


3.5 Python

#!/ usr/bin/env python

# −*− coding : iso −8859−1 −*−

"""

99 Bottles of Beer (by Gerold Penz) Python can be simple , too :−)

"""

for quant in range(99, 0, −1):

if quant > 1:

print (quant, "bottles of beer on the wall ,", quant, "bottles of beer.")

if quant > 2:

suffix = str(quant − 1) + " bottles of beer on the wall."

else :

suffix = "1 bottle of beer on the wall."

elif quant == 1:

print "1 bottle of beer on the wall, 1 bottle of beer."

suffix = "no more beer on the wall!"

print "Take one down, pass it around,", suffix

print "−−"


Chapter 4

The Python Interpreter

4.1 Launching the Python interpreter

Python can be programmed via the interactive command line (aka the interpreter or IDE) but anything you code won’t be saved. Once you close the session it all goes away. To save your program, it’s easiest to just type it in a text file and save it (be sure to use the .py extension, i.e. foo.py)


To use the interpreter, type “python” at the command prompt (*nix and Mac) or launch the Python IDE (Windows and Mac). If you’re using Windows and installed the Python .msi file, you should be able to also type Python on the command prompt. The main difference between the IDE and the command prompt is the command prompt is part of the operating system while the IDE is part of Python. The command prompt can be used for other tasks besides messing with Python; the IDE can only be used for Python.

Use whichever you’re more comfortable with.


If you’re using Linux, BSD, or another *nix operating system, I’ll assume you technologically-inclined enough to know about the terminal; you probably even know how to get Python up and running already. For those who aren’t used to opening Terminal or Command Prompt (same thing, different name on different operating systems), here’s how to do it.


4.1.1 Windows

1. Open the Start menu.

2. Click on “Run...”

3. Type “cmd” in the text box (without the quotes) and hit Return.

4. You should now have a black window with white text. This is the command prompt.

5. If you type “python” at the prompt, you should be dropped into the Python interpreter prompt.


4.1.2 Mac

1. Open Applications

2. Open Utilities

3. Scroll down and open Terminal

4. You should have a similar window as Windows users above.

5. Type “python” at the prompt and you will be in the Python interpreter.


Here is what your terminal should look like now:

Listing 4.1: Example Python interpreter

Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17) [GCC 4.0.1 (Apple Inc . build 5465) ] on darwin

Type "help", "copyright", "credits" or "license" for more information .

>>>


You may notice that the Python version used in the above examples is 2.5.1; the current version is 3.2. Don’t panic. The vast majority of what you will learn will work regardless of what version you’re using. My favorite Python book, Python How to Program, is based on version 2.2 but I still use it nearly every day as a reference while coding.


For the most part, you won’t even notice what version is in use, unless you are using a library, function, or method for a specific version. Then, you simply add a checker to your code to identify what version the user has and notify him to upgrade or modify your code so it is backwards-compatible.


(A later chapter in this book will cover all the major differences you need to be aware of when using Python 3.x. Right now you’re just learning the basics that you will use regardless of which version of the language you end up using.)


The >>> in Listing 4.1 is the Python command prompt; your code is typed here and the result is printed on the following line, without a prompt. For example, Listing 4.2 shows a simple print statement from Python 2.5:


Listing 4.2: Python command prompt

>>>print "We are the knights who say, ’Ni’." We are the knights who say, ’Ni’.


4.2 Python Versions

As an aside, Python 3.x has changed print from a simple statement, like Listing 4.2, into a bona-fide function (Listing 4.3). Since we haven’t talked about functions yet, all you need to know is that Python 3.x simply requires you to use the print statement in a slightly different way: parenthesis need to surround the quoted words. Just so you’re aware.


Listing 4.3: Print function (Python 3.x)

>>>print ("We are the knights who say, ’Ni’.") We are the knights who say, ’Ni’.


The vast majority of the code in this book will be written for Python 2.6 or earlier, since those versions are installed by default on many *nix systems and is therefore still very popular; there is also a lot of older code in the wild, especially open-source programs, that haven’t been (and probably never will be) upgraded to Python 3.x. If you decide to use other Python programs to study from or use, you will need to know the “old school” way of Python programming; many open-source programs are still written for Python 2.4.


If you are using Python 3.x and you want to type every example into your computer as we go along, please be aware that the print statements, as written, won’t work. They will have to be modified to use a print() function like in Listing 4.3. For more information about Python 3, see Chapter 19.


4.3 Using the Python Command Prompt

If you write a statement that doesn’t require any “processing” by Python, it will simply return you to the prompt, awaiting your next order. The next code example shows the user assigning the value “spam” to the variable can. Python doesn’t have to do anything with this, in regards to calculations or anything, so it accepts the statement and then waits for a new statement.


Listing 4.4: Python statements

>>>can = "spam"

>>>


(By the way, Python was named after Monty Python, not the snake. Hence, much of the code you’ll find on the Internet, tutorials, and books will have references to Monty Python sketches.)


The standard Python interpreter can be used to test ideas before you put them in your code. This is a good way to hash out the logic required to make a particular function work correctly or see how a conditional loop will work. You can also use the interpreter as a simple calculator. This may sound geeky, but I often launch a Python session for use as a calculator because it’s often faster than clicking through Windows’ menus to use its calculator.


Here’s an example of the “calculator” capabilities:


Listing 4.5: Python as a calculator

>>>2+2 4

>>>4*4 16

>>>5**2 #five squared

25


Python also has a math library that you can import to do trigonometric functions and many other higher math calculations. Importing libraries will be covered later in this book.


4.4 Commenting Python

One final thing to discuss is that comments in Python are marked with the “#” symbol. Comments are used to annotate notes or other information without having Python try to perform an operation on them.


For example,


Listing 4.6: Python comments

>>>dict = {"First phonetic":"Able", "Second phonetic":"Baker"} #create a dictionary

>>>print dict . keys () #dictionary values aren’t in order

[’Second phonetic’, ’First phonetic’]

>>>print dict ["First phonetic"] #print the key’s value

Able


You will see later on that, even though Python is a very readable language, it still helps to put comments in your code. Sometimes it’s to explicitly state what the code is doing, to explain a neat shortcut you used, or to simply remind yourself of something while you’re coding, like a “todo” list.


4.5 Launching Python programs

If you want to run a Python program, simply type python at the shell command prompt (not the IDE or interactive interpreter) followed by the program name.


Listing 4.7: Launching a Python program

$python foo.py


Files saved with the .py extension are called modules and can be called individually at the command line or within a program, similar to header files in other languages. If your program is going to import other modules, you will need to make sure they are all saved in the same directory on the computer. More information on working with modules can be found later in this book or in the Python documentation.

Depending on the program, certain arguments can be added to the command line when launching the program. This is similar to adding switches to a Windows DOS prompt command. The arguments tell the program what exactly it should do. For example, perhaps you have a Python program that can output it’s processed data to a file rather than to the screen. To invoke this function in the program you simply launch the program like so:


Listing 4.8: Launching a Python program with arguments

$python foo.py −f


The “-f” argument is received by the program and calls a function that prints the data to a designated location within the computer’s file system instead of printing it to the screen.


If you have multiple versions of Python installed on your computer, e.g. the system default is version 2.5 but you want to play around with Python 3.x, you simply have to tell the OS which version to use (see Listing 4.9). This is important since many older Python programs aren’t immediately compatible with Python 3.x. In many cases, an older version of Python must be retained on a computer to enable certain programs to run correctly; you don’t want to completely overwrite older Python versions.


Listing 4.9: Selecting a Python version

$python2 .5 sample . py #force use of Python 2.5

$python3 .0 sample . py #force use of Python 3.0


4.6 Integrated Development Environments

I should take the time now to explain more about programming environments. Throughout this book, most of the examples involve using the Python interactive interpreter, which is used by typing “python” at the operating system command prompt. This environment is is really more for testing ideas or simple “one-shot” tasks. Because the information isn’t stored anywhere, when the Python session is finished, all the data you worked with goes away.


To make a reusable program, you need to use some sort of source code editor. These editors can be an actual programming environment application, a.k.a. IDEs (integrated development environments), or they can be a simple text editor like Windows Notepad. There is nothing special about source code; regardless of the programming language, it is simply text. IDEs are basically enhanced text editors that provide special tools to make programming easier and quicker.


Python has many IDEs available and nearly all of them are free. Typically, an IDE includes a source code editor, a debugger, a compiler (not necessary for Python), and often a graphical user interface builder; different IDEs may include or remove certain features. Below is a list of some common Python IDEs:


• Eric-a free application that acts as a front-end to other programs and uses plug-ins

• IDLE-a free application included in the base Python installation; includes an integrated debugger

• Komodo-a full-featured, proprietary application that can be used for other programming languages

• PyDev-a plug-in for the Eclipse development environment

• Stani’s Python Editor (SPE)-a free application that includes many development aids, such as wxGlade, a debugger, and an interactive interpreter


Chapter 5

Types and Operators

Python is based on the C programming language and is written in C, so much of the format Python uses will be familiar to C and C++ programmers. However, it makes life a little easier because it’s not made to be a low-level language (it’s difficult to interact heavily with hardware or perform memory allocation) and it has built-in “garbage collection” (it tracks references to objects and automatically removes objects from memory when they are no longer referenced), which allows the programmer to worry more about how the program will work rather than dealing with the computer.


5.1 Python Syntax

5.1.1 Indentation

Python forces the user to program in a structured format. Code blocks are determined by the amount of indentation used. As you’ll recall from the Comparison of Programming Languages chapter, brackets and semicolons were used to show code grouping or end-of-line termination for the other languages. Python doesn’t require those; indentation is used to signify where each code block starts and ends. Here is an example:


Listing 5.1: White space is significant

1 x=1

2 if x: #if x is true

3     y=2

4     if y: #if y is true

5          print "block 2"

6     print "block 1"

7 else: print "block 0"


Each indented line demarcates a new code block. To walk through the above code snippet, line 1 is the start of the main code block. Line 2 is a new code section; if “x” has a value not equal to 0, then indented lines below it will be evaluated. Hence, lines 3 and 4 are in another code section and will be evaluated if line 2 is true. Line 5 is yet another code section and is only evaluated if “y” is not equal to 0. Line 6 is part of the same code block as lines 3 and 4; it will also be evaluated in the same block as those lines. Line 7 is in the same section as line 1 and is evaluated regardless of what any indented lines may do; in this case, “block 0” will not be printed because the previous statements are both true.

You’ll notice that compound statements, like the if comparisons, are created by having the header line followed by a colon (“:”). The rest of the statement is indented below it. The biggest thing to remember is that indentation determines grouping; if your code doesn’t work for some reason, double-check which statements are indented.


A quick note: the act of saying “x = 1” is assigning a value to a variable. In this case, “x” is the variable; by definition its value varies. That just means that you can give it any value you want; in this case the value is “1”. Variables are one of the most common programming items you will work with because they are what store values and are used in data manipulation.


5.1.2 Multiple Line Spanning

Statements can span more than one line if they are collected within braces (parenthesis “()”, square brackets “[]”, or curly braces “{}”). Normally parentheses are used. When spanning lines within braces, indentation doesn’t matter; the indentation of the initial bracket used to determine which code section the whole statement belongs to. String statements can also be multi-line if you use triple quotes. For example:


Listing 5.2: Use of triple quotes

>>> big = """This is

... a multi−line block

... of text; Python puts

... an end−of−line marker

... after each line. """

>>>

>>> big

’This is\012a multi−line block\012of text; Python puts\012an end−of−line marker \012 after each line.’


Note that the \012 is the octal version of \n, the “newline” indicator. The ellipsis (...) above are blank lines in the interactive Python prompt used to indicate the interpreter is waiting for more information.


5.2 Python Object Types

Like many other programming languages, Python has built-in data types that the programmer uses to create his program. These data types are the building blocks of the program. Depending on the language, different data types are available. Some languages, notably C and C++, have very primitive types; a lot of programming time is simply used up to combine these primitive types into useful data structures. Python does away with a lot of this tedious work. It already implements a wide range of types and structures, leaving the developer more time to actually create the program. Trust me; this is one of the things I hated when I was learning C/C++. Having to constantly recreate the same data structures for every program is not something to look forward to.


Python has the following built-in types: numbers, strings, lists, dictionaries, tuples, and files. Naturally, you can build your own types if needed, but Python was created so that very rarely will you have to “roll your own”. The built-in types are powerful enough to cover the vast majority of your code and are easily enhanced. We’ll finish up this section by talking about numbers; we’ll cover the others in later chapters.

Before I forget, I should mention that Python doesn’t have strong coded types; that is, a variable can be used as an integer, a float, a string, or whatever. Python will determine what is needed as it runs. See below:


Listing 5.3: Weak coding types

>>> x = 12

>>> y = "lumberjack"

>>> x

12

>>> y

’ lumberjack ’


Other languages often require the programmer to decide what the variable must be when it is initially created. For example, C would require you to declare “x” in the above program to be of type int and “y” to be of type string. From then on, that’s all those variables can be, even if later on you decide that they should be a different type.


That means you have to decide what each variable will be when you start your program, i.e. deciding whether a number variable should be an integer or a floating-point number. Obviously you could go back and change them at a later time but it’s just one more thing for you to think about and remember. Plus, anytime you forget what type a variable is and you try to assign the wrong value to it, you get a compiler error.


5.3 Python Numbers

Python can handle normal long integers (max length determined based on the operating system, just like C), Python long integers (max length dependent on available memory), floating point numbers (just like C doubles), octal and hex numbers, and complex numbers (numbers with an imaginary component).


Here are some examples of these numbers:

• integer: 12345, -32

• Python integer: 999999999L (In Python 3.x, all integers are Python integers)

• float: 1.23, 4e5, 3e-4

• octal: 012, 0456

• hex: 0xf34, 0X12FA

• complex: 3+4j, 2J, 5.0+2.5j


Python has the normal built-in numeric tools you’d expect: expression operators (*, >>, +, <, etc.), math functions (pow, abs, etc.), and utilities (rand, math, etc.). For heavy number-crunching Python has the Numeric Python (NumPy) extension that has such things as matrix data types. If you need it, it has to be installed separately. It’s heavily used in science and mathematical settings, as it’s power and ease of use make it equivalent to Mathematica, Maple, and MatLab.


Though this probably doesn’t mean much to non-programmers, the expression operators found in C have been included in Python, however some of them are slightly different. Logic operators are spelled out in Python rather than using symbols, e.g. logical AND is represented by “and”, not by “&&”; logical OR is represented by “or”, not “||”; and logical NOT uses “not” instead of “!”. More information can be found in the Python documentation.


Operator level-of-precedence is the same as C, but using parentheses is highly encouraged to ensure the expression is evaluated correctly and enhance readability. Mixed types (float values combined with integer values) are converted up to the highest type before evaluation, i.e. adding a float and an integer will cause the integer to be changed to a float value before the sum is evaluated.

Following up on what I said earlier, variable assignments are created when first used and do not have to be pre-declared like in C.


Listing 5.4: Generic C++ example

int a = 3; //inline initialization of integer float b;

b = 4.0f; //sequential initialization of floating point number


Listing 5.5: Generic Python example

>>>a = 3 #integer

>>>b = 4.0 #floating point


As you can see, “a” and “b” are both numbers but Python can figure out what type they are without being told. In the C++ example, a float value has to be “declared” twice; first the variable is given a type (“float”) then the actual value is given to the variable. You’ll also note that comments in Python are set off with a hash/pound sign (#) and are used exactly like the “//” comments in C++ or Java.


That’s about it for numbers in Python. It can also handle bit-wise manipulation such as left-shift and right-shift, but if you want to do that, then you’ll probably not want to use Python for your project. As also stated, complex numbers can be used but if you ever need to use them, check the documentation first.


Chapter 6

Strings

Strings in programming are simply text, either individual characters, words, phrases, or complete sentences. They are one of the most common elements to use when programming, at least when it comes to interacting with the user. Because they are so common, they are a native data type within Python, meaning they have many powerful capabilities built-in. Unlike other languages, you don’t have to worry about creating these capabilities yourself. This is good because the built-in ones have been tested many times over and have been optimized for performance and stability.


Strings in Python are different than most other languages. First off, there are no char types, only single character strings (char types are single characters, separate from actual strings, used for memory conservation). Strings also can’t be changed in-place; a new string object is created whenever you want to make changes to it, such as concatenation. This simply means you have to be aware that you are not manipulating the string in memory; it doesn’t get changed or deleted as you work with it. You are simply creating a new string each time.


Here’s a list of common string operations:

• s1=‘’: empty string

• s2 = “knight’s” : double quotes

• block = “““ - ””” : triple-quoted block

• s1 + s2 : concatenate (combine)

• s2 * 3 : repeat the string a certain number of times

• s2[n] : index (the position of a certain character)

• len(s2) : get the length of a string

• “a %s parrot” % ‘dead’ : string formatting (deprecated in Python 3.x)

• “a {0} parrot”.format(“dead”) : string formatting (Python 3.x)

• for x in s2 : iteration (sequentially move through the string’s characters)

• ‘m’ in s2 : membership (is there a given character in the string?)


Empty strings are written as two quotes with nothing in between. The quotes used can be either single or double; my preference is to use double quotes since you don’t have to escape the single quote to use it in a string. That means you can write a statement like

“And then he said, ‘No way’ when I told him.”


If you want to use just one type of quote mark all the time, you have to use the backslash character to “escape” the desired quote marks so Python doesn’t think it’s at the end of the phrase, like this:


“And then he said, \”No way\” when I told him.”


Triple quoted blocks are for strings that span multiple lines, as shown last chapter. Python collects the entire text block into a single string with embedded newline characters. This is good for things like writing short paragraphs of text, e.g. instructions, or for formatting your source code for clarification.


6.1 Basic string operations

The “+” and “*” operators are overloaded in Python, letting you concatenate and repeat string objects, respectively. Overloading is just using the same operator to do multiple things, based on the situation where it’s used. For example, the “+” symbol can mean addition when two numbers are involved or, as in this case, combining strings.


Concatenation combines two (or more) strings into a new string object whereas repeat simply repeats a given string a given number of times. Here are some examples:


Listing 6.1: Operator overloading

>>> len(’abc’) #length: number items 3

>>> ’abc’ + ’def’ #concatenation: a new string ’ abcdef ’

>>> ’Ni!’ * 4 #multiple concatentation: "Ni!" + "Ni!" + ...

’Ni!Ni!Ni!Ni! ’


You need to be aware that Python doesn’t automatically change a number to a string, so writing “spam” + 3 will give you an error. To explicitly tell Python that a number should be a string, simply tell it. This is similar to casting values in C/C++. It informs Python that the number is not an integer or floating point number but is, in reality, a text representation of the number. Just remember that you can no longer perform mathematical functions with it; it’s strictly text.


Listing 6.2: Casting a number to a string

>>>str (3) #converts number to string


Iteration in strings is a little different than in other languages. Rather than creating a loop to continually go through the string and print out each character, Python has a built-in type for iteration. Here’s an example followed by an explanation:


Listing 6.3: Iteration through a string

>>> myjob = "lumberjack"

>>> for c in myjob: print c, #step through items

...

lumberjack

>>> "k" in myjob #1 means true

1


Essentially what is happening is that Python is sequentially going through the variable “myjob” and printing each character that exists in the string. for statements will be covered in depth later in the book but for now just be aware that they are what you use to step through a range of values. As you can see they can be used for strings or, more often, numbers.


The second example is simply a comparison. Does the letter “k” exist in the value stored by “myjob”? If yes, then Python will return a numeric value of 1, indicating yes. If “k” didn’t exist, it would return a 0. This particular case is most often used in word processing applications, though you can probably think of other situations where it would be useful.


6.2 Indexing and slicing strings

Strings in Python are handled similar to arrays in C. Unlike C arrays, characters within a string can be accessed both front and backwards. Front-ways, a string starts of with a position of 0 and the character desired is found via an offset value (how far to move from the end of the string). However, you also can find this character by using a negative offset value from the end of the string. I won’t go deeply into it, but here’s a quick example:


Listing 6.4: String indexing

>>>S = "spam"

>>>S[0] , S[−2] #indexing from the front and rear

(’s’, ’a’)


Indexing is simply telling Python where a character can be found within the string. Like many other languages, Python starts counting at 0 instead of 1. So the first character’s index is 0, the second character’s index is 1, and so on. It’s the same counting backwards through the string, except that the last letter’s index is -1 instead of 0 (since 0 is already taken). Therefore, to index the final letter you would use -1, the second to the last letter is -2, etc. Knowing the index of a character is important for slicing.


Slicing a string is basically what it sounds like: by giving upper and lower index values, we can pull out just the characters we want. A great example of this is when processing an input file where each line is terminated with a newline character; just slice off the last character and process each line. You could also use it to process command- line arguments by “filtering” out the program name. Again, here’s an example:


Listing 6.5: String slicing

>>>S = "spam"

>>>S[1:3] , S[1:] , S[:−1] #slicing : extract section

(’pa’, ’pam’, ’spa’)


You’ll notice that the colon symbol is used when slicing. The colon acts as a separator between the upper and lower index values. If one of those values is not given, Python interprets that to mean that you want everything from the index value to the end of the string. In the example above, the first slice is from index 1 (the second letter, inclusive) to index 3 (the 4th letter, exclusive). You can consider the index to actually be the space before each letter; that’s why the letter “m” isn’t included in the first slice but the letter “p” is.

The second slice is from index 1 (the second letter) to the end of the string. The third slice starts at the end of the string and goes backwards.


6.3 String Formatting

Formatting strings is simply a way of presenting the information on the screen in a way that conveys the information best. Some examples of formatting are creating column headers, dynamically creating a sentence from a list or stored variable, or stripping extraneous information from the strings, such as excess spaces. (Python 3.x has a new way of formatting strings; this will be discussed in the 19 section below.)

Python supports the creation of dynamic strings. What this means is that you can create a variable containing a value of some type (such as a string or number) then “call” that value into your string. You can process a string the same way as in C if you choose to, such as %d for integers and %f for floating point numbers. Here’s an example:


Listing 6.6: Dynamic string creation

>>>S = "parrot"

>>>d = 1

>>>print ’That is %d dead %s!’ % (d, s)

That is 1 dead parrot!


Python also has a string utility module for tools such as case conversion, converting strings to numbers, etc. Here’s yet another example:


Listing 6.7: String utilities

>>> import string #standard utilities module

>>> S = "spammify"

>>> string .upper(S) #convert to uppercase

’SPAMMIFY ’

>>> string.find(S, "mm") #return index of substring

3

>>> string.atoi("42"), ‘42‘ #convert from/to string

(42, ’42’)

>>> string . join ( string . split (S, "mm") , "XX")

’ spaXXify ’


Notice the example of the second to last line. Backquotes are used to convert an object into a string. This is one way around the “don’t mix strings and numbers” problem from earlier. I’ll leave the last line example above as a mental test. See if you can figure out what the statement is doing.


Though it’s not strictly a string operation (it can be used with just about anything that can be measured), the len() method can be used to give you the length of a string. For example,


Listing 6.8: Finding the length of a string

>>>string = "The Life of Brian" >>>print len ( string )

17

>>>len("The Meaning of Life")

19


As shown in the second example above, you don’t necessarily have to use a print statement (or print() function in Python 3.x) to display a value. Simply writing what you want will print out the result. However, this doesn’t always work in your favor. Sometimes the object will only return a memory address, as we will see later in the book. Generally speaking, it’s simply easier to explicitly state “print” if you want a statement evaluated and printed out. Otherwise you don’t know exactly what value it will return.


6.4 Combining and Separating Strings

Strings can be combined (joined) and separated (split) quite easily. Tokenization is the process of splitting something up into individual tokens; in this case, a sentence is split into individual words. When a web page is parsed by a browser, the HTML, Javascript, and any other code in the page is tokenized and identified as a keyword, operator, variable, etc. The browser then uses this information to display the web page correctly, or at least as well as it can.


Continue reading this ebook at Smashwords.
Purchase this book or download sample versions for your ebook reader.
(Pages 1-94 show above.)