blob: 66c5a7c5d0107efdaa3487733ed73440156e1995 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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 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,
Christian Heimes5b5e81c2007-12-31 16:14:33 +000016``#``, and extend to the end of the physical line. A comment may appear at
Georg Brandl116aa622007-08-15 14:28:22 +000017the start 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 Brandl116aa622007-08-15 14:28:22 +000019
20Some examples::
21
22 # this is the first comment
23 SPAM = 1 # and this is the second comment
24 # ... and now a third!
25 STRING = "# This is not a comment."
26
27
28.. _tut-calculator:
29
30Using Python as a Calculator
31============================
32
33Let's try some simple Python commands. Start the interpreter and wait for the
34primary prompt, ``>>>``. (It shouldn't take long.)
35
36
37.. _tut-numbers:
38
39Numbers
40-------
41
42The interpreter acts as a simple calculator: you can type an expression at it
43and it will write the value. Expression syntax is straightforward: the
44operators ``+``, ``-``, ``*`` and ``/`` work just like in most other languages
45(for example, Pascal or C); parentheses can be used for grouping. For example::
46
47 >>> 2+2
48 4
49 >>> # This is a comment
50 ... 2+2
51 4
52 >>> 2+2 # and a comment on the same line as code
53 4
54 >>> (50-5*6)/4
Guido van Rossum0616b792007-08-31 03:25:11 +000055 5.0
56 >>> 8/5 # Fractions aren't lost when dividing integers
57 1.6000000000000001
58
59Note: You might not see exactly the same result; floating point results can
60differ from one machine to another. We will say more later about controlling
61the appearance of floating point output; what we see here is the most
62informative display but not as easy to read as we would get with::
63
64 >>> print(8/5)
65 1.6
66
67For clarity in this tutorial we will show the simpler floating point output
68unless we are specifically discussing output formatting, and explain later
69why these two ways of displaying floating point data come to be different.
70See :ref:`tut-fp-issues` for a full discussion.
71
72To do integer division and get an integer result,
73discarding any fractional result, there is another operator, ``//``::
74
Georg Brandl116aa622007-08-15 14:28:22 +000075 >>> # Integer division returns the floor:
Guido van Rossum0616b792007-08-31 03:25:11 +000076 ... 7//3
Georg Brandl116aa622007-08-15 14:28:22 +000077 2
Guido van Rossum0616b792007-08-31 03:25:11 +000078 >>> 7//-3
Georg Brandl116aa622007-08-15 14:28:22 +000079 -3
80
81The equal sign (``'='``) is used to assign a value to a variable. Afterwards, no
82result is displayed before the next interactive prompt::
83
84 >>> width = 20
85 >>> height = 5*9
86 >>> width * height
87 900
88
89A value can be assigned to several variables simultaneously::
90
91 >>> x = y = z = 0 # Zero x, y and z
92 >>> x
93 0
94 >>> y
95 0
96 >>> z
97 0
98
99There is full support for floating point; operators with mixed type operands
100convert the integer operand to floating point::
101
102 >>> 3 * 3.75 / 1.5
103 7.5
104 >>> 7.0 / 2
105 3.5
106
107Complex numbers are also supported; imaginary numbers are written with a suffix
108of ``j`` or ``J``. Complex numbers with a nonzero real component are written as
109``(real+imagj)``, or can be created with the ``complex(real, imag)`` function.
110::
111
112 >>> 1j * 1J
113 (-1+0j)
Georg Brandle4ac7502007-09-03 07:10:24 +0000114 >>> 1j * complex(0, 1)
Georg Brandl116aa622007-08-15 14:28:22 +0000115 (-1+0j)
116 >>> 3+1j*3
117 (3+3j)
118 >>> (3+1j)*3
119 (9+3j)
120 >>> (1+2j)/(1+1j)
121 (1.5+0.5j)
122
123Complex numbers are always represented as two floating point numbers, the real
124and imaginary part. To extract these parts from a complex number *z*, use
125``z.real`` and ``z.imag``. ::
126
127 >>> a=1.5+0.5j
128 >>> a.real
129 1.5
130 >>> a.imag
131 0.5
132
133The conversion functions to floating point and integer (:func:`float`,
Georg Brandl2d2590d2007-09-28 13:13:35 +0000134:func:`int`) don't work for complex numbers --- there is not one correct way to
135convert a complex number to a real number. Use ``abs(z)`` to get its magnitude
136(as a float) or ``z.real`` to get its real part::
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138 >>> a=3.0+4.0j
139 >>> float(a)
140 Traceback (most recent call last):
141 File "<stdin>", line 1, in ?
142 TypeError: can't convert complex to float; use abs(z)
143 >>> a.real
144 3.0
145 >>> a.imag
146 4.0
147 >>> abs(a) # sqrt(a.real**2 + a.imag**2)
148 5.0
149 >>>
150
151In interactive mode, the last printed expression is assigned to the variable
152``_``. This means that when you are using Python as a desk calculator, it is
153somewhat easier to continue calculations, for example::
154
155 >>> tax = 12.5 / 100
156 >>> price = 100.50
157 >>> price * tax
158 12.5625
159 >>> price + _
160 113.0625
161 >>> round(_, 2)
162 113.06
163 >>>
164
165This variable should be treated as read-only by the user. Don't explicitly
166assign a value to it --- you would create an independent local variable with the
167same name masking the built-in variable with its magic behavior.
168
169
170.. _tut-strings:
171
172Strings
173-------
174
175Besides numbers, Python can also manipulate strings, which can be expressed in
176several ways. They can be enclosed in single quotes or double quotes::
177
178 >>> 'spam eggs'
179 'spam eggs'
180 >>> 'doesn\'t'
181 "doesn't"
182 >>> "doesn't"
183 "doesn't"
184 >>> '"Yes," he said.'
185 '"Yes," he said.'
186 >>> "\"Yes,\" he said."
187 '"Yes," he said.'
188 >>> '"Isn\'t," she said.'
189 '"Isn\'t," she said.'
190
Guido van Rossum0616b792007-08-31 03:25:11 +0000191The interpreter prints the result of string operations in the same way as they
192are typed for input: inside quotes, and with quotes and other funny characters
193escaped by backslashes, to show the precise value. The string is enclosed in
194double quotes if the string contains a single quote and no double quotes, else
195it's enclosed in single quotes. Once again, the :func:`print` function
196produces the more readable output.
197
Georg Brandl116aa622007-08-15 14:28:22 +0000198String literals can span multiple lines in several ways. Continuation lines can
199be used, with a backslash as the last character on the line indicating that the
200next line is a logical continuation of the line::
201
202 hello = "This is a rather long string containing\n\
203 several lines of text just as you would do in C.\n\
204 Note that whitespace at the beginning of the line is\
205 significant."
206
Guido van Rossum0616b792007-08-31 03:25:11 +0000207 print(hello)
Georg Brandl116aa622007-08-15 14:28:22 +0000208
209Note that newlines still need to be embedded in the string using ``\n``; the
210newline following the trailing backslash is discarded. This example would print
211the following::
212
213 This is a rather long string containing
214 several lines of text just as you would do in C.
215 Note that whitespace at the beginning of the line is significant.
216
217If we make the string literal a "raw" string, however, the ``\n`` sequences are
218not converted to newlines, but the backslash at the end of the line, and the
219newline character in the source, are both included in the string as data. Thus,
220the example::
221
222 hello = r"This is a rather long string containing\n\
223 several lines of text much as you would do in C."
224
Guido van Rossum0616b792007-08-31 03:25:11 +0000225 print(hello)
Georg Brandl116aa622007-08-15 14:28:22 +0000226
227would print::
228
229 This is a rather long string containing\n\
230 several lines of text much as you would do in C.
231
232Or, strings can be surrounded in a pair of matching triple-quotes: ``"""`` or
233``'''``. End of lines do not need to be escaped when using triple-quotes, but
234they will be included in the string. ::
235
Guido van Rossum0616b792007-08-31 03:25:11 +0000236 print("""
Georg Brandl116aa622007-08-15 14:28:22 +0000237 Usage: thingy [OPTIONS]
238 -h Display this usage message
239 -H hostname Hostname to connect to
Guido van Rossum0616b792007-08-31 03:25:11 +0000240 """)
Georg Brandl116aa622007-08-15 14:28:22 +0000241
242produces the following output::
243
244 Usage: thingy [OPTIONS]
245 -h Display this usage message
246 -H hostname Hostname to connect to
247
Georg Brandl116aa622007-08-15 14:28:22 +0000248
249Strings can be concatenated (glued together) with the ``+`` operator, and
250repeated with ``*``::
251
252 >>> word = 'Help' + 'A'
253 >>> word
254 'HelpA'
255 >>> '<' + word*5 + '>'
256 '<HelpAHelpAHelpAHelpAHelpA>'
257
258Two string literals next to each other are automatically concatenated; the first
259line above could also have been written ``word = 'Help' 'A'``; this only works
260with two literals, not with arbitrary string expressions::
261
262 >>> 'str' 'ing' # <- This is ok
263 'string'
264 >>> 'str'.strip() + 'ing' # <- This is ok
265 'string'
266 >>> 'str'.strip() 'ing' # <- This is invalid
267 File "<stdin>", line 1, in ?
268 'str'.strip() 'ing'
269 ^
270 SyntaxError: invalid syntax
271
272Strings can be subscripted (indexed); like in C, the first character of a string
273has subscript (index) 0. There is no separate character type; a character is
Georg Brandle4ac7502007-09-03 07:10:24 +0000274simply a string of size one. As in the Icon programming language, substrings
275can be specified with the *slice notation*: two indices separated by a colon.
276::
Georg Brandl116aa622007-08-15 14:28:22 +0000277
278 >>> word[4]
279 'A'
280 >>> word[0:2]
281 'He'
282 >>> word[2:4]
283 'lp'
284
285Slice indices have useful defaults; an omitted first index defaults to zero, an
286omitted second index defaults to the size of the string being sliced. ::
287
288 >>> word[:2] # The first two characters
289 'He'
290 >>> word[2:] # Everything except the first two characters
291 'lpA'
292
293Unlike a C string, Python strings cannot be changed. Assigning to an indexed
294position in the string results in an error::
295
296 >>> word[0] = 'x'
297 Traceback (most recent call last):
298 File "<stdin>", line 1, in ?
Guido van Rossum0616b792007-08-31 03:25:11 +0000299 TypeError: 'str' object doesn't support item assignment
Georg Brandl116aa622007-08-15 14:28:22 +0000300 >>> word[:1] = 'Splat'
301 Traceback (most recent call last):
302 File "<stdin>", line 1, in ?
Guido van Rossum0616b792007-08-31 03:25:11 +0000303 TypeError: 'str' object doesn't support slice assignment
Georg Brandl116aa622007-08-15 14:28:22 +0000304
305However, creating a new string with the combined content is easy and efficient::
306
307 >>> 'x' + word[1:]
308 'xelpA'
309 >>> 'Splat' + word[4]
310 'SplatA'
311
312Here's a useful invariant of slice operations: ``s[:i] + s[i:]`` equals ``s``.
313::
314
315 >>> word[:2] + word[2:]
316 'HelpA'
317 >>> word[:3] + word[3:]
318 'HelpA'
319
320Degenerate slice indices are handled gracefully: an index that is too large is
321replaced by the string size, an upper bound smaller than the lower bound returns
322an empty string. ::
323
324 >>> word[1:100]
325 'elpA'
326 >>> word[10:]
327 ''
328 >>> word[2:1]
329 ''
330
331Indices may be negative numbers, to start counting from the right. For example::
332
333 >>> word[-1] # The last character
334 'A'
335 >>> word[-2] # The last-but-one character
336 'p'
337 >>> word[-2:] # The last two characters
338 'pA'
339 >>> word[:-2] # Everything except the last two characters
340 'Hel'
341
342But note that -0 is really the same as 0, so it does not count from the right!
343::
344
345 >>> word[-0] # (since -0 equals 0)
346 'H'
347
348Out-of-range negative slice indices are truncated, but don't try this for
349single-element (non-slice) indices::
350
351 >>> word[-100:]
352 'HelpA'
353 >>> word[-10] # error
354 Traceback (most recent call last):
355 File "<stdin>", line 1, in ?
356 IndexError: string index out of range
357
358One way to remember how slices work is to think of the indices as pointing
359*between* characters, with the left edge of the first character numbered 0.
360Then the right edge of the last character of a string of *n* characters has
361index *n*, for example::
362
363 +---+---+---+---+---+
364 | H | e | l | p | A |
365 +---+---+---+---+---+
366 0 1 2 3 4 5
367 -5 -4 -3 -2 -1
368
369The first row of numbers gives the position of the indices 0...5 in the string;
370the second row gives the corresponding negative indices. The slice from *i* to
371*j* consists of all characters between the edges labeled *i* and *j*,
372respectively.
373
374For non-negative indices, the length of a slice is the difference of the
375indices, if both are within bounds. For example, the length of ``word[1:3]`` is
3762.
377
378The built-in function :func:`len` returns the length of a string::
379
380 >>> s = 'supercalifragilisticexpialidocious'
381 >>> len(s)
382 34
383
384
385.. seealso::
386
387 :ref:`typesseq`
Guido van Rossum0616b792007-08-31 03:25:11 +0000388 Strings are examples of *sequence types*, and support the common
389 operations supported by such types.
Georg Brandl116aa622007-08-15 14:28:22 +0000390
391 :ref:`string-methods`
Guido van Rossum0616b792007-08-31 03:25:11 +0000392 Strings support a large number of methods for
Georg Brandl116aa622007-08-15 14:28:22 +0000393 basic transformations and searching.
394
395 :ref:`string-formatting`
Benjamin Petersone6f00632008-05-26 01:03:56 +0000396 Information about string formatting with :meth:`str.format` is described
397 here.
398
399 :ref:`old-string-formatting`
400 The old formatting operations invoked when strings and Unicode strings are
401 the left operand of the ``%`` operator are described in more detail here.
Georg Brandl116aa622007-08-15 14:28:22 +0000402
403
404.. _tut-unicodestrings:
405
Guido van Rossum0616b792007-08-31 03:25:11 +0000406About Unicode
407-------------
Georg Brandl116aa622007-08-15 14:28:22 +0000408
409.. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com>
410
411
Guido van Rossum0616b792007-08-31 03:25:11 +0000412Starting with Python 3.0 all strings support Unicode.
413(See http://www.unicode.org/)
Georg Brandl116aa622007-08-15 14:28:22 +0000414
415Unicode has the advantage of providing one ordinal for every character in every
416script used in modern and ancient texts. Previously, there were only 256
417possible ordinals for script characters. Texts were typically bound to a code
418page which mapped the ordinals to script characters. This lead to very much
419confusion especially with respect to internationalization (usually written as
420``i18n`` --- ``'i'`` + 18 characters + ``'n'``) of software. Unicode solves
421these problems by defining one code page for all scripts.
422
Guido van Rossum0616b792007-08-31 03:25:11 +0000423If you want to include special characters in a string,
Georg Brandl116aa622007-08-15 14:28:22 +0000424you can do so by using the Python *Unicode-Escape* encoding. The following
425example shows how::
426
Guido van Rossum0616b792007-08-31 03:25:11 +0000427 >>> 'Hello\u0020World !'
428 'Hello World !'
Georg Brandl116aa622007-08-15 14:28:22 +0000429
430The escape sequence ``\u0020`` indicates to insert the Unicode character with
431the ordinal value 0x0020 (the space character) at the given position.
432
433Other characters are interpreted by using their respective ordinal values
434directly as Unicode ordinals. If you have literal strings in the standard
435Latin-1 encoding that is used in many Western countries, you will find it
436convenient that the lower 256 characters of Unicode are the same as the 256
437characters of Latin-1.
438
Georg Brandl116aa622007-08-15 14:28:22 +0000439Apart from these standard encodings, Python provides a whole set of other ways
440of creating Unicode strings on the basis of a known encoding.
441
Guido van Rossum0616b792007-08-31 03:25:11 +0000442To convert a string into a sequence of bytes using a specific encoding,
443string objects provide an :func:`encode` method that takes one argument, the
Georg Brandl116aa622007-08-15 14:28:22 +0000444name of the encoding. Lowercase names for encodings are preferred. ::
445
Georg Brandlc3f5bad2007-08-31 06:46:05 +0000446 >>> "Äpfel".encode('utf-8')
447 b'\xc3\x84pfel'
Georg Brandl116aa622007-08-15 14:28:22 +0000448
449.. _tut-lists:
450
451Lists
452-----
453
454Python knows a number of *compound* data types, used to group together other
455values. The most versatile is the *list*, which can be written as a list of
456comma-separated values (items) between square brackets. List items need not all
457have the same type. ::
458
459 >>> a = ['spam', 'eggs', 100, 1234]
460 >>> a
461 ['spam', 'eggs', 100, 1234]
462
463Like string indices, list indices start at 0, and lists can be sliced,
464concatenated and so on::
465
466 >>> a[0]
467 'spam'
468 >>> a[3]
469 1234
470 >>> a[-2]
471 100
472 >>> a[1:-1]
473 ['eggs', 100]
474 >>> a[:2] + ['bacon', 2*2]
475 ['spam', 'eggs', 'bacon', 4]
476 >>> 3*a[:3] + ['Boo!']
477 ['spam', 'eggs', 100, 'spam', 'eggs', 100, 'spam', 'eggs', 100, 'Boo!']
478
479Unlike strings, which are *immutable*, it is possible to change individual
480elements of a list::
481
482 >>> a
483 ['spam', 'eggs', 100, 1234]
484 >>> a[2] = a[2] + 23
485 >>> a
486 ['spam', 'eggs', 123, 1234]
487
488Assignment to slices is also possible, and this can even change the size of the
489list or clear it entirely::
490
491 >>> # Replace some items:
492 ... a[0:2] = [1, 12]
493 >>> a
494 [1, 12, 123, 1234]
495 >>> # Remove some:
496 ... a[0:2] = []
497 >>> a
498 [123, 1234]
499 >>> # Insert some:
500 ... a[1:1] = ['bletch', 'xyzzy']
501 >>> a
502 [123, 'bletch', 'xyzzy', 1234]
503 >>> # Insert (a copy of) itself at the beginning
504 >>> a[:0] = a
505 >>> a
506 [123, 'bletch', 'xyzzy', 1234, 123, 'bletch', 'xyzzy', 1234]
507 >>> # Clear the list: replace all items with an empty list
508 >>> a[:] = []
509 >>> a
510 []
511
512The built-in function :func:`len` also applies to lists::
513
Guido van Rossum58da9312007-11-10 23:39:45 +0000514 >>> a = ['a', 'b', 'c', 'd']
Georg Brandl116aa622007-08-15 14:28:22 +0000515 >>> len(a)
Guido van Rossum58da9312007-11-10 23:39:45 +0000516 4
Georg Brandl116aa622007-08-15 14:28:22 +0000517
518It is possible to nest lists (create lists containing other lists), for
519example::
520
521 >>> q = [2, 3]
522 >>> p = [1, q, 4]
523 >>> len(p)
524 3
525 >>> p[1]
526 [2, 3]
527 >>> p[1][0]
528 2
Guido van Rossum0616b792007-08-31 03:25:11 +0000529
530You can add something to the end of the list::
531
Georg Brandle4ac7502007-09-03 07:10:24 +0000532 >>> p[1].append('xtra')
Georg Brandl116aa622007-08-15 14:28:22 +0000533 >>> p
534 [1, [2, 3, 'xtra'], 4]
535 >>> q
536 [2, 3, 'xtra']
537
538Note that in the last example, ``p[1]`` and ``q`` really refer to the same
539object! We'll come back to *object semantics* later.
540
541
542.. _tut-firststeps:
543
544First Steps Towards Programming
545===============================
546
547Of course, we can use Python for more complicated tasks than adding two and two
548together. For instance, we can write an initial sub-sequence of the *Fibonacci*
549series as follows::
550
551 >>> # Fibonacci series:
552 ... # the sum of two elements defines the next
553 ... a, b = 0, 1
554 >>> while b < 10:
Georg Brandl22ec03c2008-01-07 17:32:13 +0000555 ... print(b)
556 ... a, b = b, a+b
Georg Brandl116aa622007-08-15 14:28:22 +0000557 ...
558 1
559 1
560 2
561 3
562 5
563 8
564
565This example introduces several new features.
566
567* The first line contains a *multiple assignment*: the variables ``a`` and ``b``
568 simultaneously get the new values 0 and 1. On the last line this is used again,
569 demonstrating that the expressions on the right-hand side are all evaluated
570 first before any of the assignments take place. The right-hand side expressions
571 are evaluated from the left to the right.
572
573* The :keyword:`while` loop executes as long as the condition (here: ``b < 10``)
574 remains true. In Python, like in C, any non-zero integer value is true; zero is
575 false. The condition may also be a string or list value, in fact any sequence;
576 anything with a non-zero length is true, empty sequences are false. The test
577 used in the example is a simple comparison. The standard comparison operators
578 are written the same as in C: ``<`` (less than), ``>`` (greater than), ``==``
579 (equal to), ``<=`` (less than or equal to), ``>=`` (greater than or equal to)
580 and ``!=`` (not equal to).
581
582* The *body* of the loop is *indented*: indentation is Python's way of grouping
583 statements. Python does not (yet!) provide an intelligent input line editing
584 facility, so you have to type a tab or space(s) for each indented line. In
585 practice you will prepare more complicated input for Python with a text editor;
586 most text editors have an auto-indent facility. When a compound statement is
587 entered interactively, it must be followed by a blank line to indicate
588 completion (since the parser cannot guess when you have typed the last line).
589 Note that each line within a basic block must be indented by the same amount.
590
Guido van Rossum0616b792007-08-31 03:25:11 +0000591* The :func:`print` function writes the value of the expression(s) it is
Georg Brandl116aa622007-08-15 14:28:22 +0000592 given. It differs from just writing the expression you want to write (as we did
Guido van Rossum0616b792007-08-31 03:25:11 +0000593 earlier in the calculator examples) in the way it handles multiple
594 expressions, floating point quantities,
Georg Brandl116aa622007-08-15 14:28:22 +0000595 and strings. Strings are printed without quotes, and a space is inserted
596 between items, so you can format things nicely, like this::
597
598 >>> i = 256*256
Guido van Rossum0616b792007-08-31 03:25:11 +0000599 >>> print('The value of i is', i)
Georg Brandl116aa622007-08-15 14:28:22 +0000600 The value of i is 65536
601
Guido van Rossum0616b792007-08-31 03:25:11 +0000602 The keyword end can be used to avoid the newline after the output::
Georg Brandl116aa622007-08-15 14:28:22 +0000603
604 >>> a, b = 0, 1
605 >>> while b < 1000:
Guido van Rossum0616b792007-08-31 03:25:11 +0000606 ... print(b, ' ', end='')
Georg Brandl116aa622007-08-15 14:28:22 +0000607 ... a, b = b, a+b
608 ...
Guido van Rossum0616b792007-08-31 03:25:11 +0000609 >>> print()
Georg Brandl116aa622007-08-15 14:28:22 +0000610 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
611
Guido van Rossum0616b792007-08-31 03:25:11 +0000612 Note that nothing appeared after the loop ended, until we printed
613 a newline.
614