blob: 44519da97c83d40a2d74ef3b101c6d2b90376ea9 [file] [log] [blame]
Terry Reedy02a807e2010-11-12 04:22:22 +00001.. _tut-informal:
Georg Brandl116aa622007-08-15 14:28:22 +00002
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 Brandl116aa622007-08-15 14:28:22 +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 Brandl5d955ed2008-09-13 17:18:21 +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
Christian Heimes5b5e81c2007-12-31 16:14:33 +000018literal. A hash character within a string literal is just a hash character.
Georg Brandl5d955ed2008-09-13 17:18:21 +000019Since comments are to clarify code and are not interpreted by Python, they may
20be omitted when typing in examples.
Georg Brandl116aa622007-08-15 14:28:22 +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
Guido van Rossum0616b792007-08-31 03:25:11 +000057 5.0
58 >>> 8/5 # Fractions aren't lost when dividing integers
Mark Dickinson5a55b612009-06-28 20:59:42 +000059 1.6
Guido van Rossum0616b792007-08-31 03:25:11 +000060
Georg Brandl48310cd2009-01-03 21:18:54 +000061Note: You might not see exactly the same result; floating point results can
Guido van Rossum0616b792007-08-31 03:25:11 +000062differ from one machine to another. We will say more later about controlling
Mark Dickinsonaf15f3c2009-06-28 21:19:18 +000063the appearance of floating point output. See also :ref:`tut-fp-issues` for a
64full discussion of some of the subtleties of floating point numbers and their
65representations.
Guido van Rossum0616b792007-08-31 03:25:11 +000066
Georg Brandl48310cd2009-01-03 21:18:54 +000067To do integer division and get an integer result,
Guido van Rossum0616b792007-08-31 03:25:11 +000068discarding any fractional result, there is another operator, ``//``::
Georg Brandl48310cd2009-01-03 21:18:54 +000069
Georg Brandl116aa622007-08-15 14:28:22 +000070 >>> # Integer division returns the floor:
Guido van Rossum0616b792007-08-31 03:25:11 +000071 ... 7//3
Georg Brandl116aa622007-08-15 14:28:22 +000072 2
Guido van Rossum0616b792007-08-31 03:25:11 +000073 >>> 7//-3
Georg Brandl116aa622007-08-15 14:28:22 +000074 -3
75
76The equal sign (``'='``) is used to assign a value to a variable. Afterwards, no
77result is displayed before the next interactive prompt::
78
79 >>> width = 20
80 >>> height = 5*9
81 >>> width * height
82 900
83
84A value can be assigned to several variables simultaneously::
85
86 >>> x = y = z = 0 # Zero x, y and z
87 >>> x
88 0
89 >>> y
90 0
91 >>> z
92 0
93
Georg Brandl5d955ed2008-09-13 17:18:21 +000094Variables must be "defined" (assigned a value) before they can be used, or an
95error will occur::
96
97 >>> # try to access an undefined variable
98 ... n
Georg Brandl48310cd2009-01-03 21:18:54 +000099 Traceback (most recent call last):
Georg Brandl5d955ed2008-09-13 17:18:21 +0000100 File "<stdin>", line 1, in <module>
101 NameError: name 'n' is not defined
102
Georg Brandl116aa622007-08-15 14:28:22 +0000103There is full support for floating point; operators with mixed type operands
104convert the integer operand to floating point::
105
106 >>> 3 * 3.75 / 1.5
107 7.5
108 >>> 7.0 / 2
109 3.5
110
111Complex numbers are also supported; imaginary numbers are written with a suffix
112of ``j`` or ``J``. Complex numbers with a nonzero real component are written as
113``(real+imagj)``, or can be created with the ``complex(real, imag)`` function.
114::
115
116 >>> 1j * 1J
117 (-1+0j)
Georg Brandle4ac7502007-09-03 07:10:24 +0000118 >>> 1j * complex(0, 1)
Georg Brandl116aa622007-08-15 14:28:22 +0000119 (-1+0j)
120 >>> 3+1j*3
121 (3+3j)
122 >>> (3+1j)*3
123 (9+3j)
124 >>> (1+2j)/(1+1j)
125 (1.5+0.5j)
126
127Complex numbers are always represented as two floating point numbers, the real
128and imaginary part. To extract these parts from a complex number *z*, use
129``z.real`` and ``z.imag``. ::
130
131 >>> a=1.5+0.5j
132 >>> a.real
133 1.5
134 >>> a.imag
135 0.5
136
137The conversion functions to floating point and integer (:func:`float`,
Georg Brandl2d2590d2007-09-28 13:13:35 +0000138:func:`int`) don't work for complex numbers --- there is not one correct way to
139convert a complex number to a real number. Use ``abs(z)`` to get its magnitude
140(as a float) or ``z.real`` to get its real part::
Georg Brandl116aa622007-08-15 14:28:22 +0000141
142 >>> a=3.0+4.0j
143 >>> float(a)
144 Traceback (most recent call last):
145 File "<stdin>", line 1, in ?
146 TypeError: can't convert complex to float; use abs(z)
147 >>> a.real
148 3.0
149 >>> a.imag
150 4.0
151 >>> abs(a) # sqrt(a.real**2 + a.imag**2)
152 5.0
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154In interactive mode, the last printed expression is assigned to the variable
155``_``. This means that when you are using Python as a desk calculator, it is
156somewhat easier to continue calculations, for example::
157
158 >>> tax = 12.5 / 100
159 >>> price = 100.50
160 >>> price * tax
161 12.5625
162 >>> price + _
163 113.0625
164 >>> round(_, 2)
165 113.06
Georg Brandl116aa622007-08-15 14:28:22 +0000166
167This variable should be treated as read-only by the user. Don't explicitly
168assign a value to it --- you would create an independent local variable with the
169same name masking the built-in variable with its magic behavior.
170
171
172.. _tut-strings:
173
174Strings
175-------
176
177Besides numbers, Python can also manipulate strings, which can be expressed in
178several ways. They can be enclosed in single quotes or double quotes::
179
180 >>> 'spam eggs'
181 'spam eggs'
182 >>> 'doesn\'t'
183 "doesn't"
184 >>> "doesn't"
185 "doesn't"
186 >>> '"Yes," he said.'
187 '"Yes," he said.'
188 >>> "\"Yes,\" he said."
189 '"Yes," he said.'
190 >>> '"Isn\'t," she said.'
191 '"Isn\'t," she said.'
192
Guido van Rossum0616b792007-08-31 03:25:11 +0000193The interpreter prints the result of string operations in the same way as they
194are typed for input: inside quotes, and with quotes and other funny characters
195escaped by backslashes, to show the precise value. The string is enclosed in
196double quotes if the string contains a single quote and no double quotes, else
Senthil Kumaranc64cb6f2010-11-08 02:04:05 +0000197it's enclosed in single quotes. The :func:`print` function produces a more
Senthil Kumarana0fa1ce2010-11-08 01:53:13 +0000198readable output for such input strings.
Guido van Rossum0616b792007-08-31 03:25:11 +0000199
Georg Brandl116aa622007-08-15 14:28:22 +0000200String literals can span multiple lines in several ways. Continuation lines can
201be used, with a backslash as the last character on the line indicating that the
202next line is a logical continuation of the line::
203
204 hello = "This is a rather long string containing\n\
205 several lines of text just as you would do in C.\n\
206 Note that whitespace at the beginning of the line is\
207 significant."
208
Guido van Rossum0616b792007-08-31 03:25:11 +0000209 print(hello)
Georg Brandl116aa622007-08-15 14:28:22 +0000210
Benjamin Petersond7c3ed52010-06-27 22:32:30 +0000211Note that newlines still need to be embedded in the string using ``\n`` -- the
Georg Brandl116aa622007-08-15 14:28:22 +0000212newline following the trailing backslash is discarded. This example would print
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000213the following:
214
215.. code-block:: text
Georg Brandl116aa622007-08-15 14:28:22 +0000216
217 This is a rather long string containing
218 several lines of text just as you would do in C.
219 Note that whitespace at the beginning of the line is significant.
220
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000221Or, strings can be surrounded in a pair of matching triple-quotes: ``"""`` or
222``'''``. End of lines do not need to be escaped when using triple-quotes, but
Terry Reedy02a807e2010-11-12 04:22:22 +0000223they will be included in the string. So the following uses one escape to
224avoid an unwanted initial blank line. ::
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000225
Terry Reedy02a807e2010-11-12 04:22:22 +0000226 print("""\
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000227 Usage: thingy [OPTIONS]
228 -h Display this usage message
229 -H hostname Hostname to connect to
Ezio Melottib297e712009-09-25 20:14:02 +0000230 """)
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000231
232produces the following output:
233
234.. code-block:: text
235
236 Usage: thingy [OPTIONS]
237 -h Display this usage message
238 -H hostname Hostname to connect to
239
Benjamin Petersond23f8222009-04-05 19:13:16 +0000240If we make the string literal a "raw" string, ``\n`` sequences are not converted
241to newlines, but the backslash at the end of the line, and the newline character
242in the source, are both included in the string as data. Thus, the example::
Georg Brandl116aa622007-08-15 14:28:22 +0000243
244 hello = r"This is a rather long string containing\n\
245 several lines of text much as you would do in C."
246
Guido van Rossum0616b792007-08-31 03:25:11 +0000247 print(hello)
Georg Brandl116aa622007-08-15 14:28:22 +0000248
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000249would print:
250
251.. code-block:: text
Georg Brandl116aa622007-08-15 14:28:22 +0000252
253 This is a rather long string containing\n\
254 several lines of text much as you would do in C.
255
Georg Brandl116aa622007-08-15 14:28:22 +0000256Strings can be concatenated (glued together) with the ``+`` operator, and
257repeated with ``*``::
258
259 >>> word = 'Help' + 'A'
260 >>> word
261 'HelpA'
262 >>> '<' + word*5 + '>'
263 '<HelpAHelpAHelpAHelpAHelpA>'
264
265Two string literals next to each other are automatically concatenated; the first
266line above could also have been written ``word = 'Help' 'A'``; this only works
267with two literals, not with arbitrary string expressions::
268
269 >>> 'str' 'ing' # <- This is ok
270 'string'
271 >>> 'str'.strip() + 'ing' # <- This is ok
272 'string'
273 >>> 'str'.strip() 'ing' # <- This is invalid
274 File "<stdin>", line 1, in ?
275 'str'.strip() 'ing'
276 ^
277 SyntaxError: invalid syntax
278
279Strings can be subscripted (indexed); like in C, the first character of a string
280has subscript (index) 0. There is no separate character type; a character is
Georg Brandle4ac7502007-09-03 07:10:24 +0000281simply a string of size one. As in the Icon programming language, substrings
282can be specified with the *slice notation*: two indices separated by a colon.
283::
Georg Brandl116aa622007-08-15 14:28:22 +0000284
285 >>> word[4]
286 'A'
287 >>> word[0:2]
288 'He'
289 >>> word[2:4]
290 'lp'
291
292Slice indices have useful defaults; an omitted first index defaults to zero, an
293omitted second index defaults to the size of the string being sliced. ::
294
295 >>> word[:2] # The first two characters
296 'He'
297 >>> word[2:] # Everything except the first two characters
298 'lpA'
299
Georg Brandl5d955ed2008-09-13 17:18:21 +0000300Unlike a C string, Python strings cannot be changed. Assigning to an indexed
Georg Brandl116aa622007-08-15 14:28:22 +0000301position in the string results in an error::
302
303 >>> word[0] = 'x'
304 Traceback (most recent call last):
305 File "<stdin>", line 1, in ?
Georg Brandl7fcb3bf2009-05-17 08:18:02 +0000306 TypeError: 'str' object does not support item assignment
Georg Brandl116aa622007-08-15 14:28:22 +0000307 >>> word[:1] = 'Splat'
308 Traceback (most recent call last):
309 File "<stdin>", line 1, in ?
Georg Brandl7fcb3bf2009-05-17 08:18:02 +0000310 TypeError: 'str' object does not support slice assignment
Georg Brandl116aa622007-08-15 14:28:22 +0000311
312However, creating a new string with the combined content is easy and efficient::
313
314 >>> 'x' + word[1:]
315 'xelpA'
316 >>> 'Splat' + word[4]
317 'SplatA'
318
319Here's a useful invariant of slice operations: ``s[:i] + s[i:]`` equals ``s``.
320::
321
322 >>> word[:2] + word[2:]
323 'HelpA'
324 >>> word[:3] + word[3:]
325 'HelpA'
326
327Degenerate slice indices are handled gracefully: an index that is too large is
328replaced by the string size, an upper bound smaller than the lower bound returns
329an empty string. ::
330
331 >>> word[1:100]
332 'elpA'
333 >>> word[10:]
334 ''
335 >>> word[2:1]
336 ''
337
338Indices may be negative numbers, to start counting from the right. For example::
339
340 >>> word[-1] # The last character
341 'A'
342 >>> word[-2] # The last-but-one character
343 'p'
344 >>> word[-2:] # The last two characters
345 'pA'
346 >>> word[:-2] # Everything except the last two characters
347 'Hel'
348
349But note that -0 is really the same as 0, so it does not count from the right!
350::
351
352 >>> word[-0] # (since -0 equals 0)
353 'H'
354
355Out-of-range negative slice indices are truncated, but don't try this for
356single-element (non-slice) indices::
357
358 >>> word[-100:]
359 'HelpA'
360 >>> word[-10] # error
361 Traceback (most recent call last):
362 File "<stdin>", line 1, in ?
363 IndexError: string index out of range
364
365One way to remember how slices work is to think of the indices as pointing
366*between* characters, with the left edge of the first character numbered 0.
367Then the right edge of the last character of a string of *n* characters has
368index *n*, for example::
369
Georg Brandl48310cd2009-01-03 21:18:54 +0000370 +---+---+---+---+---+
Georg Brandl116aa622007-08-15 14:28:22 +0000371 | H | e | l | p | A |
Georg Brandl48310cd2009-01-03 21:18:54 +0000372 +---+---+---+---+---+
373 0 1 2 3 4 5
Georg Brandl116aa622007-08-15 14:28:22 +0000374 -5 -4 -3 -2 -1
375
376The first row of numbers gives the position of the indices 0...5 in the string;
377the second row gives the corresponding negative indices. The slice from *i* to
378*j* consists of all characters between the edges labeled *i* and *j*,
379respectively.
380
381For non-negative indices, the length of a slice is the difference of the
382indices, if both are within bounds. For example, the length of ``word[1:3]`` is
3832.
384
385The built-in function :func:`len` returns the length of a string::
386
387 >>> s = 'supercalifragilisticexpialidocious'
388 >>> len(s)
389 34
390
391
392.. seealso::
393
394 :ref:`typesseq`
Georg Brandl48310cd2009-01-03 21:18:54 +0000395 Strings are examples of *sequence types*, and support the common
Guido van Rossum0616b792007-08-31 03:25:11 +0000396 operations supported by such types.
Georg Brandl116aa622007-08-15 14:28:22 +0000397
398 :ref:`string-methods`
Guido van Rossum0616b792007-08-31 03:25:11 +0000399 Strings support a large number of methods for
Georg Brandl116aa622007-08-15 14:28:22 +0000400 basic transformations and searching.
401
402 :ref:`string-formatting`
Benjamin Petersone6f00632008-05-26 01:03:56 +0000403 Information about string formatting with :meth:`str.format` is described
404 here.
405
406 :ref:`old-string-formatting`
407 The old formatting operations invoked when strings and Unicode strings are
408 the left operand of the ``%`` operator are described in more detail here.
Georg Brandl116aa622007-08-15 14:28:22 +0000409
410
411.. _tut-unicodestrings:
412
Guido van Rossum0616b792007-08-31 03:25:11 +0000413About Unicode
414-------------
Georg Brandl116aa622007-08-15 14:28:22 +0000415
416.. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com>
417
418
Georg Brandl5d955ed2008-09-13 17:18:21 +0000419Starting with Python 3.0 all strings support Unicode (see
420http://www.unicode.org/).
Georg Brandl116aa622007-08-15 14:28:22 +0000421
422Unicode has the advantage of providing one ordinal for every character in every
423script used in modern and ancient texts. Previously, there were only 256
424possible ordinals for script characters. Texts were typically bound to a code
425page which mapped the ordinals to script characters. This lead to very much
426confusion especially with respect to internationalization (usually written as
427``i18n`` --- ``'i'`` + 18 characters + ``'n'``) of software. Unicode solves
428these problems by defining one code page for all scripts.
429
Guido van Rossum0616b792007-08-31 03:25:11 +0000430If you want to include special characters in a string,
Georg Brandl116aa622007-08-15 14:28:22 +0000431you can do so by using the Python *Unicode-Escape* encoding. The following
432example shows how::
433
Guido van Rossum0616b792007-08-31 03:25:11 +0000434 >>> 'Hello\u0020World !'
435 'Hello World !'
Georg Brandl116aa622007-08-15 14:28:22 +0000436
437The escape sequence ``\u0020`` indicates to insert the Unicode character with
438the ordinal value 0x0020 (the space character) at the given position.
439
440Other characters are interpreted by using their respective ordinal values
441directly as Unicode ordinals. If you have literal strings in the standard
442Latin-1 encoding that is used in many Western countries, you will find it
443convenient that the lower 256 characters of Unicode are the same as the 256
444characters of Latin-1.
445
Georg Brandl116aa622007-08-15 14:28:22 +0000446Apart from these standard encodings, Python provides a whole set of other ways
447of creating Unicode strings on the basis of a known encoding.
448
Guido van Rossum0616b792007-08-31 03:25:11 +0000449To convert a string into a sequence of bytes using a specific encoding,
450string objects provide an :func:`encode` method that takes one argument, the
Georg Brandl116aa622007-08-15 14:28:22 +0000451name of the encoding. Lowercase names for encodings are preferred. ::
452
Georg Brandlc3f5bad2007-08-31 06:46:05 +0000453 >>> "Äpfel".encode('utf-8')
454 b'\xc3\x84pfel'
Georg Brandl116aa622007-08-15 14:28:22 +0000455
456.. _tut-lists:
457
458Lists
459-----
460
461Python knows a number of *compound* data types, used to group together other
462values. The most versatile is the *list*, which can be written as a list of
463comma-separated values (items) between square brackets. List items need not all
464have the same type. ::
465
466 >>> a = ['spam', 'eggs', 100, 1234]
467 >>> a
468 ['spam', 'eggs', 100, 1234]
469
470Like string indices, list indices start at 0, and lists can be sliced,
471concatenated and so on::
472
473 >>> a[0]
474 'spam'
475 >>> a[3]
476 1234
477 >>> a[-2]
478 100
479 >>> a[1:-1]
480 ['eggs', 100]
481 >>> a[:2] + ['bacon', 2*2]
482 ['spam', 'eggs', 'bacon', 4]
483 >>> 3*a[:3] + ['Boo!']
484 ['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boo!']
485
Benjamin Peterson886af962010-03-21 23:13:07 +0000486All slice operations return a new list containing the requested elements. This
487means that the following slice returns a shallow copy of the list *a*::
488
489 >>> a[:]
490 ['spam', 'eggs', 100, 1234]
491
Georg Brandl116aa622007-08-15 14:28:22 +0000492Unlike strings, which are *immutable*, it is possible to change individual
493elements of a list::
494
495 >>> a
496 ['spam', 'eggs', 100, 1234]
497 >>> a[2] = a[2] + 23
498 >>> a
499 ['spam', 'eggs', 123, 1234]
500
501Assignment to slices is also possible, and this can even change the size of the
502list or clear it entirely::
503
504 >>> # Replace some items:
505 ... a[0:2] = [1, 12]
506 >>> a
507 [1, 12, 123, 1234]
508 >>> # Remove some:
509 ... a[0:2] = []
510 >>> a
511 [123, 1234]
512 >>> # Insert some:
513 ... a[1:1] = ['bletch', 'xyzzy']
514 >>> a
515 [123, 'bletch', 'xyzzy', 1234]
516 >>> # Insert (a copy of) itself at the beginning
517 >>> a[:0] = a
518 >>> a
519 [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
520 >>> # Clear the list: replace all items with an empty list
521 >>> a[:] = []
522 >>> a
523 []
524
525The built-in function :func:`len` also applies to lists::
526
Guido van Rossum58da9312007-11-10 23:39:45 +0000527 >>> a = ['a', 'b', 'c', 'd']
Georg Brandl116aa622007-08-15 14:28:22 +0000528 >>> len(a)
Guido van Rossum58da9312007-11-10 23:39:45 +0000529 4
Georg Brandl116aa622007-08-15 14:28:22 +0000530
531It is possible to nest lists (create lists containing other lists), for
532example::
533
534 >>> q = [2, 3]
535 >>> p = [1, q, 4]
536 >>> len(p)
537 3
538 >>> p[1]
539 [2, 3]
540 >>> p[1][0]
541 2
Guido van Rossum0616b792007-08-31 03:25:11 +0000542
543You can add something to the end of the list::
544
Georg Brandle4ac7502007-09-03 07:10:24 +0000545 >>> p[1].append('xtra')
Georg Brandl116aa622007-08-15 14:28:22 +0000546 >>> p
547 [1, [2, 3, 'xtra'], 4]
548 >>> q
549 [2, 3, 'xtra']
550
551Note that in the last example, ``p[1]`` and ``q`` really refer to the same
552object! We'll come back to *object semantics* later.
553
554
555.. _tut-firststeps:
556
557First Steps Towards Programming
558===============================
559
560Of course, we can use Python for more complicated tasks than adding two and two
561together. For instance, we can write an initial sub-sequence of the *Fibonacci*
562series as follows::
563
564 >>> # Fibonacci series:
565 ... # the sum of two elements defines the next
566 ... a, b = 0, 1
567 >>> while b < 10:
Georg Brandl22ec03c2008-01-07 17:32:13 +0000568 ... print(b)
569 ... a, b = b, a+b
Georg Brandl48310cd2009-01-03 21:18:54 +0000570 ...
Georg Brandl116aa622007-08-15 14:28:22 +0000571 1
572 1
573 2
574 3
575 5
576 8
577
578This example introduces several new features.
579
580* The first line contains a *multiple assignment*: the variables ``a`` and ``b``
581 simultaneously get the new values 0 and 1. On the last line this is used again,
582 demonstrating that the expressions on the right-hand side are all evaluated
583 first before any of the assignments take place. The right-hand side expressions
584 are evaluated from the left to the right.
585
586* The :keyword:`while` loop executes as long as the condition (here: ``b < 10``)
587 remains true. In Python, like in C, any non-zero integer value is true; zero is
588 false. The condition may also be a string or list value, in fact any sequence;
589 anything with a non-zero length is true, empty sequences are false. The test
590 used in the example is a simple comparison. The standard comparison operators
591 are written the same as in C: ``<`` (less than), ``>`` (greater than), ``==``
592 (equal to), ``<=`` (less than or equal to), ``>=`` (greater than or equal to)
593 and ``!=`` (not equal to).
594
595* The *body* of the loop is *indented*: indentation is Python's way of grouping
596 statements. Python does not (yet!) provide an intelligent input line editing
597 facility, so you have to type a tab or space(s) for each indented line. In
598 practice you will prepare more complicated input for Python with a text editor;
599 most text editors have an auto-indent facility. When a compound statement is
600 entered interactively, it must be followed by a blank line to indicate
601 completion (since the parser cannot guess when you have typed the last line).
602 Note that each line within a basic block must be indented by the same amount.
603
Guido van Rossum0616b792007-08-31 03:25:11 +0000604* The :func:`print` function writes the value of the expression(s) it is
Georg Brandl116aa622007-08-15 14:28:22 +0000605 given. It differs from just writing the expression you want to write (as we did
Georg Brandl48310cd2009-01-03 21:18:54 +0000606 earlier in the calculator examples) in the way it handles multiple
607 expressions, floating point quantities,
Georg Brandl116aa622007-08-15 14:28:22 +0000608 and strings. Strings are printed without quotes, and a space is inserted
609 between items, so you can format things nicely, like this::
610
611 >>> i = 256*256
Guido van Rossum0616b792007-08-31 03:25:11 +0000612 >>> print('The value of i is', i)
Georg Brandl116aa622007-08-15 14:28:22 +0000613 The value of i is 65536
614
Georg Brandl11e18b02008-08-05 09:04:16 +0000615 The keyword *end* can be used to avoid the newline after the output, or end
616 the output with a different string::
Georg Brandl116aa622007-08-15 14:28:22 +0000617
618 >>> a, b = 0, 1
619 >>> while b < 1000:
Terry Reedy02a807e2010-11-12 04:22:22 +0000620 ... print(b, end=',')
Georg Brandl116aa622007-08-15 14:28:22 +0000621 ... a, b = b, a+b
Georg Brandl48310cd2009-01-03 21:18:54 +0000622 ...
Terry Reedy02a807e2010-11-12 04:22:22 +0000623 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,