Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1 | % Format this file with latex. |
| 2 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 3 | %\documentstyle[11pt,myformat]{article} |
| 4 | \documentstyle[palatino,11pt,myformat]{article} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 5 | |
| 6 | \title{\bf |
| 7 | Python Tutorial \\ |
| 8 | (DRAFT) |
| 9 | } |
| 10 | |
| 11 | \author{ |
| 12 | Guido van Rossum \\ |
| 13 | Dept. CST, CWI, Kruislaan 413 \\ |
| 14 | 1098 SJ Amsterdam, The Netherlands \\ |
| 15 | E-mail: {\tt guido@cwi.nl} |
| 16 | } |
| 17 | |
| 18 | \begin{document} |
| 19 | |
| 20 | \pagenumbering{roman} |
| 21 | |
| 22 | \maketitle |
| 23 | |
| 24 | \begin{abstract} |
| 25 | |
| 26 | \noindent |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 27 | Python is a simple, yet powerful programming language that bridges the |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 28 | gap between C and shell programming, and is thus ideally suited for rapid |
| 29 | prototyping. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 30 | Its syntax is put together from constructs borrowed from a variety of other |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 31 | languages; most prominent are influences from ABC, C, Modula-3 and Icon. |
| 32 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 33 | The Python interpreter is easily extended with new functions and data |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 34 | types implemented in C. |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 35 | Python is also suitable as an extension language for highly |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 36 | customizable C applications such as editors or window managers. |
| 37 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 38 | Python is available for various operating systems, amongst which |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 39 | several flavors of \UNIX, Amoeba, and the Apple Macintosh O.S. |
| 40 | |
| 41 | This tutorial introduces the reader informally to the basic concepts and |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 42 | features of the Python language and system. |
| 43 | It helps to have a Python interpreter handy for hands-on experience, |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 44 | but as the examples are self-contained, the tutorial can be read |
| 45 | off-line as well. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 46 | |
| 47 | For a description of standard objects and modules, see the Library |
| 48 | Reference document. |
| 49 | The Language Reference document (XXX not yet existing) |
Guido van Rossum | 67fa160 | 1991-04-23 14:14:57 +0000 | [diff] [blame] | 50 | gives a more formal definition of the language. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 51 | |
| 52 | \end{abstract} |
| 53 | |
| 54 | \pagebreak |
| 55 | |
| 56 | \tableofcontents |
| 57 | |
| 58 | \pagebreak |
| 59 | |
| 60 | \pagenumbering{arabic} |
| 61 | |
| 62 | \section{Whetting Your Appetite} |
| 63 | |
| 64 | If you ever wrote a large shell script, you probably know this feeling: |
| 65 | you'd love to add yet another feature, but it's already so slow, and so |
| 66 | big, and so complicated; or the feature involves a system call or other |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 67 | funcion that is only accessible from C \ldots |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 68 | Usually the problem at hand isn't serious enough to warrant rewriting |
| 69 | the script in C; perhaps because the problem requires variable-length |
| 70 | strings or other data types (like sorted lists of file names) that |
| 71 | are easy in the shell but lots of work to implement in C; or perhaps |
| 72 | just because you're not sufficiently familiar with C. |
| 73 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 74 | In such cases, Python may be just the language for you. |
| 75 | Python is simple to use, but it is a real programming language, offering |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 76 | much more structure and support for large programs than the shell has. |
| 77 | On the other hand, it also offers much more error checking than C, and, |
| 78 | being a |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 79 | {\em very-high-level language}, |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 80 | it has high-level data types built in, such as flexible arrays and |
| 81 | dictionaries that would cost you days to implement efficiently in C. |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 82 | Because of its more general data types Python is applicable to a |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 83 | much larger problem domain than |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 84 | {\em Awk} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 85 | or even |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 86 | {\em Perl}, |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 87 | yet most simple things are at least as easy in Python as in those |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 88 | languages. |
| 89 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 90 | Python allows you to split up your program in modules that can be reused |
| 91 | in other Python programs. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 92 | It comes with a large collection of standard modules that you can use as |
| 93 | the basis for your programs --- or as examples to start learning to |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 94 | program in Python. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 95 | There are also built-in modules that provide things like file I/O, |
| 96 | system calls, and even a generic interface to window systems (STDWIN). |
| 97 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 98 | Python is an interpreted language, which saves you considerable time |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 99 | during program development because no compilation and linking is |
| 100 | necessary. |
| 101 | The interpreter can be used interactively, which makes it easy to |
| 102 | experiment with features of the language, to write throw-away programs, |
| 103 | or to test functions during bottom-up program development. |
| 104 | It is also a handy desk calculator. |
| 105 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 106 | Python allows writing very compact and readable programs. |
| 107 | Programs written in Python are typically much shorter than equivalent C |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 108 | programs: |
| 109 | No declarations are necessary (all type checking is |
| 110 | dynamic); statement grouping is done by indentation instead of begin/end |
| 111 | brackets; and the high-level data types allow you to express complex |
| 112 | operations in a single statement. |
| 113 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 114 | Python is |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 115 | {\em extensible}: |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 116 | if you know how to program in C it is easy to add a new built-in module |
| 117 | to the interpreter, either to perform critical operations at maximum |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 118 | speed, or to link Python programs to libraries that may be only available |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 119 | in binary form (such as a vendor-specific graphics library). |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 120 | Once you are really hooked, you can link the Python interpreter into an |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 121 | application written in C and use it as an extension or command language. |
| 122 | |
| 123 | \subsection{Where From Here} |
| 124 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 125 | Now that you are all excited about Python, you'll want to examine it in |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 126 | some more detail. |
| 127 | Since the best introduction to a language is using it, you are invited |
| 128 | here to do so. |
| 129 | |
| 130 | In the next section, the mechanics of using the interpreter are |
| 131 | explained. |
| 132 | This is rather mundane information, but essential for trying out the |
| 133 | examples shown later. |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 134 | The rest of the tutorial introduces various features of the Python |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 135 | language and system though examples, beginning with simple expressions, |
| 136 | statements and data types, through functions and modules, and finally |
| 137 | touching upon advanced concepts like exceptions and classes. |
| 138 | |
| 139 | \section{Using the Python Interpreter} |
| 140 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 141 | The Python interpreter is usually installed as |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 142 | {\tt /usr/local/python} |
| 143 | on those machines where it is available; putting |
| 144 | {\tt /usr/local} |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 145 | in your {\UNIX} shell's search path makes it possible to start it by |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 146 | typing the command |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 147 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 148 | python |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 149 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 150 | to the shell. |
| 151 | Since the choice of the directory where the interpreter lives is an |
| 152 | installation option, other places instead of |
| 153 | {\tt /usr/local} |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 154 | are possible; check with your local Python guru or system |
Guido van Rossum | 67fa160 | 1991-04-23 14:14:57 +0000 | [diff] [blame] | 155 | administrator. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 157 | The interpreter operates somewhat like the {\UNIX} shell: when called with |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 158 | standard input connected to a tty device, it reads and executes commands |
| 159 | interactively; when called with a file name argument or with a file as |
| 160 | standard input, it reads and executes a |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 161 | {\em script} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 162 | from that file.% |
| 163 | \footnote{ |
| 164 | There is a difference between ``{\tt python file}'' and |
| 165 | ``{\tt python $<$file}''. In the latter case {\tt input()} and |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 166 | {\tt raw\_input()} are satisfied from {\em file}, which has |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 167 | already been read until the end by the parser, so they will read |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 168 | EOF immediately. In the former case (which is usually what |
| 169 | you want) they are satisfied from whatever file or device is |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 170 | connected to standard input of the Python interpreter. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 171 | } |
Guido van Rossum | dd01080 | 1991-06-07 14:31:11 +0000 | [diff] [blame] | 172 | A third possibility is ``{\tt python -c command [arg] ...}'', which |
| 173 | executes the statement(s) in {\tt command}, in analogy of the shell's |
| 174 | {\tt -c} option. |
| 175 | When available, the script name and additional arguments thereafter are |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 176 | passed to the script in the variable |
| 177 | {\tt sys.argv}, |
| 178 | which is a list of strings. |
Guido van Rossum | dd01080 | 1991-06-07 14:31:11 +0000 | [diff] [blame] | 179 | When {\tt -c command} is used, {\tt sys.argv} is set to {\tt '-c'}. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 180 | |
Guido van Rossum | dd01080 | 1991-06-07 14:31:11 +0000 | [diff] [blame] | 181 | When commands are read from a tty, the interpreter is said to be in |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 182 | {\em interactive\ mode}. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 183 | In this mode it prompts for the next command with the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 184 | {\em primary\ prompt}, |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 185 | usually three greater-than signs ({\tt >>>}); for continuation lines |
| 186 | it prompts with the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 187 | {\em secondary\ prompt}, |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 188 | by default three dots ({\tt ...}). |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 189 | Typing an EOF (Control-D) at the primary prompt causes the interpreter |
| 190 | to exit with a zero exit status. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 191 | |
| 192 | When an error occurs in interactive mode, the interpreter prints a |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 193 | message and a stack trace and returns to the primary prompt; with input |
| 194 | from a file, it exits with a nonzero exit status. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 195 | (Exceptions handled by an |
| 196 | {\tt except} |
| 197 | clause in a |
| 198 | {\tt try} |
| 199 | statement are not errors in this context.) |
| 200 | Some errors are unconditionally fatal and cause an exit with a nonzero |
| 201 | exit; this applies to internal inconsistencies and some cases of running |
| 202 | out of memory. |
| 203 | All error messages are written to the standard error stream; normal |
| 204 | output from the executed commands is written to standard output. |
| 205 | |
| 206 | Typing an interrupt (normally Control-C or DEL) to the primary or |
| 207 | secondary prompt cancels the input and returns to the primary prompt. |
| 208 | Typing an interrupt while a command is being executed raises the |
| 209 | {\tt KeyboardInterrupt} |
| 210 | exception, which may be handled by a |
| 211 | {\tt try} |
| 212 | statement. |
| 213 | |
| 214 | When a module named |
| 215 | {\tt foo} |
| 216 | is imported, the interpreter searches for a file named |
| 217 | {\tt foo.py} |
| 218 | in a list of directories specified by the environment variable |
| 219 | {\tt PYTHONPATH}. |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 220 | It has the same syntax as the {\UNIX} shell variable |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 221 | {\tt PATH}, |
| 222 | i.e., a list of colon-separated directory names. |
| 223 | When |
| 224 | {\tt PYTHONPATH} |
| 225 | is not set, an installation-dependent default path is used, usually |
| 226 | {\tt .:/usr/local/lib/python}.% |
| 227 | \footnote{ |
| 228 | Modules are really searched in the list of directories given by |
| 229 | the variable {\tt sys.path} which is initialized from |
| 230 | {\tt PYTHONPATH} or from the installation-dependent default. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 231 | See the section on Standard Modules later. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 232 | } |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 234 | As an important speed-up of the start-up time of short programs, if a |
| 235 | file called {\tt foo.pyc} exists in the directory where {\tt foo.py} |
| 236 | is found, this is assumed to contain an already-``compiled'' version |
| 237 | of the module {\tt foo}. The last modification time of {\tt foo.py} |
| 238 | is recorded in {\tt foo.pyc}, and if these don't match, {\tt foo.pyc} |
| 239 | is ignored. Whenever {\tt foo.py} is successfully compiled, an |
| 240 | attempt is made to write the compiled version to {\tt foo.pyc}. |
| 241 | |
| 242 | On BSD'ish {\UNIX} systems, Python scripts can be made directly executable, |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 243 | like shell scripts, by putting the line |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 244 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 245 | #! /usr/local/python |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 246 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 247 | (assuming that's the name of the interpreter) at the beginning of the |
| 248 | script and giving the file an executable mode. |
| 249 | (The |
| 250 | {\tt \#!} |
| 251 | must be the first two characters of the file.) |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 252 | |
| 253 | \subsection{Interactive Input Editing and History Substitution} |
| 254 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 255 | Some versions of the Python interpreter support editing of the current |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 256 | input line and history substitution, similar to facilities found in the |
| 257 | Korn shell and the GNU Bash shell. |
| 258 | This is implemented using the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 259 | {\em GNU\ Readline} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 260 | library, which supports Emacs-style and vi-style editing. |
| 261 | This library has its own documentation which I won't duplicate here; |
| 262 | however, the basics are easily explained. |
| 263 | |
| 264 | If supported,% |
| 265 | \footnote{ |
| 266 | Perhaps the quickest check to see whether command line editing |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 267 | is supported is typing Control-P to the first Python prompt |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 268 | you get. If it beeps, you have command line editing. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 269 | If not, you can skip the rest of this section. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 270 | } |
| 271 | input line editing is active whenever the interpreter prints a primary |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 272 | or secondary prompt. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 273 | The current line can be edited using the conventional Emacs control |
| 274 | characters. |
| 275 | The most important of these are: |
| 276 | C-A (Control-A) moves the cursor to the beginning of the line, C-E to |
| 277 | the end, C-B moves it one position to the left, C-F to the right. |
| 278 | Backspace erases the character to the left of the cursor, C-D the |
| 279 | character to its right. |
| 280 | C-K kills (erases) the rest of the line to the right of the cursor, C-Y |
| 281 | yanks back the last killed string. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 282 | C-underscore undoes the last change you made; it can be repeated for |
| 283 | cumulative effect. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 284 | |
| 285 | History substitution works as follows. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 286 | All non-empty input lines issued are saved in a history buffer, |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 287 | and when a new prompt is given you are positioned on a new line at the |
| 288 | bottom of this buffer. |
| 289 | C-P moves one line up (back) in the history buffer, C-N moves one down. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 290 | Any line in the history buffer can be edited; an asterisk appears in |
| 291 | front of the prompt to mark a line as modified. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 292 | Pressing the Return key passes the current line to the interpreter. |
| 293 | C-R starts an incremental reverse search; C-S starts a forward search. |
| 294 | |
| 295 | The key bindings and some other parameters of the Readline library can |
| 296 | be customized by placing commands in an initialization file called |
| 297 | {\tt \$HOME/.initrc}. |
| 298 | Key bindings have the form |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 299 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 300 | key-name: function-name |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 301 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 302 | and options can be set with |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 303 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 304 | set option-name value |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 305 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 306 | Example: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 307 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 308 | # I prefer vi-style editing: |
| 309 | set editing-mode vi |
| 310 | # Edit using a single line: |
| 311 | set horizontal-scroll-mode On |
| 312 | # Rebind some keys: |
| 313 | Meta-h: backward-kill-word |
| 314 | Control-u: universal-argument |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 315 | \end{verbatim}\ecode |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 316 | Note that the default binding for TAB in Python is to insert a TAB |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 317 | instead of Readline's default filename completion function. |
| 318 | If you insist, you can override this by putting |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 319 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 320 | TAB: complete |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 321 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 322 | in your |
| 323 | {\tt \$HOME/.inputrc}. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 324 | (Of course, this makes it hard to type indented continuation lines.) |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 325 | |
| 326 | This facility is an enormous step forward compared to previous versions of |
| 327 | the interpreter; however, some wishes are left: |
| 328 | It would be nice if the proper indentation were suggested on |
| 329 | continuation lines (the parser knows if an indent token is required |
| 330 | next). |
| 331 | The completion mechanism might use the interpreter's symbol table. |
| 332 | A function to check (or even suggest) matching parentheses, quotes |
| 333 | etc. would also be useful. |
| 334 | |
| 335 | \section{An Informal Introduction to Python} |
| 336 | |
| 337 | In the following examples, input and output are distinguished by the |
| 338 | presence or absence of prompts ({\tt >>>} and {\tt ...}): to repeat the |
| 339 | example, you must type everything after the prompt, when the prompt |
| 340 | appears; everything on lines that do not begin with a prompt is output |
| 341 | from the interpreter. |
| 342 | Note that a secondary prompt on a line by itself in an example means you |
| 343 | must type a blank line; this is used to end a multi-line command. |
| 344 | |
| 345 | \subsection{Using Python as a Calculator} |
| 346 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 347 | Let's try some simple Python commands. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 348 | Start the interpreter and wait for the primary prompt, |
| 349 | {\tt >>>}. |
| 350 | The interpreter acts as a simple calculator: you can type an expression |
| 351 | at it and it will write the value. |
| 352 | Expression syntax is straightforward: the operators |
| 353 | {\tt +}, |
| 354 | {\tt -}, |
| 355 | {\tt *} |
| 356 | and |
| 357 | {\tt /} |
| 358 | work just as in most other languages (e.g., Pascal or C); parentheses |
| 359 | can be used for grouping. |
| 360 | For example: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 361 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 362 | >>> # This is a comment |
| 363 | >>> 2+2 |
| 364 | 4 |
| 365 | >>> |
| 366 | >>> (50-5+5*6+25)/4 |
| 367 | 25 |
| 368 | >>> # Division truncates towards zero: |
| 369 | >>> 7/3 |
| 370 | 2 |
| 371 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 372 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 373 | As in C, the equal sign ({\tt =}) is used to assign a value to a variable. |
| 374 | The value of an assignment is not written: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 375 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 376 | >>> width = 20 |
| 377 | >>> height = 5*9 |
| 378 | >>> width * height |
| 379 | 900 |
| 380 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 381 | \end{verbatim}\ecode |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 382 | There is some support for floating point, but you can't mix floating |
| 383 | point and integral numbers in expression (yet): |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 384 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 385 | >>> 10.0 / 3.3 |
| 386 | 3.0303030303 |
| 387 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 388 | \end{verbatim}\ecode |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 389 | Besides numbers, Python can also manipulate strings, enclosed in single |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 390 | quotes: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 391 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 392 | >>> 'foo bar' |
| 393 | 'foo bar' |
| 394 | >>> 'doesn\'t' |
| 395 | 'doesn\'t' |
| 396 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 397 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 398 | Strings are written inside quotes and with quotes and other funny |
| 399 | characters escaped by backslashes, to show the precise value. |
| 400 | (There is also a way to write strings without quotes and escapes.) |
| 401 | Strings can be concatenated (glued together) with the |
| 402 | {\tt +} |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 403 | operator, and repeated with~{\tt *}: |
| 404 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 405 | >>> word = 'Help' + 'A' |
| 406 | >>> word |
| 407 | 'HelpA' |
| 408 | >>> '<' + word*5 + '>' |
| 409 | '<HelpAHelpAHelpAHelpAHelpA>' |
| 410 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 411 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 412 | Strings can be subscripted; as in C, the first character of a string has |
| 413 | subscript 0. |
| 414 | There is no separate character type; a character is simply a string of |
| 415 | size one. |
| 416 | As in Icon, substrings can be specified with the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 417 | {\em slice} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 418 | notation: two subscripts (indices) separated by a colon. |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 419 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 420 | >>> word[4] |
| 421 | 'A' |
| 422 | >>> word[0:2] |
| 423 | 'He' |
| 424 | >>> word[2:4] |
| 425 | 'lp' |
| 426 | >>> # Slice indices have useful defaults: |
| 427 | >>> word[:2] # Take first two characters |
| 428 | 'He' |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 429 | >>> word[2:] # Drop first two characters |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 430 | 'lpA' |
| 431 | >>> # A useful invariant: s[:i] + s[i:] = s |
| 432 | >>> word[:3] + word[3:] |
| 433 | 'HelpA' |
| 434 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 435 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 436 | Degenerate cases are handled gracefully: an index that is too large is |
| 437 | replaced by the string size, an upper bound smaller than the lower bound |
| 438 | returns an empty string. |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 439 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 440 | >>> word[1:100] |
| 441 | 'elpA' |
| 442 | >>> word[10:] |
| 443 | '' |
| 444 | >>> word[2:1] |
| 445 | '' |
| 446 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 447 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 448 | Slice indices (but not simple subscripts) may be negative numbers, to |
| 449 | start counting from the right. |
| 450 | For example: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 451 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 452 | >>> word[-2:] # Take last two characters |
| 453 | 'pA' |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 454 | >>> word[:-2] # Drop last two characters |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 455 | 'Hel' |
| 456 | >>> # But -0 does not count from the right! |
| 457 | >>> word[-0:] # (since -0 equals 0) |
| 458 | 'HelpA' |
| 459 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 460 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 461 | The best way to remember how slices work is to think of the indices as |
| 462 | pointing |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 463 | {\em between} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 464 | characters, with the left edge of the first character numbered 0. |
| 465 | Then the right edge of the last character of a string of |
| 466 | {\tt n} |
| 467 | characters has index |
| 468 | {\tt n}, |
| 469 | for example: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 470 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 471 | +---+---+---+---+---+ |
| 472 | | H | e | l | p | A | |
| 473 | +---+---+---+---+---+ |
| 474 | 0 1 2 3 4 5 |
| 475 | -5 -4 -3 -2 -1 |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 476 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 477 | The first row of numbers gives the position of the indices 0...5 in the |
| 478 | string; the second row gives the corresponding negative indices. |
| 479 | For nonnegative indices, the length of a slice is the difference of the |
| 480 | indices, if both are within bounds, |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 481 | e.g., |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 482 | the length of |
| 483 | {\tt word[1:3]} |
| 484 | is 3--1 = 2. |
| 485 | |
| 486 | Finally, the built-in function {\tt len()} computes the length of a |
| 487 | string: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 488 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 489 | >>> s = 'supercalifragilisticexpialidocious' |
| 490 | >>> len(s) |
| 491 | 34 |
| 492 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 493 | \end{verbatim}\ecode |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 494 | Python knows a number of |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 495 | {\em compound} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 496 | data types, used to group together other values. |
| 497 | The most versatile is the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 498 | {\em list}, |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 499 | which can be written as a list of comma-separated values between square |
| 500 | brackets: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 501 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 502 | >>> a = ['foo', 'bar', 100, 1234] |
| 503 | >>> a |
| 504 | ['foo', 'bar', 100, 1234] |
| 505 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 506 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 507 | As for strings, list subscripts start at 0: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 508 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 509 | >>> a[0] |
| 510 | 'foo' |
| 511 | >>> a[3] |
| 512 | 1234 |
| 513 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 514 | \end{verbatim}\ecode |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 515 | Lists can be sliced, concatenated and so on, like strings: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 516 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 517 | >>> a[1:3] |
| 518 | ['bar', 100] |
| 519 | >>> a[:2] + ['bletch', 2*2] |
| 520 | ['foo', 'bar', 'bletch', 4] |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 521 | >>> 3*a[:3] + ['Boe!'] |
| 522 | ['foo', 'bar', 100, 'foo', 'bar', 100, 'foo', 'bar', 100, 'Boe!'] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 523 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 524 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 525 | Unlike strings, which are |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 526 | {\em immutable}, |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 527 | it is possible to change individual elements of a list: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 528 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 529 | >>> a |
| 530 | ['foo', 'bar', 100, 1234] |
| 531 | >>> a[2] = a[2] + 23 |
| 532 | >>> a |
| 533 | ['foo', 'bar', 123, 1234] |
| 534 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 535 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 536 | Assignment to slices is also possible, and this may even change the size |
| 537 | of the list: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 538 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 539 | >>> # Replace some items: |
| 540 | >>> a[0:2] = [1, 12] |
| 541 | >>> a |
| 542 | [1, 12, 123, 1234] |
| 543 | >>> # Remove some: |
| 544 | >>> a[0:2] = [] |
| 545 | >>> a |
| 546 | [123, 1234] |
| 547 | >>> # Insert some: |
| 548 | >>> a[1:1] = ['bletch', 'xyzzy'] |
| 549 | >>> a |
| 550 | [123, 'bletch', 'xyzzy', 1234] |
| 551 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 552 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 553 | The built-in function {\tt len()} also applies to lists: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 554 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 555 | >>> len(a) |
| 556 | 4 |
| 557 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 558 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 559 | |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 560 | \subsection{Tuples and Sequences} |
| 561 | |
| 562 | XXX To Be Done. |
| 563 | |
| 564 | \subsection{First Steps Towards Programming} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 565 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 566 | Of course, we can use Python for more complicated tasks than adding two |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 567 | and two together. |
| 568 | For instance, we can write an initial subsequence of the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 569 | {\em Fibonacci} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 570 | series as follows: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 571 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 572 | >>> # Fibonacci series: |
| 573 | >>> # the sum of two elements defines the next |
| 574 | >>> a, b = 0, 1 |
| 575 | >>> while b < 100: |
| 576 | ... print b |
| 577 | ... a, b = b, a+b |
| 578 | ... |
| 579 | 1 |
| 580 | 1 |
| 581 | 2 |
| 582 | 3 |
| 583 | 5 |
| 584 | 8 |
| 585 | 13 |
| 586 | 21 |
| 587 | 34 |
| 588 | 55 |
| 589 | 89 |
| 590 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 591 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 592 | This example introduces several new features. |
| 593 | \begin{itemize} |
| 594 | \item |
| 595 | The first line contains a |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 596 | {\em multiple\ assignment}: |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 597 | the variables |
| 598 | {\tt a} |
| 599 | and |
| 600 | {\tt b} |
| 601 | simultaneously get the new values 0 and 1. |
| 602 | On the last line this is used again, demonstrating that the expressions |
| 603 | on the right-hand side are all evaluated first before any of the |
| 604 | assignments take place. |
| 605 | \item |
| 606 | The |
| 607 | {\tt while} |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 608 | loop executes as long as the condition (here: $b < 100$) remains true. |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 609 | In Python, as in C, any non-zero integer value is true; zero is false. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 610 | The condition may also be a string or list value, in fact any sequence; |
| 611 | anything with a non-zero length is true, empty sequences are false. |
| 612 | The test used in the example is a simple comparison. |
| 613 | The standard comparison operators are written as |
| 614 | {\tt <}, |
| 615 | {\tt >}, |
| 616 | {\tt =}, |
| 617 | {\tt <=}, |
| 618 | {\tt >=} |
| 619 | and |
| 620 | {\tt <>}.% |
| 621 | \footnote{ |
| 622 | The ambiguity of using {\tt =} |
| 623 | for both assignment and equality is resolved by disallowing |
| 624 | unparenthesized conditions at the right hand side of assignments. |
| 625 | } |
| 626 | \item |
| 627 | The |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 628 | {\em body} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 629 | of the loop is |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 630 | {\em indented}: indentation is Python's way of grouping statements. |
| 631 | Python does not (yet!) provide an intelligent input line editing |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 632 | facility, so you have to type a tab or space(s) for each indented line. |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 633 | In practice you will prepare more complicated input for Python with a |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 634 | text editor; most text editors have an auto-indent facility. |
| 635 | When a compound statement is entered interactively, it must be |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 636 | followed by a blank line to indicate completion (since the parser |
| 637 | cannot guess when you have typed the last line). |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 638 | \item |
| 639 | The |
| 640 | {\tt print} |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 641 | statement writes the value of the expression(s) it is given. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 642 | It differs from just writing the expression you want to write (as we did |
| 643 | earlier in the calculator examples) in the way it handles multiple |
| 644 | expressions and strings. |
| 645 | Strings are written without quotes and a space is inserted between |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 646 | items, so you can format things nicely, like this: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 647 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 648 | >>> i = 256*256 |
| 649 | >>> print 'The value of i is', i |
| 650 | The value of i is 65536 |
| 651 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 652 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 653 | A trailing comma avoids the newline after the output: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 654 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 655 | >>> a, b = 0, 1 |
| 656 | >>> while b < 1000: |
| 657 | ... print b, |
| 658 | ... a, b = b, a+b |
| 659 | ... |
| 660 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 |
| 661 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 662 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 663 | Note that the interpreter inserts a newline before it prints the next |
| 664 | prompt if the last line was not completed. |
| 665 | \end{itemize} |
| 666 | |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 667 | \subsection{More Control Flow Tools} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 668 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 669 | Besides the {\tt while} statement just introduced, Python knows the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 670 | usual control flow statements known from other languages, with some |
| 671 | twists. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 672 | |
| 673 | \subsubsection{If Statements} |
| 674 | |
| 675 | Perhaps the most well-known statement type is the {\tt if} statement. |
| 676 | For example: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 677 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 678 | >>> if x < 0: |
| 679 | ... x = 0 |
| 680 | ... print 'Negative changed to zero' |
| 681 | ... elif x = 0: |
| 682 | ... print 'Zero' |
| 683 | ... elif x = 1: |
| 684 | ... print 'Single' |
| 685 | ... else: |
| 686 | ... print 'More' |
| 687 | ... |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 688 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 689 | There can be zero or more {\tt elif} parts, and the {\tt else} part is |
| 690 | optional. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 691 | The keyword `{\tt elif}' is short for `{\tt else if}', and is useful to |
| 692 | avoid excessive indentation. |
| 693 | An {\tt if...elif...elif...} sequence is a substitute for the |
| 694 | {\em switch} or {\em case} statements found in other languages. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 695 | |
| 696 | \subsubsection{For Statements} |
| 697 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 698 | The {\tt for} statement in Python differs a bit from what you may be |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 699 | used to in C or Pascal. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 700 | Rather than always iterating over an arithmetic progression of numbers |
| 701 | (as Pascal), or leaving the user completely free in the iteration test |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 702 | and step (as C), Python's {\tt for} statement iterates over the items |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 703 | of any sequence (e.g., a list or a string). |
| 704 | For example (no pun intended): |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 705 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 706 | >>> # Measure some strings: |
| 707 | >>> a = ['cat', 'window', 'defenestrate'] |
| 708 | >>> for x in a: |
| 709 | ... print x, len(x) |
| 710 | ... |
| 711 | cat 3 |
| 712 | window 6 |
| 713 | defenestrate 12 |
| 714 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 715 | \end{verbatim}\ecode |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 716 | |
| 717 | \subsubsection{The {\tt range()} Function} |
| 718 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 719 | If you do need to iterate over a sequence of numbers, the built-in |
| 720 | function {\tt range()} comes in handy. |
| 721 | It generates lists containing arithmetic progressions, |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 722 | e.g.: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 723 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 724 | >>> range(10) |
| 725 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
| 726 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 727 | \end{verbatim}\ecode |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 728 | The given end point is never part of the generated list; |
| 729 | {\tt range(10)} generates a list of 10 values, |
| 730 | exactly the legal indices for items of a sequence of length 10. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 731 | It is possible to let the range start at another number, or to specify a |
| 732 | different increment (even negative): |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 733 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 734 | >>> range(5, 10) |
| 735 | [5, 6, 7, 8, 9] |
| 736 | >>> range(0, 10, 3) |
| 737 | [0, 3, 6, 9] |
| 738 | >>> range(-10, -100, -30) |
| 739 | [-10, -40, -70] |
| 740 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 741 | \end{verbatim}\ecode |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 742 | To iterate over the indices of a sequence, combine {\tt range()} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 743 | and {\tt len()} as follows: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 744 | \bcode\begin{verbatim} |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 745 | >>> a = ['Mary', 'had', 'a', 'little', 'boy'] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 746 | >>> for i in range(len(a)): |
| 747 | ... print i, a[i] |
| 748 | ... |
| 749 | 0 Mary |
| 750 | 1 had |
| 751 | 2 a |
| 752 | 3 little |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 753 | 4 boy |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 754 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 755 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 756 | |
| 757 | \subsubsection{Break Statements and Else Clauses on Loops} |
| 758 | |
| 759 | The {\tt break} statement breaks out of the smallest enclosing {\tt for} |
| 760 | or {\tt while} loop. |
| 761 | Loop statements may have an {\tt else} clause; it is executed when the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 762 | loop terminates through exhaustion of the list (with {\tt for}) or when |
| 763 | the condition becomes false (with {\tt while}) but not when the loop is |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 764 | terminated by a {\tt break} statement. |
| 765 | This is exemplified by the following loop, which searches for a list |
| 766 | item of value 0: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 767 | \bcode\begin{verbatim} |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 768 | >>> for n in range(2, 10): |
| 769 | ... for x in range(2, n): |
| 770 | ... if n % x = 0: |
| 771 | ... print n, 'equals', x, '*', n/x |
| 772 | ... break |
| 773 | ... else: |
| 774 | ... print n, 'is a prime number' |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 775 | ... |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 776 | 2 is a prime number |
| 777 | 3 is a prime number |
| 778 | 4 equals 2 * 2 |
| 779 | 5 is a prime number |
| 780 | 6 equals 2 * 3 |
| 781 | 7 is a prime number |
| 782 | 8 equals 2 * 4 |
| 783 | 9 equals 3 * 3 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 784 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 785 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 786 | |
| 787 | \subsubsection{Pass Statements} |
| 788 | |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 789 | The {\tt pass} statement does nothing. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 790 | It can be used when a statement is required syntactically but the |
| 791 | program requires no action. |
| 792 | For example: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 793 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 794 | >>> while 1: |
| 795 | ... pass # Busy-wait for keyboard interrupt |
| 796 | ... |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 797 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 798 | |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 799 | \subsubsection{Conditions Revisited} |
| 800 | |
| 801 | XXX To Be Done. |
| 802 | |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 803 | \subsection{Defining Functions} |
| 804 | |
| 805 | We can create a function that writes the Fibonacci series to an |
| 806 | arbitrary boundary: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 807 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 808 | >>> def fib(n): # write Fibonacci series up to n |
| 809 | ... a, b = 0, 1 |
| 810 | ... while b <= n: |
| 811 | ... print b, |
| 812 | ... a, b = b, a+b |
| 813 | ... |
| 814 | >>> # Now call the function we just defined: |
| 815 | >>> fib(2000) |
| 816 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 |
| 817 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 818 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 819 | The keyword |
| 820 | {\tt def} |
| 821 | introduces a function |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 822 | {\em definition}. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 823 | It must be followed by the function name and the parenthesized list of |
| 824 | formal parameters. |
| 825 | The statements that form the body of the function starts at the next |
| 826 | line, indented by a tab stop. |
| 827 | The |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 828 | {\em execution} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 829 | of a function introduces a new symbol table used for the local variables |
| 830 | of the function. |
| 831 | More precisely, all variable assignments in a function store the value |
| 832 | in the local symbol table; variable references first look in the local |
| 833 | symbol table, then in the global symbol table, and then in the table of |
| 834 | built-in names. |
| 835 | Thus, the global symbol table is |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 836 | {\em read-only} |
| 837 | within a function. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 838 | The actual parameters (arguments) to a function call are introduced in |
| 839 | the local symbol table of the called function when it is called; |
| 840 | thus, arguments are passed using |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 841 | {\em call\ by\ value}.% |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 842 | \footnote{ |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 843 | Actually, {\em call by object reference} would be a better |
| 844 | description, since if a mutable object is passed, the caller |
| 845 | will see any changes the callee makes to it (e.g., items |
| 846 | inserted into a list). |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 847 | } |
| 848 | When a function calls another function, a new local symbol table is |
| 849 | created for that call. |
| 850 | |
| 851 | A function definition introduces the function name in the global symbol |
| 852 | table. |
| 853 | The value has a type that is recognized by the interpreter as a |
| 854 | user-defined function. |
| 855 | This value can be assigned to another name which can then also be used |
| 856 | as a function. |
| 857 | This serves as a general renaming mechanism: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 858 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 859 | >>> fib |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 860 | <function object at 10042ed0> |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 861 | >>> f = fib |
| 862 | >>> f(100) |
| 863 | 1 1 2 3 5 8 13 21 34 55 89 |
| 864 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 865 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 866 | You might object that |
| 867 | {\tt fib} |
| 868 | is not a function but a procedure. |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 869 | In Python, as in C, procedures are just functions that don't return a |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 870 | value. |
| 871 | In fact, technically speaking, procedures do return a value, albeit a |
| 872 | rather boring one. |
| 873 | This value is called {\tt None} (it's a built-in name). |
| 874 | Writing the value {\tt None} is normally suppressed by the interpreter |
| 875 | if it would be the only value written. |
| 876 | You can see it if you really want to: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 877 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 878 | >>> print fib(0) |
| 879 | None |
| 880 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 881 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 882 | It is simple to write a function that returns a list of the numbers of |
| 883 | the Fibonacci series, instead of printing it: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 884 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 885 | >>> def fib2(n): # return Fibonacci series up to n |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 886 | ... result = [] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 887 | ... a, b = 0, 1 |
| 888 | ... while b <= n: |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 889 | ... result.append(b) # see below |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 890 | ... a, b = b, a+b |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 891 | ... return result |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 892 | ... |
| 893 | >>> f100 = fib2(100) # call it |
| 894 | >>> f100 # write the result |
| 895 | [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |
| 896 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 897 | \end{verbatim}\ecode |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 898 | This example, as usual, demonstrates some new Python features: |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 899 | \begin{itemize} |
| 900 | \item |
| 901 | The |
| 902 | {\tt return} |
| 903 | statement returns with a value from a function. |
| 904 | {\tt return} |
| 905 | without an expression argument is used to return from the middle of a |
| 906 | procedure (falling off the end also returns from a proceduce). |
| 907 | \item |
| 908 | The statement |
| 909 | {\tt ret.append(b)} |
| 910 | calls a |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 911 | {\em method} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 912 | of the list object |
| 913 | {\tt ret}. |
| 914 | A method is a function that `belongs' to an object and is named |
| 915 | {\tt obj.methodname}, |
| 916 | where |
| 917 | {\tt obj} |
| 918 | is some object (this may be an expression), and |
| 919 | {\tt methodname} |
| 920 | is the name of a method that is defined by the object's type. |
| 921 | Different types define different methods. |
| 922 | Methods of different types may have the same name without causing |
| 923 | ambiguity. |
| 924 | See the section on classes, later, to find out how you can define your |
| 925 | own object types and methods. |
| 926 | The method |
| 927 | {\tt append} |
| 928 | shown in the example, is defined for list objects; it adds a new element |
| 929 | at the end of the list. |
| 930 | In this case it is equivalent to |
| 931 | {\tt ret = ret + [b]}, |
| 932 | but more efficient.% |
| 933 | \footnote{ |
| 934 | There is a subtle semantic difference if the object |
| 935 | is referenced from more than one place. |
| 936 | } |
| 937 | \end{itemize} |
| 938 | The list object type has two more methods: |
Guido van Rossum | 7d9f8d7 | 1991-01-22 11:45:00 +0000 | [diff] [blame] | 939 | \begin{description} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 940 | \item[{\tt insert(i, x)}] |
| 941 | Inserts an item at a given position. |
| 942 | The first argument is the index of the element before which to insert, |
| 943 | so {\tt a.insert(0, x)} inserts at the front of the list, and |
| 944 | {\tt a.insert(len(a), x)} is equivalent to {\tt a.append(x)}. |
| 945 | \item[{\tt sort()}] |
| 946 | Sorts the elements of the list. |
Guido van Rossum | 7d9f8d7 | 1991-01-22 11:45:00 +0000 | [diff] [blame] | 947 | \end{description} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 948 | For example: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 949 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 950 | >>> a = [10, 100, 1, 1000] |
| 951 | >>> a.insert(2, -1) |
| 952 | >>> a |
| 953 | [10, 100, -1, 1, 1000] |
| 954 | >>> a.sort() |
| 955 | >>> a |
| 956 | [-1, 1, 10, 100, 1000] |
| 957 | >>> # Strings are sorted according to ASCII: |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 958 | >>> b = ['Mary', 'had', 'a', 'little', 'boy'] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 959 | >>> b.sort() |
| 960 | >>> b |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 961 | ['Mary', 'a', 'boy', 'had', 'little'] |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 962 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 963 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 964 | |
| 965 | \subsection{Modules} |
| 966 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 967 | If you quit from the Python interpreter and enter it again, the |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 968 | definitions you have made (functions and variables) are lost. |
| 969 | Therefore, if you want to write a somewhat longer program, you are |
| 970 | better off using a text editor to prepare the input for the interpreter |
| 971 | and run it with that file as input instead. |
| 972 | This is known as creating a |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 973 | {\em script}. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 974 | As your program gets longer, you may want to split it into several files |
| 975 | for easier maintenance. |
| 976 | You may also want to use a handy function that you've written in several |
| 977 | programs without copying its definition into each program. |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 978 | To support this, Python has a way to put definitions in a file and use |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 979 | them in a script or in an interactive instance of the interpreter. |
| 980 | Such a file is called a |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 981 | {\em module}; |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 982 | definitions from a module can be |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 983 | {\em imported} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 984 | into other modules or into the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 985 | {\em main} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 986 | module (the collection of variables that you have access to in |
| 987 | a script and in calculator mode). |
| 988 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 989 | A module is a file containing Python definitions and statements. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 990 | The file name is the module name with the suffix |
| 991 | {\tt .py} |
| 992 | appended. |
| 993 | For instance, use your favorite text editor to create a file called |
| 994 | {\tt fibo.py} |
| 995 | in the current directory with the following contents: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 996 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 997 | # Fibonacci numbers module |
| 998 | |
| 999 | def fib(n): # write Fibonacci series up to n |
| 1000 | a, b = 0, 1 |
| 1001 | while b <= n: |
| 1002 | print b, |
| 1003 | a, b = b, a+b |
| 1004 | |
| 1005 | def fib2(n): # return Fibonacci series up to n |
| 1006 | ret = [] |
| 1007 | a, b = 0, 1 |
| 1008 | while b <= n: |
| 1009 | ret.append(b) |
| 1010 | a, b = b, a+b |
| 1011 | return ret |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1012 | \end{verbatim}\ecode |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1013 | Now enter the Python interpreter and import this module with the |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1014 | following command: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1015 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1016 | >>> import fibo |
| 1017 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1018 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1019 | This does not enter the names of the functions defined in |
| 1020 | {\tt fibo} |
| 1021 | directly in the symbol table; it only enters the module name |
| 1022 | {\tt fibo} |
| 1023 | there. |
| 1024 | Using the module name you can access the functions: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1025 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1026 | >>> fibo.fib(1000) |
| 1027 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 |
| 1028 | >>> fibo.fib2(100) |
| 1029 | [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] |
| 1030 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1031 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1032 | If you intend to use a function often you can assign it to a local name: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1033 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1034 | >>> fib = fibo.fib |
| 1035 | >>> fib(500) |
| 1036 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 |
| 1037 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1038 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1039 | |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1040 | \subsubsection{More on Modules} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1041 | |
| 1042 | A module can contain executable statements as well as function |
| 1043 | definitions. |
| 1044 | These statements are intended to initialize the module. |
| 1045 | They are executed only the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1046 | {\em first} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1047 | time the module is imported somewhere.% |
| 1048 | \footnote{ |
| 1049 | In fact function definitions are also `statements' that are |
| 1050 | `executed'; the execution enters the function name in the |
| 1051 | module's global symbol table. |
| 1052 | } |
| 1053 | |
| 1054 | Each module has its own private symbol table, which is used as the |
| 1055 | global symbol table by all functions defined in the module. |
| 1056 | Thus, the author of a module can use global variables in the module |
| 1057 | without worrying about accidental clashes with a user's global |
| 1058 | variables. |
| 1059 | On the other hand, if you know what you are doing you can touch a |
| 1060 | module's global variables with the same notation used to refer to its |
| 1061 | functions, |
| 1062 | {\tt modname.itemname}. |
| 1063 | |
| 1064 | Modules can import other modules. |
| 1065 | It is customary but not required to place all |
| 1066 | {\tt import} |
| 1067 | statements at the beginning of a module (or script, for that matter). |
| 1068 | The imported module names are placed in the importing module's global |
| 1069 | symbol table. |
| 1070 | |
| 1071 | There is a variant of the |
| 1072 | {\tt import} |
| 1073 | statement that imports names from a module directly into the importing |
| 1074 | module's symbol table. |
| 1075 | For example: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1076 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1077 | >>> from fibo import fib, fib2 |
| 1078 | >>> fib(500) |
| 1079 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 |
| 1080 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1081 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1082 | This does not introduce the module name from which the imports are taken |
| 1083 | in the local symbol table (so in the example, {\tt fibo} is not |
| 1084 | defined). |
| 1085 | |
| 1086 | There is even a variant to import all names that a module defines: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1087 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1088 | >>> from fibo import * |
| 1089 | >>> fib(500) |
| 1090 | 1 1 2 3 5 8 13 21 34 55 89 144 233 377 |
| 1091 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1092 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1093 | This imports all names except those beginning with an underscore |
| 1094 | ({\tt \_}). |
| 1095 | |
| 1096 | \subsubsection{Standard Modules} |
| 1097 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1098 | Python comes with a library of standard modules, described in a separate |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1099 | document (Python Library and Module Reference). |
| 1100 | Some modules are built into the interpreter; these provide access to |
| 1101 | operations that are not part of the core of the language but are |
| 1102 | nevertheless built in, either for efficiency or to provide access to |
| 1103 | operating system primitives such as system calls. |
| 1104 | The set of such modules is a configuration option; e.g., the |
| 1105 | {\tt amoeba} |
| 1106 | module is only provided on systems that somehow support Amoeba |
| 1107 | primitives. |
| 1108 | One particular module deserves some attention: |
| 1109 | {\tt sys}, |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1110 | which is built into every Python interpreter. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1111 | The variables |
| 1112 | {\tt sys.ps1} |
| 1113 | and |
| 1114 | {\tt sys.ps2} |
| 1115 | define the strings used as primary and secondary prompts: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1116 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1117 | >>> import sys |
| 1118 | >>> sys.ps1 |
| 1119 | '>>> ' |
| 1120 | >>> sys.ps2 |
| 1121 | '... ' |
| 1122 | >>> sys.ps1 = 'C> ' |
| 1123 | C> print 'Yuck!' |
| 1124 | Yuck! |
| 1125 | C> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1126 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1127 | These two variables are only defined if the interpreter is in |
| 1128 | interactive mode. |
| 1129 | |
| 1130 | The variable |
| 1131 | {\tt sys.path} |
| 1132 | is a list of strings that determine the interpreter's search path for |
| 1133 | modules. |
| 1134 | It is initialized to a default path taken from the environment variable |
| 1135 | {\tt PYTHONPATH}, |
| 1136 | or from a built-in default if |
| 1137 | {\tt PYTHONPATH} |
| 1138 | is not set. |
| 1139 | You can modify it using standard list operations, e.g.: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1140 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1141 | >>> import sys |
| 1142 | >>> sys.path.append('/ufs/guido/lib/python') |
| 1143 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1144 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1145 | |
| 1146 | \subsection{Errors and Exceptions} |
| 1147 | |
| 1148 | Until now error messages haven't yet been mentioned, but if you have |
| 1149 | tried out the examples you have probably seen some. |
| 1150 | There are (at least) two distinguishable kinds of errors: |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1151 | {\em syntax\ errors} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1152 | and |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1153 | {\em exceptions}. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1154 | |
| 1155 | \subsubsection{Syntax Errors} |
| 1156 | |
| 1157 | Syntax errors, also known as parsing errors, are perhaps the most common |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1158 | kind of complaint you get while you are still learning Python: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1159 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1160 | >>> while 1 print 'Hello world' |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1161 | Parsing error: file <stdin>, line 1: |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1162 | while 1 print 'Hello world' |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1163 | ^ |
| 1164 | Unhandled exception: run-time error: syntax error |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1165 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1166 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1167 | The parser repeats the offending line and displays a little `arrow' |
| 1168 | pointing at the earliest point in the line where the error was detected. |
| 1169 | The error is caused by (or at least detected at) the token |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1170 | {\em preceding} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1171 | the arrow: in the example, the error is detected at the keyword |
| 1172 | {\tt print}, since a colon ({\tt :}) is missing before it. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1173 | File name and line number are printed so you know where to look in case |
| 1174 | the input came from a script. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1175 | |
| 1176 | \subsubsection{Exceptions} |
| 1177 | |
| 1178 | Even if a statement or expression is syntactically correct, it may cause |
| 1179 | an error when an attempt is made to execute it: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1180 | \bcode\small\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1181 | >>> 10 * (1/0) |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1182 | Unhandled exception: run-time error: integer division by zero |
| 1183 | Stack backtrace (innermost last): |
| 1184 | File "<stdin>", line 1 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1185 | >>> 4 + foo*3 |
| 1186 | Unhandled exception: undefined name: foo |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1187 | Stack backtrace (innermost last): |
| 1188 | File "<stdin>", line 1 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1189 | >>> '2' + 2 |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1190 | Unhandled exception: type error: illegal argument type for built-in operation |
| 1191 | Stack backtrace (innermost last): |
| 1192 | File "<stdin>", line 1 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1193 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1194 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1195 | Errors detected during execution are called |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1196 | {\em exceptions} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1197 | and are not unconditionally fatal: you will soon learn how to handle |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1198 | them in Python programs. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1199 | Most exceptions are not handled by programs, however, and result |
| 1200 | in error messages as shown here. |
| 1201 | |
| 1202 | The first line of the error message indicates what happened. |
| 1203 | Exceptions come in different types, and the type is printed as part of |
| 1204 | the message: the types in the example are |
| 1205 | {\tt run-time error}, |
| 1206 | {\tt undefined name} |
| 1207 | and |
| 1208 | {\tt type error}. |
| 1209 | The rest of the line is a detail whose interpretation depends on the |
| 1210 | exception type. |
| 1211 | |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1212 | The rest of the error message shows the context where the |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1213 | exception happened. |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1214 | In general it contains a stack backtrace listing source lines; however, |
| 1215 | it will not display lines read from standard input. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1216 | |
| 1217 | Here is a summary of the most common exceptions: |
| 1218 | \begin{itemize} |
| 1219 | \item |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1220 | {\em Run-time\ errors} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1221 | are generally caused by wrong data used by the program; this can be the |
| 1222 | programmer's fault or caused by bad input. |
| 1223 | The detail states the cause of the error in more detail. |
| 1224 | \item |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1225 | {\em Undefined\ name} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1226 | errors are more serious: these are usually caused by misspelled |
| 1227 | identifiers.% |
| 1228 | \footnote{ |
| 1229 | The parser does not check whether names used in a program are at |
| 1230 | all defined elsewhere in the program, so such checks are |
| 1231 | postponed until run-time. The same holds for type checking. |
| 1232 | } |
| 1233 | The detail is the offending identifier. |
| 1234 | \item |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1235 | {\em Type\ errors} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1236 | are also pretty serious: this is another case of using wrong data (or |
| 1237 | better, using data the wrong way), but here the error can be glanced |
| 1238 | from the object type(s) alone. |
| 1239 | The detail shows in what context the error was detected. |
| 1240 | \end{itemize} |
| 1241 | |
| 1242 | \subsubsection{Handling Exceptions} |
| 1243 | |
| 1244 | It is possible to write programs that handle selected exceptions. |
| 1245 | Look at the following example, which prints a table of inverses of |
| 1246 | some floating point numbers: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1247 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1248 | >>> numbers = [0.3333, 2.5, 0.0, 10.0] |
| 1249 | >>> for x in numbers: |
| 1250 | ... print x, |
| 1251 | ... try: |
| 1252 | ... print 1.0 / x |
| 1253 | ... except RuntimeError: |
| 1254 | ... print '*** has no inverse ***' |
| 1255 | ... |
| 1256 | 0.3333 3.00030003 |
| 1257 | 2.5 0.4 |
| 1258 | 0 *** has no inverse *** |
| 1259 | 10 0.1 |
| 1260 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1261 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1262 | The {\tt try} statement works as follows. |
| 1263 | \begin{itemize} |
| 1264 | \item |
| 1265 | First, the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1266 | {\em try\ clause} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1267 | (the statement(s) between the {\tt try} and {\tt except} keywords) is |
| 1268 | executed. |
| 1269 | \item |
| 1270 | If no exception occurs, the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1271 | {\em except\ clause} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1272 | is skipped and execution of the {\tt try} statement is finished. |
| 1273 | \item |
| 1274 | If an exception occurs during execution of the try clause, and its |
| 1275 | type matches the exception named after the {\tt except} keyword, the |
| 1276 | rest of the try clause is skipped, the except clause is executed, and |
| 1277 | then execution continues after the {\tt try} statement. |
| 1278 | \item |
| 1279 | If an exception occurs which does not match the exception named in the |
| 1280 | except clause, it is passed on to outer try statements; if no handler is |
| 1281 | found, it is an |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1282 | {\em unhandled\ exception} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1283 | and execution stops with a message as shown above. |
| 1284 | \end{itemize} |
| 1285 | A {\tt try} statement may have more than one except clause, to specify |
| 1286 | handlers for different exceptions. |
| 1287 | At most one handler will be executed. |
| 1288 | Handlers only handle exceptions that occur in the corresponding try |
| 1289 | clause, not in other handlers of the same {\tt try} statement. |
| 1290 | An except clause may name multiple exceptions as a parenthesized list, |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1291 | e.g.: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1292 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1293 | ... except (RuntimeError, TypeError, NameError): |
| 1294 | ... pass |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1295 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1296 | The last except clause may omit the exception name(s), to serve as a |
| 1297 | wildcard. |
| 1298 | Use this with extreme caution! |
| 1299 | |
| 1300 | When an exception occurs, it may have an associated value, also known as |
| 1301 | the exceptions's |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1302 | {\em argument}. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1303 | The presence and type of the argument depend on the exception type. |
| 1304 | For exception types which have an argument, the except clause may |
| 1305 | specify a variable after the exception name (or list) to receive the |
| 1306 | argument's value, as follows: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1307 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1308 | >>> try: |
| 1309 | ... foo() |
| 1310 | ... except NameError, x: |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1311 | ... print 'name', x, 'undefined' |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1312 | ... |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1313 | name foo undefined |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1314 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1315 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1316 | If an exception has an argument, it is printed as the third part |
| 1317 | (`detail') of the message for unhandled exceptions. |
| 1318 | |
| 1319 | Standard exception names are built-in identifiers (not reserved |
| 1320 | keywords). |
| 1321 | These are in fact string objects whose |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1322 | {\em object\ identity} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1323 | (not their value!) identifies the exceptions.% |
| 1324 | \footnote{ |
| 1325 | There should really be a separate exception type; it is pure |
| 1326 | laziness that exceptions are identified by strings, and this may |
| 1327 | be fixed in the future. |
| 1328 | } |
| 1329 | The string is printed as the second part of the message for unhandled |
| 1330 | exceptions. |
| 1331 | Their names and values are: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1332 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1333 | EOFError 'end-of-file read' |
| 1334 | KeyboardInterrupt 'keyboard interrupt' |
| 1335 | MemoryError 'out of memory' * |
| 1336 | NameError 'undefined name' * |
| 1337 | RuntimeError 'run-time error' * |
| 1338 | SystemError 'system error' * |
| 1339 | TypeError 'type error' * |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1340 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1341 | The meanings should be clear enough. |
| 1342 | Those exceptions with a {\tt *} in the third column have an argument. |
| 1343 | |
| 1344 | Exception handlers don't just handle exceptions if they occur |
| 1345 | immediately in the try clause, but also if they occur inside functions |
| 1346 | that are called (even indirectly) in the try clause. |
| 1347 | For example: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1348 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1349 | >>> def this_fails(): |
| 1350 | ... x = 1/0 |
| 1351 | ... |
| 1352 | >>> try: |
| 1353 | ... this_fails() |
| 1354 | ... except RuntimeError, detail: |
| 1355 | ... print 'Handling run-time error:', detail |
| 1356 | ... |
Guido van Rossum | 67fa160 | 1991-04-23 14:14:57 +0000 | [diff] [blame] | 1357 | Handling run-time error: integer division by zero |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1358 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1359 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1360 | |
| 1361 | \subsubsection{Raising Exceptions} |
| 1362 | |
| 1363 | The {\tt raise} statement allows the programmer to force a specified |
| 1364 | exception to occur. |
| 1365 | For example: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1366 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1367 | >>> raise NameError, 'Hi There!' |
| 1368 | Unhandled exception: undefined name: Hi There! |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1369 | Stack backtrace (innermost last): |
| 1370 | File "<stdin>", line 1 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1371 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1372 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1373 | The first argument to {\tt raise} names the exception to be raised. |
| 1374 | The optional second argument specifies the exception's argument. |
| 1375 | |
| 1376 | \subsubsection{User-defined Exceptions} |
| 1377 | |
| 1378 | Programs may name their own exceptions by assigning a string to a |
| 1379 | variable. |
| 1380 | For example: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1381 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1382 | >>> my_exc = 'nobody likes me!' |
| 1383 | >>> try: |
| 1384 | ... raise my_exc, 2*2 |
| 1385 | ... except my_exc, val: |
Guido van Rossum | 67fa160 | 1991-04-23 14:14:57 +0000 | [diff] [blame] | 1386 | ... print 'My exception occurred, value:', val |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1387 | ... |
| 1388 | My exception occured, value: 4 |
| 1389 | >>> raise my_exc, 1 |
| 1390 | Unhandled exception: nobody likes me!: 1 |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1391 | Stack backtrace (innermost last): |
| 1392 | File "<stdin>", line 7 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1393 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1394 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1395 | Many standard modules use this to report errors that may occur in |
| 1396 | functions they define. |
| 1397 | |
| 1398 | \subsubsection{Defining Clean-up Actions} |
| 1399 | |
| 1400 | The {\tt try} statement has another optional clause which is intended to |
| 1401 | define clean-up actions that must be executed under all circumstances. |
| 1402 | For example: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1403 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1404 | >>> try: |
| 1405 | ... raise KeyboardInterrupt |
| 1406 | ... finally: |
| 1407 | ... print 'Goodbye, world!' |
| 1408 | ... |
| 1409 | Goodbye, world! |
| 1410 | Unhandled exception: keyboard interrupt |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1411 | Stack backtrace (innermost last): |
| 1412 | File "<stdin>", line 2 |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1413 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1414 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1415 | The |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1416 | {\em finally\ clause} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1417 | must follow the except clauses(s), if any. |
| 1418 | It is executed whether or not an exception occurred. |
| 1419 | If the exception is handled, the finally clause is executed after the |
| 1420 | handler (and even if another exception occurred in the handler). |
| 1421 | It is also executed when the {\tt try} statement is left via a |
| 1422 | {\tt break} or {\tt return} statement. |
| 1423 | |
| 1424 | \subsection{Classes} |
| 1425 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1426 | Classes in Python make it possible to play the game of encapsulation in a |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1427 | somewhat different way than it is played with modules. |
| 1428 | Classes are an advanced topic and are probably best skipped on the first |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1429 | encounter with Python. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1430 | |
| 1431 | \subsubsection{Prologue} |
| 1432 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1433 | Python's class mechanism is not particularly elegant, but quite powerful. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1434 | It is a mixture of the class mechanisms found in C++ and Modula-3. |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1435 | As is true for modules, classes in Python do not put an absolute barrier |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1436 | between definition and user, but rather rely on the politeness of the |
| 1437 | user not to ``break into the definition.'' |
| 1438 | The most important features of classes are retained with full power, |
| 1439 | however: the class inheritance mechanism allows multiple base classes, |
| 1440 | a derived class can override any method of its base class(es), a method |
| 1441 | can call the method of a base class with the same name. |
| 1442 | Objects can contain an arbitrary amount of private data. |
| 1443 | |
| 1444 | In C++ terminology, all class members (including data members) are |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1445 | {\em public}, |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1446 | and all member functions (methods) are |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1447 | {\em virtual}. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1448 | There are no special constructors or destructors. |
| 1449 | As in Modula-3, there are no shorthands for referencing the object's |
| 1450 | members from its methods: the method function is declared with an |
| 1451 | explicit first argument representing the object, which is provided |
| 1452 | implicitly by the call. |
| 1453 | As in Smalltalk, classes themselves are objects, albeit in the wider |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1454 | sense of the word: in Python, all data types are objects. |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1455 | This provides semantics for renaming or aliasing. |
| 1456 | But, just like in C++ or Modula-3, the built-in types cannot be used as |
| 1457 | base classes for extension by the user. |
| 1458 | Also, like Modula-3 but unlike C++, the built-in operators with special |
| 1459 | syntax (arithmetic operators, subscripting etc.) cannot be redefined for |
| 1460 | class members.% |
| 1461 | \footnote{ |
| 1462 | They can be redefined for new object types implemented in C in |
| 1463 | extensions to the interpreter, however. It would require only a |
| 1464 | naming convention and a relatively small change to the |
| 1465 | interpreter to allow operator overloading for classes, so |
| 1466 | perhaps someday... |
| 1467 | } |
| 1468 | |
| 1469 | \subsubsection{A Simple Example} |
| 1470 | |
| 1471 | Consider the following example, which defines a class {\tt Set} |
| 1472 | representing a (finite) mathematical set with operations to add and |
| 1473 | remove elements, a membership test, and a request for the size of the |
| 1474 | set. |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1475 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1476 | class Set(): |
| 1477 | def new(self): |
| 1478 | self.elements = [] |
| 1479 | return self |
| 1480 | def add(self, e): |
| 1481 | if e not in self.elements: |
| 1482 | self.elements.append(e) |
| 1483 | def remove(self, e): |
| 1484 | if e in self.elements: |
| 1485 | for i in range(len(self.elements)): |
| 1486 | if self.elements[i] = e: |
| 1487 | del self.elements[i] |
| 1488 | break |
| 1489 | def is_element(self, e): |
| 1490 | return e in self.elements |
| 1491 | def size(self): |
| 1492 | return len(self.elements) |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1493 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1494 | Note that the class definition looks like a big compound statement, |
| 1495 | with all the function definitons indented repective to the |
| 1496 | {\tt class} |
| 1497 | keyword. |
| 1498 | |
| 1499 | Let's assume that this |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1500 | {\em class\ definition} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1501 | is the only contents of the module file |
| 1502 | {\tt SetClass.py}. |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1503 | We can then use it in a Python program as follows: |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1504 | \bcode\begin{verbatim} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1505 | >>> from SetClass import Set |
| 1506 | >>> a = Set().new() # create a Set object |
| 1507 | >>> a.add(2) |
| 1508 | >>> a.add(3) |
| 1509 | >>> a.add(1) |
| 1510 | >>> a.add(1) |
| 1511 | >>> if a.is_element(3): print '3 is in the set' |
| 1512 | ... |
| 1513 | 3 is in the set |
| 1514 | >>> if not a.is_element(4): print '4 is not in the set' |
| 1515 | ... |
| 1516 | 4 is not in the set |
| 1517 | >>> print 'a has', a.size(), 'elements' |
| 1518 | a has 3 elements |
| 1519 | >>> a.remove(1) |
| 1520 | >>> print 'now a has', a.size(), 'elements' |
| 1521 | >>> |
| 1522 | now a has 2 elements |
| 1523 | >>> |
Guido van Rossum | 5ce78f1 | 1991-01-25 13:27:18 +0000 | [diff] [blame] | 1524 | \end{verbatim}\ecode |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1525 | From the example we learn in the first place that the functions defined |
| 1526 | in the class (e.g., |
| 1527 | {\tt add}) |
| 1528 | can be called using the |
Guido van Rossum | 2292b8e | 1991-01-23 16:31:24 +0000 | [diff] [blame] | 1529 | {\em member} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1530 | notation for the object |
| 1531 | {\tt a}. |
| 1532 | The member function is called with one less argument than it is defined: |
| 1533 | the object is implicitly passed as the first argument. |
| 1534 | Thus, the call |
| 1535 | {\tt a.add(2)} |
| 1536 | is equivalent to |
| 1537 | {\tt Set.add(a, 2)}. |
| 1538 | |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1539 | XXX This section is not complete yet! Inheritance! |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1540 | |
| 1541 | \section{XXX P.M.} |
| 1542 | |
Guido van Rossum | 7d9f8d7 | 1991-01-22 11:45:00 +0000 | [diff] [blame] | 1543 | \begin{itemize} |
| 1544 | \item The {\tt del} statement. |
| 1545 | \item The {\tt dir()} function. |
| 1546 | \item Tuples. |
| 1547 | \item Dictionaries. |
| 1548 | \item Objects and types in general. |
| 1549 | \item Backquotes. |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1550 | \item Output formatting. |
Guido van Rossum | 7d9f8d7 | 1991-01-22 11:45:00 +0000 | [diff] [blame] | 1551 | \item And/Or/Not. |
Guido van Rossum | 4410c75 | 1991-06-04 20:22:18 +0000 | [diff] [blame] | 1552 | \item ``.pyc'' files. |
Guido van Rossum | 7d9f8d7 | 1991-01-22 11:45:00 +0000 | [diff] [blame] | 1553 | \end{itemize} |
Guido van Rossum | d9bf55d | 1991-01-11 16:35:08 +0000 | [diff] [blame] | 1554 | |
| 1555 | \end{document} |