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