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