Transcription

Introduction to Python ProgrammingCourse NotesPhil SpectorDepartment of Statistics, University of California BerkeleyMarch 16, 2005

2

Contents1 Introduction1.1 What is Python? . . . . . . . . . . . . .1.2 The very Basics of Python . . . . . . . .1.3 Invoking Python . . . . . . . . . . . . .1.4 Basic Principles of Python . . . . . . . .1.4.1 Basic Core Language . . . . . . .1.4.2 Modules . . . . . . . . . . . . . .1.4.3 Object Oriented Programming . .1.4.4 Namespaces and Variable Scoping1.4.5 Exception Handling . . . . . . . .2 String Data2.1 String Constants . . . . . . . . . . . . . . .2.2 Special Characters and Raw Strings . . . . .2.3 Unicode Strings . . . . . . . . . . . . . . . .2.4 String Operations . . . . . . . . . . . . . . .2.4.1 Concatenation . . . . . . . . . . . . .2.4.2 Repetition . . . . . . . . . . . . . . .2.4.3 Indexing and Slicing . . . . . . . . .2.4.4 Functions and Methods for Character3 Numeric Data3.1 Types of Numeric Data . . . . . . . . . .3.1.1 Hexadecimal and Octal Constants3.1.2 Numeric Operators . . . . . . . .3.1.3 Functions for Numeric Data . . .3.2 Conversion of Scalar Types . . . . . . . .3. . . . . . . . . . . . . . . . . . . . . . . . . . . . 1313233

4CONTENTS4 Lists, Tuples and Dictionaries4.1 List Data . . . . . . . . . . . . . . . .4.2 List Indexing and Slicing . . . . . . . .4.3 List Operators . . . . . . . . . . . . . .4.3.1 Concatenation . . . . . . . . . .4.3.2 Repetition . . . . . . . . . . . .4.3.3 The in operator . . . . . . . . .4.4 Functions and Methods for Lists . . . .4.5 Tuple Objects . . . . . . . . . . . . . .4.6 Operators and Indexing for Tuples . .4.7 Functions and Methods for Tuples . . .4.8 Dictionaries . . . . . . . . . . . . . . .4.9 Functions and Methods for Dictionaries5 Input and Output5.1 The print command . . . . . . . . .5.2 Formatting Strings . . . . . . . . . .5.3 Using Names in Format Strings . . .5.4 File Objects . . . . . . . . . . . . . .5.4.1 Methods for Reading . . . . .5.4.2 Methods for Writing . . . . .5.4.3 “Printing” to a File . . . . . .5.4.4 Other Methods . . . . . . . .5.4.5 File Object Variables . . . . .5.5 Standard Input and Output Streams5.6 Pipes . . . . . . . . . . . . . . . . . .6 Programming6.1 Assignments . . . . . . . . . . . . . . . .6.2 Indentation . . . . . . . . . . . . . . . .6.3 Truth, Falsehood and Logical Operators6.4 if statement . . . . . . . . . . . . . . .6.5 for loops . . . . . . . . . . . . . . . . .6.6 for loops and the range function . . . .6.7 while loops . . . . . . . . . . . . . . . .6.8 Control in Loops: break and continue .6.9 List Comprehensions . . . . . . . . . . 63.67677272747678808284

CONTENTS7 Functions7.1 Introduction . . . . . . . . . . . . . . . . . . . . . .7.2 Scoping: How Python finds your variables . . . . .7.3 Function Basics . . . . . . . . . . . . . . . . . . . .7.4 Named Arguments and Default Values . . . . . . .7.5 Variable Number of Arguments . . . . . . . . . . .7.6 Functional Programming, and anonymous functions5.878788929396988 Using Modules1038.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1038.2 Namespaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1048.3 Functions for working with modules . . . . . . . . . . . . . . . 1068.4 The string module . . . . . . . . . . . . . . . . . . . . . . . . 1068.4.1 String Constants . . . . . . . . . . . . . . . . . . . . . 1068.4.2 Functions in the string module . . . . . . . . . . . . . 1078.5 The re module: Regular Expressions . . . . . . . . . . . . . . 1098.5.1 Introduction to Regular Expressions . . . . . . . . . . 1098.5.2 Constructing Regular Expressions . . . . . . . . . . . . 1098.5.3 Compiling Regular Expressions . . . . . . . . . . . . . 1108.5.4 Finding Regular Expression Matches . . . . . . . . . . 1118.5.5 Tagging in Regular Expressions . . . . . . . . . . . . . 1138.5.6 Using Named Groups for Tagging . . . . . . . . . . . . 1158.5.7 Greediness of Regular Expressions . . . . . . . . . . . . 1168.5.8 Multiple Matches . . . . . . . . . . . . . . . . . . . . . 1178.5.9 Substitutions . . . . . . . . . . . . . . . . . . . . . . . 1198.6 Operating System Services: os and shutil modules . . . . . . 1218.7 Expansion of Filename wildcards - the glob module . . . . . . 1258.8 Information about your Python session - the sys module . . . 1268.9 Copying: the copy module . . . . . . . . . . . . . . . . . . . . 1278.10 Object Persistence: the pickle/cPickle and shelve modules 1288.10.1 Pickling . . . . . . . . . . . . . . . . . . . . . . . . . . 1288.10.2 The shelve module . . . . . . . . . . . . . . . . . . . . 1308.11 CGI (Common Gateway Interface): the cgi module . . . . . . 1318.11.1 Introduction to CGI . . . . . . . . . . . . . . . . . . . 1318.11.2 Security Concerns . . . . . . . . . . . . . . . . . . . . . 1348.11.3 CGI Environmental Variables . . . . . . . . . . . . . . 1358.12 Accessing Documents on the Web: the urllib module . . . . 135

69 Exceptions9.1 Introduction . . . . . . . . . . . .9.2 Tracebacks . . . . . . . . . . . . .9.3 Dealing with Multiple Exceptions9.4 The Exception Hierarchy . . . . .9.5 Raising Exceptions . . . . . . . .CONTENTS.10 Writing Modules10.1 Introduction . . . . . . . . . . . . . . . . .10.2 An Example . . . . . . . . . . . . . . . . .10.3 Test Programs for Modules . . . . . . . . .10.4 Classes and Object Oriented Programming10.5 Operator Overloading . . . . . . . . . . . .10.6 Private Attributes . . . . . . . . . . . . . .10.7 A First Example of Classes . . . . . . . . .10.8 Inheritance . . . . . . . . . . . . . . . . .10.9 Adding Methods to the Basic Types . . . .10.10Iterators . . . . . . . . . . . . . . . . . . .139. 139. 139. 140. 142. 142.147. 147. 148. 150. 151. 152. 153. 153. 158. 163. 164

Chapter 1Introduction1.1What is Python?Python is a high-level scripting language which can be used for a wide varietyof text processing, system administration and internet-related tasks. Unlikemany similar languages, it’s core language is very small and easy to master, while allowing the addition of modules to perform a virtually limitlessvariety of tasks. Python is a true object-oriented language, and is availableon a wide variety of platforms. There’s even a python interpreter writtenentirely in Java, further enhancing python’s position as an excellent solutionfor internet-based problems.Python was developed in the early 1990’s by Guido van Rossum, thenat CWI in Amsterdam, and currently at CNRI in Virginia. In some ways,python grew out of a project to design a computer language which would beeasy for beginners to learn, yet would be powerful enough for even advancedusers. This heritage is reflected in python’s small, clean syntax and the thoroughness of the implementation of ideas like object-oriented programming,without eliminating the ability to program in a more traditional style. Sopython is an excellent choice as a first programming language without sacrificing the power and advanced capabilities that users will eventually need.Although pictures of snakes often appear on python books and websites,the name is derived from Guido van Rossum’s favorite TV show, “MontyPython’s Flying Circus”. For this reason, lots of online and print documentation for the language has a light and humorous touch. Interestingly, manyexperienced programmers report that python has brought back a lot of the7

8CHAPTER 1. INTRODUCTIONfun they used to have programming, so van Rossum’s inspiration may be wellexpressed in the language itself.1.2The very Basics of PythonThere are a few features of python which are different than other programming languages, and which should be mentioned early on so that subsequentexamples don’t seem confusing. Further information on all of these featureswill be provided later, when the topics are covered in depth.Python statements do not need to end with a special character – thepython interpreter knows that you are done with an individual statementby the presence of a newline, which will be generated when you press the“Return” key of your keyboard. If a statement spans more than one line, thesafest course of action is to use a backslash (\) at the end of the line to letpython know that you are going to continue the statement on the next line;you can continue using backslashes on additional continuation lines. (Thereare situations where the backslashes are not needed which will be discussedlater.)Python provides you with a certain level of freedom when composing aprogram, but there are some rules which must always be obeyed. One ofthese rules, which some people find very surprising, is that python uses indentation (that is, the amount of white space before the statement itself) toindicate the presence of loops, instead of using delimiters like curly braces({}) or keywords (like “begin” and “end”) as in many other languages. Theamount of indentation you use is not important, but it must be consistentwithin a given depth of a loop, and statements which are not indented mustbegin in the first column. Most python programmers prefer to use an editor like emacs, which automatically provides consistent indentation; you willprobably find it easier to maintain your programs if you use consistent indentation in every loop, at all depths, and an intelligent editor is very usefulin achieving this.1.3Invoking PythonThere are three ways to invoke python, each with its’ own uses. The firstway is to type “python” at the shell command prompt. This brings up the

1.3. INVOKING PYTHON9python interpreter with a message similar to this one:Python 2.2.1 (#2, Aug 27 2002, 09:01:47)[GCC 2.95.4 20011002 (Debian prerelease)] on linux2Type "help", "copyright", "credits" or "license" for more information.The three greater-than signs ( ) represent python’s prompt; you type yourcommands after the prompt, and hit return for python to execute them. Ifyou’ve typed an executable statement, python will execute it immediatelyand display the results of the statement on the screen. For example, if I usepython’s print statement to print the famous “Hello, world” greeting, I’llimmediately see a response: print ’hello,world’hello,worldThe print statement automatically adds a newline at the end of the printedstring. This is true regardless of how python is invoked. (You can suppressthe newline by following the string to be printed with a comma.)When using the python interpreter this way, it executes statements immediately, and, unless the value of an expression is assigned to a variable(See Section 6.1), python will display the value of that expression as soon asit’s typed. This makes python a very handy calculator: cost 27.00 taxrate .075 cost * taxrate2.025 16 25 92 * 3317When you use python interactively and wish to use a loop, you must,as always, indent the body of the loop consistently when you type yourstatements. Python can’t execute your statements until the completion ofthe loop, and as a reminder, it changes its prompt from greater-than signs toperiods. Here’s a trivial loop that prints each letter of a word on a separateline — notice the change in the prompt, and that python doesn’t responduntil you enter a completely blank line.

10CHAPTER 1. INTRODUCTION word ’python’ for i in word:.print i.pythonThe need for a completely blank line is peculiar to the interactive use ofpython. In other settings, simply returning to the previous level of indentation informs python that you’re closing the loop.You can terminate an interactive session by entering the end-of-file character appropriate to your system (control-Z for Windows, control-D for Unix),or by enteringimport syssys.exit()orraise SystemExitat the python prompt.For longer programs, you can compose your python code in the editor ofyour choice, and execute the program by either typing “python”, followedby the name of the file containing your program, or by clicking on the file’sicon, if you’ve associated the suffix of your python file with the python interpreter. The file extension most commonly used for python files is “.py”.Under UNIX systems, a standard technique for running programs writtenin languages like python is to include a specially formed comment as thefirst line of the file, informing the shell where to find the interpreter for yourprogram. Suppose that python is installed as /usr/local/bin/python onyour system. (The UNIX command “which python” should tell you wherepython is installed if it’s not in /usr/local/bin.) Then the first line of yourpython program, starting in column 1, should look like this:#!/usr/local/bin/python

1.4. BASIC PRINCIPLES OF PYTHON11After creating a file, say myprogram.py, which contains the special commentas its first line, you would make the file executable (through the UNIX command “chmod x myprogram.py”), and then you could execute your program by simply typing “myprogram.py” at the UNIX prompt.When you’re running python interactively, you can instruct python to execute files containing python programs with the execfile function.