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