blob: b7be00ed7ad3273364a8dffb900f2c5f725f33b0 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001.. _tut-informal:
2
3**********************************
4An Informal Introduction to Python
5**********************************
6
7In the following examples, input and output are distinguished by the presence or
Zachary Ware5b1b38cb2014-07-01 14:25:34 -05008absence of prompts (:term:`>>>` and :term:`...`): to repeat the example, you must type
Georg Brandl8ec7f652007-08-15 14:28:01 +00009everything after the prompt, when the prompt appears; lines that do not begin
10with a prompt are output from the interpreter. Note that a secondary prompt on a
11line by itself in an example means you must type a blank line; this is used to
12end a multi-line command.
13
Georg Brandl8ec7f652007-08-15 14:28:01 +000014Many of the examples in this manual, even those entered at the interactive
15prompt, include comments. Comments in Python start with the hash character,
Georg Brandl3ce0dee2008-09-13 17:18:11 +000016``#``, and extend to the end of the physical line. A comment may appear at the
17start of a line or following whitespace or code, but not within a string
Georg Brandlb19be572007-12-29 10:57:00 +000018literal. A hash character within a string literal is just a hash character.
Georg Brandl3ce0dee2008-09-13 17:18:11 +000019Since comments are to clarify code and are not interpreted by Python, they may
20be omitted when typing in examples.
Georg Brandl8ec7f652007-08-15 14:28:01 +000021
22Some examples::
23
24 # this is the first comment
Zachary Ware5b1b38cb2014-07-01 14:25:34 -050025 spam = 1 # and this is the second comment
26 # ... and now a third!
27 text = "# This is not a comment because it's inside quotes."
Georg Brandl8ec7f652007-08-15 14:28:01 +000028
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
Zachary Ware5b1b38cb2014-07-01 14:25:34 -050047(for example, Pascal or C); parentheses (``()``) can be used for grouping.
48For example::
Georg Brandl8ec7f652007-08-15 14:28:01 +000049
Zachary Ware5b1b38cb2014-07-01 14:25:34 -050050 >>> 2 + 2
Georg Brandl8ec7f652007-08-15 14:28:01 +000051 4
Zachary Ware5b1b38cb2014-07-01 14:25:34 -050052 >>> 50 - 5*6
53 20
54 >>> (50 - 5.0*6) / 4
55 5.0
56 >>> 8 / 5.0
57 1.6
58
59The integer numbers (e.g. ``2``, ``4``, ``20``) have type :class:`int`,
60the ones with a fractional part (e.g. ``5.0``, ``1.6``) have type
61:class:`float`. We will see more about numeric types later in the tutorial.
62
63The return type of a division (``/``) operation depends on its operands. If
64both operands are of type :class:`int`, :term:`floor division` is performed
65and an :class:`int` is returned. If either operand is a :class:`float`,
66classic division is performed and a :class:`float` is returned. The ``//``
67operator is also provided for doing floor division no matter what the
68operands are. The remainder can be calculated with the ``%`` operator::
69
70 >>> 17 / 3 # int / int -> int
Georg Brandl8ec7f652007-08-15 14:28:01 +000071 5
Zachary Ware5b1b38cb2014-07-01 14:25:34 -050072 >>> 17 / 3.0 # int / float -> float
73 5.666666666666667
74 >>> 17 // 3.0 # explicit floor division discards the fractional part
75 5.0
76 >>> 17 % 3 # the % operator returns the remainder of the division
Georg Brandl8ec7f652007-08-15 14:28:01 +000077 2
Zachary Ware5b1b38cb2014-07-01 14:25:34 -050078 >>> 5 * 3 + 2 # result * divisor + remainder
79 17
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
Zachary Ware5b1b38cb2014-07-01 14:25:34 -050081With Python, it is possible to use the ``**`` operator to calculate powers [#]_::
82
83 >>> 5 ** 2 # 5 squared
84 25
85 >>> 2 ** 7 # 2 to the power of 7
86 128
87
88The equal sign (``=``) is used to assign a value to a variable. Afterwards, no
Georg Brandl8ec7f652007-08-15 14:28:01 +000089result is displayed before the next interactive prompt::
90
91 >>> width = 20
Zachary Ware5b1b38cb2014-07-01 14:25:34 -050092 >>> height = 5 * 9
Georg Brandl8ec7f652007-08-15 14:28:01 +000093 >>> width * height
94 900
95
Zachary Ware5b1b38cb2014-07-01 14:25:34 -050096If a variable is not "defined" (assigned a value), trying to use it will
97give you an error::
Georg Brandl3ce0dee2008-09-13 17:18:11 +000098
Chris Jerdonek3dec4492012-09-24 19:33:32 -070099 >>> n # try to access an undefined variable
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000100 Traceback (most recent call last):
Georg Brandl3ce0dee2008-09-13 17:18:11 +0000101 File "<stdin>", line 1, in <module>
102 NameError: name 'n' is not defined
103
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104There is full support for floating point; operators with mixed type operands
105convert the integer operand to floating point::
106
107 >>> 3 * 3.75 / 1.5
108 7.5
109 >>> 7.0 / 2
110 3.5
111
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112In interactive mode, the last printed expression is assigned to the variable
113``_``. This means that when you are using Python as a desk calculator, it is
114somewhat easier to continue calculations, for example::
115
116 >>> tax = 12.5 / 100
117 >>> price = 100.50
118 >>> price * tax
119 12.5625
120 >>> price + _
121 113.0625
122 >>> round(_, 2)
123 113.06
Georg Brandl8ec7f652007-08-15 14:28:01 +0000124
125This variable should be treated as read-only by the user. Don't explicitly
126assign a value to it --- you would create an independent local variable with the
127same name masking the built-in variable with its magic behavior.
128
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500129In addition to :class:`int` and :class:`float`, Python supports other types of
130numbers, such as :class:`~decimal.Decimal` and :class:`~fractions.Fraction`.
131Python also has built-in support for :ref:`complex numbers <typesnumeric>`,
132and uses the ``j`` or ``J`` suffix to indicate the imaginary part
133(e.g. ``3+5j``).
134
Georg Brandl8ec7f652007-08-15 14:28:01 +0000135
136.. _tut-strings:
137
138Strings
139-------
140
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500141Besides numbers, Python can also manipulate strings, which can be expressed
142in several ways. They can be enclosed in single quotes (``'...'``) or
143double quotes (``"..."``) with the same result [#]_. ``\`` can be used
144to escape quotes::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000145
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500146 >>> 'spam eggs' # single quotes
Georg Brandl8ec7f652007-08-15 14:28:01 +0000147 'spam eggs'
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500148 >>> 'doesn\'t' # use \' to escape the single quote...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000149 "doesn't"
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500150 >>> "doesn't" # ...or use double quotes instead
Georg Brandl8ec7f652007-08-15 14:28:01 +0000151 "doesn't"
152 >>> '"Yes," he said.'
153 '"Yes," he said.'
154 >>> "\"Yes,\" he said."
155 '"Yes," he said.'
156 >>> '"Isn\'t," she said.'
157 '"Isn\'t," she said.'
158
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500159In the interactive interpreter, the output string is enclosed in quotes and
160special characters are escaped with backslashes. While this might sometimes
161look different from the input (the enclosing quotes could change), the two
162strings are equivalent. The string is enclosed in double quotes if
163the string contains a single quote and no double quotes, otherwise it is
164enclosed in single quotes. The :keyword:`print` statement produces a more
165readable output, by omitting the enclosing quotes and by printing escaped
166and special characters::
Senthil Kumaranbf024292010-11-08 02:12:57 +0000167
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500168 >>> '"Isn\'t," she said.'
169 '"Isn\'t," she said.'
170 >>> print '"Isn\'t," she said.'
171 "Isn't," she said.
172 >>> s = 'First line.\nSecond line.' # \n means newline
Benjamin Petersonad5ddf72015-02-04 22:06:55 -0500173 >>> s # without print, \n is included in the output
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500174 'First line.\nSecond line.'
175 >>> print s # with print, \n produces a new line
176 First line.
177 Second line.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000178
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500179If you don't want characters prefaced by ``\`` to be interpreted as
180special characters, you can use *raw strings* by adding an ``r`` before
181the first quote::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000182
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500183 >>> print 'C:\some\name' # here \n means newline!
184 C:\some
185 ame
186 >>> print r'C:\some\name' # note the r before the quote
187 C:\some\name
Georg Brandl8ec7f652007-08-15 14:28:01 +0000188
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500189String literals can span multiple lines. One way is using triple-quotes:
190``"""..."""`` or ``'''...'''``. End of lines are automatically
191included in the string, but it's possible to prevent this by adding a ``\`` at
192the end of the line. The following example::
Georg Brandlbf58d802009-09-03 07:27:26 +0000193
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500194 print """\
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000195 Usage: thingy [OPTIONS]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000196 -h Display this usage message
197 -H hostname Hostname to connect to
198 """
199
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500200produces the following output (note that the initial newline is not included):
Georg Brandlbf58d802009-09-03 07:27:26 +0000201
202.. code-block:: text
Georg Brandl8ec7f652007-08-15 14:28:01 +0000203
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000204 Usage: thingy [OPTIONS]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000205 -h Display this usage message
206 -H hostname Hostname to connect to
207
Georg Brandl8ec7f652007-08-15 14:28:01 +0000208Strings can be concatenated (glued together) with the ``+`` operator, and
209repeated with ``*``::
210
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500211 >>> # 3 times 'un', followed by 'ium'
212 >>> 3 * 'un' + 'ium'
213 'unununium'
Georg Brandl8ec7f652007-08-15 14:28:01 +0000214
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500215Two or more *string literals* (i.e. the ones enclosed between quotes) next
216to each other are automatically concatenated. ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000217
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500218 >>> 'Py' 'thon'
219 'Python'
220
221This only works with two literals though, not with variables or expressions::
222
223 >>> prefix = 'Py'
224 >>> prefix 'thon' # can't concatenate a variable and a string literal
225 ...
226 SyntaxError: invalid syntax
227 >>> ('un' * 3) 'ium'
228 ...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000229 SyntaxError: invalid syntax
230
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500231If you want to concatenate variables or a variable and a literal, use ``+``::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000232
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500233 >>> prefix + 'thon'
234 'Python'
235
236This feature is particularly useful when you want to break long strings::
237
238 >>> text = ('Put several strings within parentheses '
Serhiy Storchaka12d547a2016-05-10 13:45:32 +0300239 ... 'to have them joined together.')
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500240 >>> text
241 'Put several strings within parentheses to have them joined together.'
242
243Strings can be *indexed* (subscripted), with the first character having index 0.
244There is no separate character type; a character is simply a string of size
245one::
246
247 >>> word = 'Python'
248 >>> word[0] # character in position 0
249 'P'
250 >>> word[5] # character in position 5
251 'n'
252
253Indices may also be negative numbers, to start counting from the right::
254
255 >>> word[-1] # last character
256 'n'
257 >>> word[-2] # second-last character
258 'o'
259 >>> word[-6]
260 'P'
261
262Note that since -0 is the same as 0, negative indices start from -1.
263
264In addition to indexing, *slicing* is also supported. While indexing is used
265to obtain individual characters, *slicing* allows you to obtain a substring::
266
267 >>> word[0:2] # characters from position 0 (included) to 2 (excluded)
268 'Py'
269 >>> word[2:5] # characters from position 2 (included) to 5 (excluded)
270 'tho'
271
272Note how the start is always included, and the end always excluded. This
273makes sure that ``s[:i] + s[i:]`` is always equal to ``s``::
274
275 >>> word[:2] + word[2:]
276 'Python'
277 >>> word[:4] + word[4:]
278 'Python'
Georg Brandl8ec7f652007-08-15 14:28:01 +0000279
280Slice indices have useful defaults; an omitted first index defaults to zero, an
281omitted second index defaults to the size of the string being sliced. ::
282
Serhiy Storchaka12d547a2016-05-10 13:45:32 +0300283 >>> word[:2] # character from the beginning to position 2 (excluded)
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500284 'Py'
Serhiy Storchaka12d547a2016-05-10 13:45:32 +0300285 >>> word[4:] # characters from position 4 (included) to the end
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500286 'on'
Serhiy Storchaka12d547a2016-05-10 13:45:32 +0300287 >>> word[-2:] # characters from the second-last (included) to the end
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500288 'on'
Georg Brandl8ec7f652007-08-15 14:28:01 +0000289
290One way to remember how slices work is to think of the indices as pointing
291*between* characters, with the left edge of the first character numbered 0.
292Then the right edge of the last character of a string of *n* characters has
293index *n*, for example::
294
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500295 +---+---+---+---+---+---+
296 | P | y | t | h | o | n |
297 +---+---+---+---+---+---+
298 0 1 2 3 4 5 6
299 -6 -5 -4 -3 -2 -1
Georg Brandl8ec7f652007-08-15 14:28:01 +0000300
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500301The first row of numbers gives the position of the indices 0...6 in the string;
Georg Brandl8ec7f652007-08-15 14:28:01 +0000302the second row gives the corresponding negative indices. The slice from *i* to
303*j* consists of all characters between the edges labeled *i* and *j*,
304respectively.
305
306For non-negative indices, the length of a slice is the difference of the
307indices, if both are within bounds. For example, the length of ``word[1:3]`` is
3082.
309
Serhiy Storchaka9a118f12016-04-17 09:37:36 +0300310Attempting to use an index that is too large will result in an error::
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500311
Berker Peksagd4f5c142014-12-17 14:59:33 +0200312 >>> word[42] # the word only has 6 characters
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500313 Traceback (most recent call last):
314 File "<stdin>", line 1, in <module>
315 IndexError: string index out of range
316
317However, out of range slice indexes are handled gracefully when used for
318slicing::
319
320 >>> word[4:42]
321 'on'
322 >>> word[42:]
323 ''
324
325Python strings cannot be changed --- they are :term:`immutable`.
326Therefore, assigning to an indexed position in the string results in an error::
327
328 >>> word[0] = 'J'
329 ...
330 TypeError: 'str' object does not support item assignment
331 >>> word[2:] = 'py'
332 ...
333 TypeError: 'str' object does not support item assignment
334
335If you need a different string, you should create a new one::
336
337 >>> 'J' + word[1:]
338 'Jython'
339 >>> word[:2] + 'py'
340 'Pypy'
341
Georg Brandl8ec7f652007-08-15 14:28:01 +0000342The built-in function :func:`len` returns the length of a string::
343
344 >>> s = 'supercalifragilisticexpialidocious'
345 >>> len(s)
346 34
347
348
349.. seealso::
350
351 :ref:`typesseq`
352 Strings, and the Unicode strings described in the next section, are
353 examples of *sequence types*, and support the common operations supported
354 by such types.
355
356 :ref:`string-methods`
357 Both strings and Unicode strings support a large number of methods for
358 basic transformations and searching.
359
Martin Panter00f19ef2016-02-08 01:34:09 +0000360 :ref:`formatstrings`
361 Information about string formatting with :meth:`str.format`.
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000362
Georg Brandl8ec7f652007-08-15 14:28:01 +0000363 :ref:`string-formatting`
Benjamin Petersonf9ef9882008-05-26 00:54:22 +0000364 The old formatting operations invoked when strings and Unicode strings are
365 the left operand of the ``%`` operator are described in more detail here.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000366
367
368.. _tut-unicodestrings:
369
370Unicode Strings
371---------------
372
373.. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com>
374
375
376Starting with Python 2.0 a new data type for storing text data is available to
377the programmer: the Unicode object. It can be used to store and manipulate
378Unicode data (see http://www.unicode.org/) and integrates well with the existing
379string objects, providing auto-conversions where necessary.
380
381Unicode has the advantage of providing one ordinal for every character in every
382script used in modern and ancient texts. Previously, there were only 256
383possible ordinals for script characters. Texts were typically bound to a code
384page which mapped the ordinals to script characters. This lead to very much
385confusion especially with respect to internationalization (usually written as
386``i18n`` --- ``'i'`` + 18 characters + ``'n'``) of software. Unicode solves
387these problems by defining one code page for all scripts.
388
389Creating Unicode strings in Python is just as simple as creating normal
390strings::
391
392 >>> u'Hello World !'
393 u'Hello World !'
394
395The small ``'u'`` in front of the quote indicates that a Unicode string is
396supposed to be created. If you want to include special characters in the string,
397you can do so by using the Python *Unicode-Escape* encoding. The following
398example shows how::
399
400 >>> u'Hello\u0020World !'
401 u'Hello World !'
402
403The escape sequence ``\u0020`` indicates to insert the Unicode character with
404the ordinal value 0x0020 (the space character) at the given position.
405
406Other characters are interpreted by using their respective ordinal values
407directly as Unicode ordinals. If you have literal strings in the standard
408Latin-1 encoding that is used in many Western countries, you will find it
409convenient that the lower 256 characters of Unicode are the same as the 256
410characters of Latin-1.
411
412For experts, there is also a raw mode just like the one for normal strings. You
413have to prefix the opening quote with 'ur' to have Python use the
414*Raw-Unicode-Escape* encoding. It will only apply the above ``\uXXXX``
415conversion if there is an uneven number of backslashes in front of the small
416'u'. ::
417
418 >>> ur'Hello\u0020World !'
419 u'Hello World !'
420 >>> ur'Hello\\u0020World !'
421 u'Hello\\\\u0020World !'
422
423The raw mode is most useful when you have to enter lots of backslashes, as can
424be necessary in regular expressions.
425
426Apart from these standard encodings, Python provides a whole set of other ways
427of creating Unicode strings on the basis of a known encoding.
428
429.. index:: builtin: unicode
430
431The built-in function :func:`unicode` provides access to all registered Unicode
432codecs (COders and DECoders). Some of the more well known encodings which these
433codecs can convert are *Latin-1*, *ASCII*, *UTF-8*, and *UTF-16*. The latter two
434are variable-length encodings that store each Unicode character in one or more
435bytes. The default encoding is normally set to ASCII, which passes through
436characters in the range 0 to 127 and rejects any other characters with an error.
437When a Unicode string is printed, written to a file, or converted with
438:func:`str`, conversion takes place using this default encoding. ::
439
440 >>> u"abc"
441 u'abc'
442 >>> str(u"abc")
443 'abc'
444 >>> u"äöü"
445 u'\xe4\xf6\xfc'
446 >>> str(u"äöü")
447 Traceback (most recent call last):
448 File "<stdin>", line 1, in ?
449 UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-2: ordinal not in range(128)
450
451To convert a Unicode string into an 8-bit string using a specific encoding,
452Unicode objects provide an :func:`encode` method that takes one argument, the
453name of the encoding. Lowercase names for encodings are preferred. ::
454
455 >>> u"äöü".encode('utf-8')
456 '\xc3\xa4\xc3\xb6\xc3\xbc'
457
458If you have data in a specific encoding and want to produce a corresponding
459Unicode string from it, you can use the :func:`unicode` function with the
460encoding name as the second argument. ::
461
462 >>> unicode('\xc3\xa4\xc3\xb6\xc3\xbc', 'utf-8')
463 u'\xe4\xf6\xfc'
464
465
466.. _tut-lists:
467
468Lists
469-----
470
471Python knows a number of *compound* data types, used to group together other
472values. The most versatile is the *list*, which can be written as a list of
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500473comma-separated values (items) between square brackets. Lists might contain
474items of different types, but usually the items all have the same type. ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000475
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500476 >>> squares = [1, 4, 9, 16, 25]
477 >>> squares
478 [1, 4, 9, 16, 25]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000479
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500480Like strings (and all other built-in :term:`sequence` type), lists can be
481indexed and sliced::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000482
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500483 >>> squares[0] # indexing returns the item
484 1
485 >>> squares[-1]
486 25
487 >>> squares[-3:] # slicing returns a new list
488 [9, 16, 25]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000489
Georg Brandl0fcd8822010-03-21 09:17:41 +0000490All slice operations return a new list containing the requested elements. This
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500491means that the following slice returns a new (shallow) copy of the list::
Georg Brandl0fcd8822010-03-21 09:17:41 +0000492
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500493 >>> squares[:]
494 [1, 4, 9, 16, 25]
Georg Brandl0fcd8822010-03-21 09:17:41 +0000495
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500496Lists also supports operations like concatenation::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000497
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500498 >>> squares + [36, 49, 64, 81, 100]
499 [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
500
501Unlike strings, which are :term:`immutable`, lists are a :term:`mutable`
502type, i.e. it is possible to change their content::
503
504 >>> cubes = [1, 8, 27, 65, 125] # something's wrong here
505 >>> 4 ** 3 # the cube of 4 is 64, not 65!
506 64
507 >>> cubes[3] = 64 # replace the wrong value
508 >>> cubes
509 [1, 8, 27, 64, 125]
510
511You can also add new items at the end of the list, by using
512the :meth:`~list.append` *method* (we will see more about methods later)::
513
514 >>> cubes.append(216) # add the cube of 6
515 >>> cubes.append(7 ** 3) # and the cube of 7
516 >>> cubes
517 [1, 8, 27, 64, 125, 216, 343]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000518
519Assignment to slices is also possible, and this can even change the size of the
520list or clear it entirely::
521
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500522 >>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
523 >>> letters
524 ['a', 'b', 'c', 'd', 'e', 'f', 'g']
525 >>> # replace some values
526 >>> letters[2:5] = ['C', 'D', 'E']
527 >>> letters
528 ['a', 'b', 'C', 'D', 'E', 'f', 'g']
529 >>> # now remove them
530 >>> letters[2:5] = []
531 >>> letters
532 ['a', 'b', 'f', 'g']
533 >>> # clear the list by replacing all the elements with an empty list
534 >>> letters[:] = []
535 >>> letters
Georg Brandl8ec7f652007-08-15 14:28:01 +0000536 []
537
538The built-in function :func:`len` also applies to lists::
539
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500540 >>> letters = ['a', 'b', 'c', 'd']
541 >>> len(letters)
Georg Brandl87426cb2007-11-09 13:08:48 +0000542 4
Georg Brandl8ec7f652007-08-15 14:28:01 +0000543
544It is possible to nest lists (create lists containing other lists), for
545example::
546
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500547 >>> a = ['a', 'b', 'c']
548 >>> n = [1, 2, 3]
549 >>> x = [a, n]
550 >>> x
551 [['a', 'b', 'c'], [1, 2, 3]]
552 >>> x[0]
553 ['a', 'b', 'c']
554 >>> x[0][1]
555 'b'
Georg Brandl8ec7f652007-08-15 14:28:01 +0000556
557.. _tut-firststeps:
558
559First Steps Towards Programming
560===============================
561
562Of course, we can use Python for more complicated tasks than adding two and two
563together. For instance, we can write an initial sub-sequence of the *Fibonacci*
564series as follows::
565
566 >>> # Fibonacci series:
567 ... # the sum of two elements defines the next
568 ... a, b = 0, 1
569 >>> while b < 10:
Georg Brandl35f88612008-01-06 22:05:40 +0000570 ... print b
571 ... a, b = b, a+b
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000572 ...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000573 1
574 1
575 2
576 3
577 5
578 8
579
580This example introduces several new features.
581
582* The first line contains a *multiple assignment*: the variables ``a`` and ``b``
583 simultaneously get the new values 0 and 1. On the last line this is used again,
584 demonstrating that the expressions on the right-hand side are all evaluated
585 first before any of the assignments take place. The right-hand side expressions
586 are evaluated from the left to the right.
587
588* The :keyword:`while` loop executes as long as the condition (here: ``b < 10``)
589 remains true. In Python, like in C, any non-zero integer value is true; zero is
590 false. The condition may also be a string or list value, in fact any sequence;
591 anything with a non-zero length is true, empty sequences are false. The test
592 used in the example is a simple comparison. The standard comparison operators
593 are written the same as in C: ``<`` (less than), ``>`` (greater than), ``==``
594 (equal to), ``<=`` (less than or equal to), ``>=`` (greater than or equal to)
595 and ``!=`` (not equal to).
596
597* The *body* of the loop is *indented*: indentation is Python's way of grouping
Georg Brandl2c9eee12011-12-25 19:03:07 +0100598 statements. At the interactive prompt, you have to type a tab or space(s) for
599 each indented line. In practice you will prepare more complicated input
600 for Python with a text editor; all decent text editors have an auto-indent
601 facility. When a compound statement is entered interactively, it must be
602 followed by a blank line to indicate completion (since the parser cannot
603 guess when you have typed the last line). Note that each line within a basic
604 block must be indented by the same amount.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000605
606* The :keyword:`print` statement writes the value of the expression(s) it is
607 given. It differs from just writing the expression you want to write (as we did
608 earlier in the calculator examples) in the way it handles multiple expressions
609 and strings. Strings are printed without quotes, and a space is inserted
610 between items, so you can format things nicely, like this::
611
612 >>> i = 256*256
613 >>> print 'The value of i is', i
614 The value of i is 65536
615
616 A trailing comma avoids the newline after the output::
617
618 >>> a, b = 0, 1
619 >>> while b < 1000:
620 ... print b,
621 ... a, b = b, a+b
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000622 ...
Georg Brandl8ec7f652007-08-15 14:28:01 +0000623 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987
624
625 Note that the interpreter inserts a newline before it prints the next prompt if
626 the last line was not completed.
Zachary Ware5b1b38cb2014-07-01 14:25:34 -0500627
628.. rubric:: Footnotes
629
630.. [#] Since ``**`` has higher precedence than ``-``, ``-3**2`` will be
631 interpreted as ``-(3**2)`` and thus result in ``-9``. To avoid this
632 and get ``9``, you can use ``(-3)**2``.
633
634.. [#] Unlike other languages, special characters such as ``\n`` have the
635 same meaning with both single (``'...'``) and double (``"..."``) quotes.
636 The only difference between the two is that within single quotes you don't
637 need to escape ``"`` (but you have to escape ``\'``) and vice versa.