blob: c953394e5d17d9d7fc72db062e1960d49ba06d45 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. _tut-informal:
2
3**********************************
4An Informal Introduction to Python
5**********************************
6
7In the following examples, input and output are distinguished by the presence or
8absence of prompts (``>>>`` and ``...``): to repeat the example, you must type
9everything after the prompt, when the prompt appears; lines that do not begin
10with a prompt are output from the interpreter. Note that a secondary prompt on a
11line by itself in an example means you must type a blank line; this is used to
12end a multi-line command.
13
Georg Brandl8ec7f652007-08-15 14:28:01 +000014Many of the examples in this manual, even those entered at the interactive
15prompt, include comments. Comments in Python start with the hash character,
Georg Brandl3ce0dee2008-09-13 17:18:11 +000016``#``, and extend to the end of the physical line. A comment may appear at the
17start of a line or following whitespace or code, but not within a string
Georg Brandlb19be572007-12-29 10:57:00 +000018literal. A hash character within a string literal is just a hash character.
Georg Brandl3ce0dee2008-09-13 17:18:11 +000019Since comments are to clarify code and are not interpreted by Python, they may
20be omitted when typing in examples.
Georg Brandl8ec7f652007-08-15 14:28:01 +000021
22Some examples::
23
24 # this is the first comment
25 SPAM = 1 # and this is the second comment
26 # ... and now a third!
27 STRING = "# This is not a comment."
28
29
30.. _tut-calculator:
31
32Using Python as a Calculator
33============================
34
35Let's try some simple Python commands. Start the interpreter and wait for the
36primary prompt, ``>>>``. (It shouldn't take long.)
37
38
39.. _tut-numbers:
40
41Numbers
42-------
43
44The interpreter acts as a simple calculator: you can type an expression at it
45and it will write the value. Expression syntax is straightforward: the
46operators ``+``, ``-``, ``*`` and ``/`` work just like in most other languages
47(for example, Pascal or C); parentheses can be used for grouping. For example::
48
49 >>> 2+2
50 4
51 >>> # This is a comment
52 ... 2+2
53 4
54 >>> 2+2 # and a comment on the same line as code
55 4
56 >>> (50-5*6)/4
57 5
58 >>> # Integer division returns the floor:
59 ... 7/3
60 2
61 >>> 7/-3
62 -3
63
64The equal sign (``'='``) is used to assign a value to a variable. Afterwards, no
65result is displayed before the next interactive prompt::
66
67 >>> width = 20
68 >>> height = 5*9
69 >>> width * height
70 900
71
72A value can be assigned to several variables simultaneously::
73
74 >>> x = y = z = 0 # Zero x, y and z
75 >>> x
76 0
77 >>> y
78 0
79 >>> z
80 0
81
Georg Brandl3ce0dee2008-09-13 17:18:11 +000082Variables must be "defined" (assigned a value) before they can be used, or an
83error will occur::
84
85 >>> # try to access an undefined variable
86 ... n
Georg Brandlc62ef8b2009-01-03 20:55:06 +000087 Traceback (most recent call last):
Georg Brandl3ce0dee2008-09-13 17:18:11 +000088 File "<stdin>", line 1, in <module>
89 NameError: name 'n' is not defined
90
Georg Brandl8ec7f652007-08-15 14:28:01 +000091There is full support for floating point; operators with mixed type operands
92convert the integer operand to floating point::
93
94 >>> 3 * 3.75 / 1.5
95 7.5
96 >>> 7.0 / 2
97 3.5
98
99Complex numbers are also supported; imaginary numbers are written with a suffix
100of ``j`` or ``J``. Complex numbers with a nonzero real component are written as
101``(real+imagj)``, or can be created with the ``complex(real, imag)`` function.
102::
103
104 >>> 1j * 1J
105 (-1+0j)
106 >>> 1j * complex(0,1)
107 (-1+0j)
108 >>> 3+1j*3
109 (3+3j)
110 >>> (3+1j)*3
111 (9+3j)
112 >>> (1+2j)/(1+1j)
113 (1.5+0.5j)
114
115Complex numbers are always represented as two floating point numbers, the real
116and imaginary part. To extract these parts from a complex number *z*, use
117``z.real`` and ``z.imag``. ::
118
119 >>> a=1.5+0.5j
120 >>> a.real
121 1.5
122 >>> a.imag
123 0.5
124
125The conversion functions to floating point and integer (:func:`float`,
126:func:`int` and :func:`long`) don't work for complex numbers --- there is no one
127correct way to convert a complex number to a real number. Use ``abs(z)`` to get
128its magnitude (as a float) or ``z.real`` to get its real part. ::
129
130 >>> a=3.0+4.0j
131 >>> float(a)
132 Traceback (most recent call last):
133 File "<stdin>", line 1, in ?
134 TypeError: can't convert complex to float; use abs(z)
135 >>> a.real
136 3.0
137 >>> a.imag
138 4.0
139 >>> abs(a) # sqrt(a.real**2 + a.imag**2)
140 5.0
Georg Brandl8ec7f652007-08-15 14:28:01 +0000141
142In interactive mode, the last printed expression is assigned to the variable
143``_``. This means that when you are using Python as a desk calculator, it is
144somewhat easier to continue calculations, for example::
145
146 >>> tax = 12.5 / 100
147 >>> price = 100.50
148 >>> price * tax
149 12.5625
150 >>> price + _
151 113.0625
152 >>> round(_, 2)
153 113.06
Georg Brandl8ec7f652007-08-15 14:28:01 +0000154
155This variable should be treated as read-only by the user. Don't explicitly
156assign a value to it --- you would create an independent local variable with the
157same name masking the built-in variable with its magic behavior.
158
159
160.. _tut-strings:
161
162Strings
163-------
164
165Besides numbers, Python can also manipulate strings, which can be expressed in
166several ways. They can be enclosed in single quotes or double quotes::
167
168 >>> 'spam eggs'
169 'spam eggs'
170 >>> 'doesn\'t'
171 "doesn't"
172 >>> "doesn't"
173 "doesn't"
174 >>> '"Yes," he said.'
175 '"Yes," he said.'
176 >>> "\"Yes,\" he said."
177 '"Yes," he said.'
178 >>> '"Isn\'t," she said.'
179 '"Isn\'t," she said.'
180
181String literals can span multiple lines in several ways. Continuation lines can
182be used, with a backslash as the last character on the line indicating that the
183next line is a logical continuation of the line::
184
185 hello = "This is a rather long string containing\n\
186 several lines of text just as you would do in C.\n\
187 Note that whitespace at the beginning of the line is\
188 significant."
189
190 print hello
191
192Note that newlines still need to be embedded in the string using ``\n``; the
193newline following the trailing backslash is discarded. This example would print
Georg Brandlbf58d802009-09-03 07:27:26 +0000194the following:
195
196.. code-block:: text
Georg Brandl8ec7f652007-08-15 14:28:01 +0000197
198 This is a rather long string containing
199 several lines of text just as you would do in C.
200 Note that whitespace at the beginning of the line is significant.
201
Georg Brandl8ec7f652007-08-15 14:28:01 +0000202Or, strings can be surrounded in a pair of matching triple-quotes: ``"""`` or
203``'''``. End of lines do not need to be escaped when using triple-quotes, but
204they will be included in the string. ::
205
206 print """
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000207 Usage: thingy [OPTIONS]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000208 -h Display this usage message
209 -H hostname Hostname to connect to
210 """
211
Georg Brandlbf58d802009-09-03 07:27:26 +0000212produces the following output:
213
214.. code-block:: text
Georg Brandl8ec7f652007-08-15 14:28:01 +0000215
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000216 Usage: thingy [OPTIONS]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000217 -h Display this usage message
218 -H hostname Hostname to connect to
219
Georg Brandl186188d2009-03-31 20:56:32 +0000220If we make the string literal a "raw" string, ``\n`` sequences are not converted
221to newlines, but the backslash at the end of the line, and the newline character
222in the source, are both included in the string as data. Thus, the example::
223
224 hello = r"This is a rather long string containing\n\
225 several lines of text much as you would do in C."
226
227 print hello
228
Georg Brandlbf58d802009-09-03 07:27:26 +0000229would print:
230
231.. code-block:: text
Georg Brandl186188d2009-03-31 20:56:32 +0000232
233 This is a rather long string containing\n\
234 several lines of text much as you would do in C.
235
Georg Brandl8ec7f652007-08-15 14:28:01 +0000236The interpreter prints the result of string operations in the same way as they
237are typed for input: inside quotes, and with quotes and other funny characters
238escaped by backslashes, to show the precise value. The string is enclosed in
239double quotes if the string contains a single quote and no double quotes, else
240it's enclosed in single quotes. (The :keyword:`print` statement, described
241later, can be used to write strings without quotes or escapes.)
242
243Strings can be concatenated (glued together) with the ``+`` operator, and
244repeated with ``*``::
245
246 >>> word = 'Help' + 'A'
247 >>> word
248 'HelpA'
249 >>> '<' + word*5 + '>'
250 '<HelpAHelpAHelpAHelpAHelpA>'
251
252Two string literals next to each other are automatically concatenated; the first
253line above could also have been written ``word = 'Help' 'A'``; this only works
254with two literals, not with arbitrary string expressions::
255
256 >>> 'str' 'ing' # <- This is ok
257 'string'
258 >>> 'str'.strip() + 'ing' # <- This is ok
259 'string'
260 >>> 'str'.strip() 'ing' # <- This is invalid
261 File "<stdin>", line 1, in ?
262 'str'.strip() 'ing'
263 ^
264 SyntaxError: invalid syntax
265
266Strings can be subscripted (indexed); like in C, the first character of a string
267has subscript (index) 0. There is no separate character type; a character is
268simply a string of size one. Like in Icon, substrings can be specified with the
269*slice notation*: two indices separated by a colon. ::
270
271 >>> word[4]
272 'A'
273 >>> word[0:2]
274 'He'
275 >>> word[2:4]
276 'lp'
277
278Slice indices have useful defaults; an omitted first index defaults to zero, an
279omitted second index defaults to the size of the string being sliced. ::
280
281 >>> word[:2] # The first two characters
282 'He'
283 >>> word[2:] # Everything except the first two characters
284 'lpA'
285
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000286Unlike a C string, Python strings cannot be changed. Assigning to an indexed
Georg Brandl8ec7f652007-08-15 14:28:01 +0000287position in the string results in an error::
288
289 >>> word[0] = 'x'
290 Traceback (most recent call last):
291 File "<stdin>", line 1, in ?
Georg Brandl5e88eea2009-05-17 08:10:27 +0000292 TypeError: object does not support item assignment
Georg Brandl8ec7f652007-08-15 14:28:01 +0000293 >>> word[:1] = 'Splat'
294 Traceback (most recent call last):
295 File "<stdin>", line 1, in ?
Georg Brandl5e88eea2009-05-17 08:10:27 +0000296 TypeError: object does not support slice assignment
Georg Brandl8ec7f652007-08-15 14:28:01 +0000297
298However, creating a new string with the combined content is easy and efficient::
299
300 >>> 'x' + word[1:]
301 'xelpA'
302 >>> 'Splat' + word[4]
303 'SplatA'
304
305Here's a useful invariant of slice operations: ``s[:i] + s[i:]`` equals ``s``.
306::
307
308 >>> word[:2] + word[2:]
309 'HelpA'
310 >>> word[:3] + word[3:]
311 'HelpA'
312
313Degenerate slice indices are handled gracefully: an index that is too large is
314replaced by the string size, an upper bound smaller than the lower bound returns
315an empty string. ::
316
317 >>> word[1:100]
318 'elpA'
319 >>> word[10:]
320 ''
321 >>> word[2:1]
322 ''
323
324Indices may be negative numbers, to start counting from the right. For example::
325
326 >>> word[-1] # The last character
327 'A'
328 >>> word[-2] # The last-but-one character
329 'p'
330 >>> word[-2:] # The last two characters
331 'pA'
332 >>> word[:-2] # Everything except the last two characters
333 'Hel'
334
335But note that -0 is really the same as 0, so it does not count from the right!
336::
337
338 >>> word[-0] # (since -0 equals 0)
339 'H'
340
341Out-of-range negative slice indices are truncated, but don't try this for
342single-element (non-slice) indices::
343
344 >>> word[-100:]
345 'HelpA'
346 >>> word[-10] # error
347 Traceback (most recent call last):
348 File "<stdin>", line 1, in ?
349 IndexError: string index out of range
350
351One way to remember how slices work is to think of the indices as pointing
352*between* characters, with the left edge of the first character numbered 0.
353Then the right edge of the last character of a string of *n* characters has
354index *n*, for example::
355
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000356 +---+---+---+---+---+
Georg Brandl8ec7f652007-08-15 14:28:01 +0000357 | H | e | l | p | A |
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000358 +---+---+---+---+---+
359 0 1 2 3 4 5
Georg Brandl8ec7f652007-08-15 14:28:01 +0000360 -5 -4 -3 -2 -1
361
362The first row of numbers gives the position of the indices 0...5 in the string;
363the second row gives the corresponding negative indices. The slice from *i* to
364*j* consists of all characters between the edges labeled *i* and *j*,
365respectively.
366
367For non-negative indices, the length of a slice is the difference of the
368indices, if both are within bounds. For example, the length of ``word[1:3]`` is
3692.
370
371The built-in function :func:`len` returns the length of a string::
372
373 >>> s = 'supercalifragilisticexpialidocious'
374 >>> len(s)
375 34
376
377
378.. seealso::
379
380 :ref:`typesseq`
381 Strings, and the Unicode strings described in the next section, are
382 examples of *sequence types*, and support the common operations supported
383 by such types.
384
385 :ref:`string-methods`
386 Both strings and Unicode strings support a large number of methods for
387 basic transformations and searching.
388
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000389 :ref:`new-string-formatting`
390 Information about string formatting with :meth:`str.format` is described
391 here.
392
Georg Brandl8ec7f652007-08-15 14:28:01 +0000393 :ref:`string-formatting`
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000394 The old formatting operations invoked when strings and Unicode strings are
395 the left operand of the ``%`` operator are described in more detail here.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000396
397
398.. _tut-unicodestrings:
399
400Unicode Strings
401---------------
402
403.. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com>
404
405
406Starting with Python 2.0 a new data type for storing text data is available to
407the programmer: the Unicode object. It can be used to store and manipulate
408Unicode data (see http://www.unicode.org/) and integrates well with the existing
409string objects, providing auto-conversions where necessary.
410
411Unicode has the advantage of providing one ordinal for every character in every
412script used in modern and ancient texts. Previously, there were only 256
413possible ordinals for script characters. Texts were typically bound to a code
414page which mapped the ordinals to script characters. This lead to very much
415confusion especially with respect to internationalization (usually written as
416``i18n`` --- ``'i'`` + 18 characters + ``'n'``) of software. Unicode solves
417these problems by defining one code page for all scripts.
418
419Creating Unicode strings in Python is just as simple as creating normal
420strings::
421
422 >>> u'Hello World !'
423 u'Hello World !'
424
425The small ``'u'`` in front of the quote indicates that a Unicode string is
426supposed to be created. If you want to include special characters in the string,
427you can do so by using the Python *Unicode-Escape* encoding. The following
428example shows how::
429
430 >>> u'Hello\u0020World !'
431 u'Hello World !'
432
433The escape sequence ``\u0020`` indicates to insert the Unicode character with
434the ordinal value 0x0020 (the space character) at the given position.
435
436Other characters are interpreted by using their respective ordinal values
437directly as Unicode ordinals. If you have literal strings in the standard
438Latin-1 encoding that is used in many Western countries, you will find it
439convenient that the lower 256 characters of Unicode are the same as the 256
440characters of Latin-1.
441
442For experts, there is also a raw mode just like the one for normal strings. You
443have to prefix the opening quote with 'ur' to have Python use the
444*Raw-Unicode-Escape* encoding. It will only apply the above ``\uXXXX``
445conversion if there is an uneven number of backslashes in front of the small
446'u'. ::
447
448 >>> ur'Hello\u0020World !'
449 u'Hello World !'
450 >>> ur'Hello\\u0020World !'
451 u'Hello\\\\u0020World !'
452
453The raw mode is most useful when you have to enter lots of backslashes, as can
454be necessary in regular expressions.
455
456Apart from these standard encodings, Python provides a whole set of other ways
457of creating Unicode strings on the basis of a known encoding.
458
459.. index:: builtin: unicode
460
461The built-in function :func:`unicode` provides access to all registered Unicode
462codecs (COders and DECoders). Some of the more well known encodings which these
463codecs can convert are *Latin-1*, *ASCII*, *UTF-8*, and *UTF-16*. The latter two
464are variable-length encodings that store each Unicode character in one or more
465bytes. The default encoding is normally set to ASCII, which passes through
466characters in the range 0 to 127 and rejects any other characters with an error.
467When a Unicode string is printed, written to a file, or converted with
468:func:`str`, conversion takes place using this default encoding. ::
469
470 >>> u"abc"
471 u'abc'
472 >>> str(u"abc")
473 'abc'
474 >>> u"äöü"
475 u'\xe4\xf6\xfc'
476 >>> str(u"äöü")
477 Traceback (most recent call last):
478 File "<stdin>", line 1, in ?
479 UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
480
481To convert a Unicode string into an 8-bit string using a specific encoding,
482Unicode objects provide an :func:`encode` method that takes one argument, the
483name of the encoding. Lowercase names for encodings are preferred. ::
484
485 >>> u"äöü".encode('utf-8')
486 '\xc3\xa4\xc3\xb6\xc3\xbc'
487
488If you have data in a specific encoding and want to produce a corresponding
489Unicode string from it, you can use the :func:`unicode` function with the
490encoding name as the second argument. ::
491
492 >>> unicode('\xc3\xa4\xc3\xb6\xc3\xbc', 'utf-8')
493 u'\xe4\xf6\xfc'
494
495
496.. _tut-lists:
497
498Lists
499-----
500
501Python knows a number of *compound* data types, used to group together other
502values. The most versatile is the *list*, which can be written as a list of
503comma-separated values (items) between square brackets. List items need not all
504have the same type. ::
505
506 >>> a = ['spam', 'eggs', 100, 1234]
507 >>> a
508 ['spam', 'eggs', 100, 1234]
509
510Like string indices, list indices start at 0, and lists can be sliced,
511concatenated and so on::
512
513 >>> a[0]
514 'spam'
515 >>> a[3]
516 1234
517 >>> a[-2]
518 100
519 >>> a[1:-1]
520 ['eggs', 100]
521 >>> a[:2] + ['bacon', 2*2]
522 ['spam', 'eggs', 'bacon', 4]
523 >>> 3*a[:3] + ['Boo!']
524 ['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boo!']
525
Georg Brandl0fcd8822010-03-21 09:17:41 +0000526All slice operations return a new list containing the requested elements. This
527means that the following slice returns a shallow copy of the list *a*::
528
529 >>> a[:]
530 ['spam', 'eggs', 100, 1234]
531
Georg Brandl8ec7f652007-08-15 14:28:01 +0000532Unlike strings, which are *immutable*, it is possible to change individual
533elements of a list::
534
535 >>> a
536 ['spam', 'eggs', 100, 1234]
537 >>> a[2] = a[2] + 23
538 >>> a
539 ['spam', 'eggs', 123, 1234]
540
541Assignment to slices is also possible, and this can even change the size of the
542list or clear it entirely::
543
544 >>> # Replace some items:
545 ... a[0:2] = [1, 12]
546 >>> a
547 [1, 12, 123, 1234]
548 >>> # Remove some:
549 ... a[0:2] = []
550 >>> a
551 [123, 1234]
552 >>> # Insert some:
553 ... a[1:1] = ['bletch', 'xyzzy']
554 >>> a
555 [123, 'bletch', 'xyzzy', 1234]
556 >>> # Insert (a copy of) itself at the beginning
557 >>> a[:0] = a
558 >>> a
559 [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
560 >>> # Clear the list: replace all items with an empty list
561 >>> a[:] = []
562 >>> a
563 []
564
565The built-in function :func:`len` also applies to lists::
566
Georg Brandl87426cb2007-11-09 13:08:48 +0000567 >>> a = ['a', 'b', 'c', 'd']
Georg Brandl8ec7f652007-08-15 14:28:01 +0000568 >>> len(a)
Georg Brandl87426cb2007-11-09 13:08:48 +0000569 4
Georg Brandl8ec7f652007-08-15 14:28:01 +0000570
571It is possible to nest lists (create lists containing other lists), for
572example::
573
574 >>> q = [2, 3]
575 >>> p = [1, q, 4]
576 >>> len(p)
577 3
578 >>> p[1]
579 [2, 3]
580 >>> p[1][0]
581 2
582 >>> p[1].append('xtra') # See section 5.1
583 >>> p
584 [1, [2, 3, 'xtra'], 4]
585 >>> q
586 [2, 3, 'xtra']
587
588Note that in the last example, ``p[1]`` and ``q`` really refer to the same
589object! We'll come back to *object semantics* later.
590
591
592.. _tut-firststeps:
593
594First Steps Towards Programming
595===============================
596
597Of course, we can use Python for more complicated tasks than adding two and two
598together. For instance, we can write an initial sub-sequence of the *Fibonacci*
599series as follows::
600
601 >>> # Fibonacci series:
602 ... # the sum of two elements defines the next
603 ... a, b = 0, 1
604 >>> while b < 10:
Georg Brandl35f88612008-01-06 22:05:40 +0000605 ... print b
606 ... a, b = b, a+b
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000607 ...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000608 1
609 1
610 2
611 3
612 5
613 8
614
615This example introduces several new features.
616
617* The first line contains a *multiple assignment*: the variables ``a`` and ``b``
618 simultaneously get the new values 0 and 1. On the last line this is used again,
619 demonstrating that the expressions on the right-hand side are all evaluated
620 first before any of the assignments take place. The right-hand side expressions
621 are evaluated from the left to the right.
622
623* The :keyword:`while` loop executes as long as the condition (here: ``b < 10``)
624 remains true. In Python, like in C, any non-zero integer value is true; zero is
625 false. The condition may also be a string or list value, in fact any sequence;
626 anything with a non-zero length is true, empty sequences are false. The test
627 used in the example is a simple comparison. The standard comparison operators
628 are written the same as in C: ``<`` (less than), ``>`` (greater than), ``==``
629 (equal to), ``<=`` (less than or equal to), ``>=`` (greater than or equal to)
630 and ``!=`` (not equal to).
631
632* The *body* of the loop is *indented*: indentation is Python's way of grouping
633 statements. Python does not (yet!) provide an intelligent input line editing
634 facility, so you have to type a tab or space(s) for each indented line. In
635 practice you will prepare more complicated input for Python with a text editor;
636 most text editors have an auto-indent facility. When a compound statement is
637 entered interactively, it must be followed by a blank line to indicate
638 completion (since the parser cannot guess when you have typed the last line).
639 Note that each line within a basic block must be indented by the same amount.
640
641* The :keyword:`print` statement writes the value of the expression(s) it is
642 given. 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 expressions
644 and strings. Strings are printed without quotes, and a space is inserted
645 between items, so you can format things nicely, like this::
646
647 >>> i = 256*256
648 >>> print 'The value of i is', i
649 The value of i is 65536
650
651 A trailing comma avoids the newline after the output::
652
653 >>> a, b = 0, 1
654 >>> while b < 1000:
655 ... print b,
656 ... a, b = b, a+b
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000657 ...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000658 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
659
660 Note that the interpreter inserts a newline before it prints the next prompt if
661 the last line was not completed.