diff options
Diffstat (limited to 'doc/tut.tex')
| -rw-r--r-- | doc/tut.tex | 136 |
1 files changed, 68 insertions, 68 deletions
diff --git a/doc/tut.tex b/doc/tut.tex index e0229b9..a17e79c 100644 --- a/doc/tut.tex +++ b/doc/tut.tex @@ -4,15 +4,15 @@ \documentstyle[11pt,myformat]{article} \title{\bf - Python Tutorial \\ - (DRAFT) + Python Tutorial \\ + (DRAFT) } \author{ Guido van Rossum \\ Dept. CST, CWI, Kruislaan 413 \\ 1098 SJ Amsterdam, The Netherlands \\ - E-mail: {\tt [email protected]} + E-mail: {\tt [email protected]} } \begin{document} @@ -361,13 +361,13 @@ For example: >>> # This is a comment >>> 2+2 4 ->>> +>>> >>> (50-5+5*6+25)/4 25 >>> # Division truncates towards zero: >>> 7/3 2 ->>> +>>> \end{verbatim}\ecode As in C, the equal sign ({\tt =}) is used to assign a value to a variable. The value of an assignment is not written: @@ -376,14 +376,14 @@ The value of an assignment is not written: >>> height = 5*9 >>> width * height 900 ->>> +>>> \end{verbatim}\ecode There is some support for floating point, but you can't mix floating point and integral numbers in expression (yet): \bcode\begin{verbatim} >>> 10.0 / 3.3 3.0303030303 ->>> +>>> \end{verbatim}\ecode Besides numbers, \Python\ can also manipulate strings, enclosed in single quotes: @@ -392,7 +392,7 @@ quotes: 'foo bar' >>> 'doesn\'t' 'doesn\'t' ->>> +>>> \end{verbatim}\ecode Strings are written inside quotes and with quotes and other funny characters escaped by backslashes, to show the precise value. @@ -406,7 +406,7 @@ operator, and repeated with~{\tt *}: 'HelpA' >>> '<' + word*5 + '>' '<HelpAHelpAHelpAHelpAHelpA>' ->>> +>>> \end{verbatim}\ecode Strings can be subscripted; as in C, the first character of a string has subscript 0. @@ -430,7 +430,7 @@ notation: two subscripts (indices) separated by a colon. >>> # A useful invariant: s[:i] + s[i:] = s >>> word[:3] + word[3:] 'HelpA' ->>> +>>> \end{verbatim}\ecode Degenerate cases are handled gracefully: an index that is too large is replaced by the string size, an upper bound smaller than the lower bound @@ -442,7 +442,7 @@ returns an empty string. '' >>> word[2:1] '' ->>> +>>> \end{verbatim}\ecode Slice indices (but not simple subscripts) may be negative numbers, to start counting from the right. @@ -455,7 +455,7 @@ For example: >>> # But -0 does not count from the right! >>> word[-0:] # (since -0 equals 0) 'HelpA' ->>> +>>> \end{verbatim}\ecode The best way to remember how slices work is to think of the indices as pointing @@ -467,10 +467,10 @@ characters has index {\tt n}, for example: \bcode\begin{verbatim} - +---+---+---+---+---+ + +---+---+---+---+---+ | H | e | l | p | A | - +---+---+---+---+---+ - 0 1 2 3 4 5 + +---+---+---+---+---+ + 0 1 2 3 4 5 -5 -4 -3 -2 -1 \end{verbatim}\ecode The first row of numbers gives the position of the indices 0...5 in the @@ -488,7 +488,7 @@ string: >>> s = 'supercalifragilisticexpialidocious' >>> len(s) 34 ->>> +>>> \end{verbatim}\ecode \Python\ knows a number of {\em compound} @@ -501,7 +501,7 @@ brackets: >>> a = ['foo', 'bar', 100, 1234] >>> a ['foo', 'bar', 100, 1234] ->>> +>>> \end{verbatim}\ecode As for strings, list subscripts start at 0: \bcode\begin{verbatim} @@ -509,7 +509,7 @@ As for strings, list subscripts start at 0: 'foo' >>> a[3] 1234 ->>> +>>> \end{verbatim}\ecode Lists can be sliced and concatenated like strings: \bcode\begin{verbatim} @@ -517,7 +517,7 @@ Lists can be sliced and concatenated like strings: ['bar', 100] >>> a[:2] + ['bletch', 2*2] ['foo', 'bar', 'bletch', 4] ->>> +>>> \end{verbatim}\ecode Unlike strings, which are {\em immutable}, @@ -545,13 +545,13 @@ of the list: >>> a[1:1] = ['bletch', 'xyzzy'] >>> a [123, 'bletch', 'xyzzy', 1234] ->>> +>>> \end{verbatim}\ecode The built-in function {\tt len()} also applies to lists: \bcode\begin{verbatim} >>> len(a) 4 ->>> +>>> \end{verbatim}\ecode \subsection{Tuples and Sequences} @@ -572,7 +572,7 @@ series as follows: >>> while b < 100: ... print b ... a, b = b, a+b -... +... 1 1 2 @@ -584,7 +584,7 @@ series as follows: 34 55 89 ->>> +>>> \end{verbatim}\ecode This example introduces several new features. \begin{itemize} @@ -645,7 +645,7 @@ items, so you can format things nicely, like this: >>> i = 256*256 >>> print 'The value of i is', i The value of i is 65536 ->>> +>>> \end{verbatim}\ecode A trailing comma avoids the newline after the output: \bcode\begin{verbatim} @@ -653,9 +653,9 @@ A trailing comma avoids the newline after the output: >>> while b < 1000: ... print b, ... a, b = b, a+b -... +... 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 ->>> +>>> \end{verbatim}\ecode Note that the interpreter inserts a newline before it prints the next prompt if the last line was not completed. @@ -681,7 +681,7 @@ For example: ... print 'Single' ... else: ... print 'More' -... +... \end{verbatim}\ecode There can be zero or more {\tt elif} parts, and the {\tt else} part is optional. @@ -704,11 +704,11 @@ For example (no pun intended): >>> a = ['cat', 'window', 'defenestrate'] >>> for x in a: ... print x, len(x) -... +... cat 3 window 6 defenestrate 12 ->>> +>>> \end{verbatim}\ecode \subsubsection{The {\tt range()} Function} @@ -720,7 +720,7 @@ e.g.: \bcode\begin{verbatim} >>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] ->>> +>>> \end{verbatim}\ecode The given end point is never part of the generated list; {\tt range(10)} generates a list of 10 values, @@ -734,7 +734,7 @@ different increment (even negative): [0, 3, 6, 9] >>> range(-10, -100, -30) [-10, -40, -70] ->>> +>>> \end{verbatim}\ecode To iterate over the indices of a sequence, combine {\tt range()} and {\tt len()} as follows: @@ -742,13 +742,13 @@ and {\tt len()} as follows: >>> a = ['Mary', 'had', 'a', 'little', 'boy'] >>> for i in range(len(a)): ... print i, a[i] -... +... 0 Mary 1 had 2 a 3 little 4 boy ->>> +>>> \end{verbatim}\ecode \subsubsection{Break Statements and Else Clauses on Loops} @@ -769,7 +769,7 @@ item of value 0: ... break ... else: ... print n, 'is a prime number' -... +... 2 is a prime number 3 is a prime number 4 equals 2 * 2 @@ -778,7 +778,7 @@ item of value 0: 7 is a prime number 8 equals 2 * 4 9 equals 3 * 3 ->>> +>>> \end{verbatim}\ecode \subsubsection{Pass Statements} @@ -790,7 +790,7 @@ For example: \bcode\begin{verbatim} >>> while 1: ... pass # Busy-wait for keyboard interrupt -... +... \end{verbatim}\ecode \subsubsection{Conditions Revisited} @@ -807,11 +807,11 @@ arbitrary boundary: ... while b <= n: ... print b, ... a, b = b, a+b -... +... >>> # Now call the function we just defined: >>> fib(2000) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 ->>> +>>> \end{verbatim}\ecode The keyword {\tt def} @@ -858,7 +858,7 @@ This serves as a general renaming mechanism: >>> f = fib >>> f(100) 1 1 2 3 5 8 13 21 34 55 89 ->>> +>>> \end{verbatim}\ecode You might object that {\tt fib} @@ -874,7 +874,7 @@ You can see it if you really want to: \bcode\begin{verbatim} >>> print fib(0) None ->>> +>>> \end{verbatim}\ecode It is simple to write a function that returns a list of the numbers of the Fibonacci series, instead of printing it: @@ -886,11 +886,11 @@ the Fibonacci series, instead of printing it: ... result.append(b) # see below ... a, b = b, a+b ... return result -... +... >>> f100 = fib2(100) # call it >>> f100 # write the result [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] ->>> +>>> \end{verbatim}\ecode This example, as usual, demonstrates some new \Python\ features: \begin{itemize} @@ -956,7 +956,7 @@ For example: >>> b.sort() >>> b ['Mary', 'a', 'boy', 'had', 'little'] ->>> +>>> \end{verbatim}\ecode \subsection{Modules} @@ -1011,7 +1011,7 @@ Now enter the \Python\ interpreter and import this module with the following command: \bcode\begin{verbatim} >>> import fibo ->>> +>>> \end{verbatim}\ecode This does not enter the names of the functions defined in {\tt fibo} @@ -1024,14 +1024,14 @@ Using the module name you can access the functions: 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 >>> fibo.fib2(100) [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89] ->>> +>>> \end{verbatim}\ecode If you intend to use a function often you can assign it to a local name: \bcode\begin{verbatim} >>> fib = fibo.fib >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ->>> +>>> \end{verbatim}\ecode \subsubsection{More on Modules} @@ -1074,7 +1074,7 @@ For example: >>> from fibo import fib, fib2 >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ->>> +>>> \end{verbatim}\ecode This does not introduce the module name from which the imports are taken in the local symbol table (so in the example, {\tt fibo} is not @@ -1085,7 +1085,7 @@ There is even a variant to import all names that a module defines: >>> from fibo import * >>> fib(500) 1 1 2 3 5 8 13 21 34 55 89 144 233 377 ->>> +>>> \end{verbatim}\ecode This imports all names except those beginning with an underscore ({\tt \_}). @@ -1119,7 +1119,7 @@ define the strings used as primary and secondary prompts: >>> sys.ps1 = 'C> ' C> print 'Yuck!' Yuck! -C> +C> \end{verbatim}\ecode These two variables are only defined if the interpreter is in interactive mode. @@ -1137,7 +1137,7 @@ You can modify it using standard list operations, e.g.: \bcode\begin{verbatim} >>> import sys >>> sys.path.append('/ufs/guido/lib/python') ->>> +>>> \end{verbatim}\ecode \subsection{Errors and Exceptions} @@ -1159,7 +1159,7 @@ Parsing error: file <stdin>, line 1: while 1 print 'Hello world' ^ Unhandled exception: run-time error: syntax error ->>> +>>> \end{verbatim}\ecode The parser repeats the offending line and displays a little `arrow' pointing at the earliest point in the line where the error was detected. @@ -1187,7 +1187,7 @@ Stack backtrace (innermost last): Unhandled exception: type error: illegal argument type for built-in operation Stack backtrace (innermost last): File "<stdin>", line 1 ->>> +>>> \end{verbatim}\ecode Errors detected during execution are called {\em exceptions} @@ -1249,12 +1249,12 @@ some floating point numbers: ... print 1.0 / x ... except RuntimeError: ... print '*** has no inverse ***' -... +... 0.3333 3.00030003 2.5 0.4 0 *** has no inverse *** 10 0.1 ->>> +>>> \end{verbatim}\ecode The {\tt try} statement works as follows. \begin{itemize} @@ -1306,9 +1306,9 @@ argument's value, as follows: ... foo() ... except NameError, x: ... print 'name', x, 'undefined' -... +... name foo undefined ->>> +>>> \end{verbatim}\ecode If an exception has an argument, it is printed as the third part (`detail') of the message for unhandled exceptions. @@ -1345,14 +1345,14 @@ For example: \bcode\begin{verbatim} >>> def this_fails(): ... x = 1/0 -... +... >>> try: ... this_fails() ... except RuntimeError, detail: ... print 'Handling run-time error:', detail -... +... Handling run-time error: domain error or zero division ->>> +>>> \end{verbatim}\ecode \subsubsection{Raising Exceptions} @@ -1365,7 +1365,7 @@ For example: Unhandled exception: undefined name: Hi There! Stack backtrace (innermost last): File "<stdin>", line 1 ->>> +>>> \end{verbatim}\ecode The first argument to {\tt raise} names the exception to be raised. The optional second argument specifies the exception's argument. @@ -1381,13 +1381,13 @@ For example: ... raise my_exc, 2*2 ... except my_exc, val: ... print 'My exception occured, value:', val -... +... My exception occured, value: 4 >>> raise my_exc, 1 Unhandled exception: nobody likes me!: 1 Stack backtrace (innermost last): File "<stdin>", line 7 ->>> +>>> \end{verbatim}\ecode Many standard modules use this to report errors that may occur in functions they define. @@ -1402,12 +1402,12 @@ For example: ... raise KeyboardInterrupt ... finally: ... print 'Goodbye, world!' -... +... Goodbye, world! Unhandled exception: keyboard interrupt Stack backtrace (innermost last): File "<stdin>", line 2 ->>> +>>> \end{verbatim}\ecode The {\em finally\ clause} @@ -1506,18 +1506,18 @@ We can then use it in a \Python\ program as follows: >>> a.add(1) >>> a.add(1) >>> if a.is_element(3): print '3 is in the set' -... +... 3 is in the set >>> if not a.is_element(4): print '4 is not in the set' -... +... 4 is not in the set >>> print 'a has', a.size(), 'elements' a has 3 elements >>> a.remove(1) >>> print 'now a has', a.size(), 'elements' ->>> +>>> now a has 2 elements ->>> +>>> \end{verbatim}\ecode From the example we learn in the first place that the functions defined in the class (e.g., |
