Mercurial > cpython-lru_cache-weakref
annotate Doc/tutorial/introduction.rst @ 84007:dbfbdf2b5c19 3.3
Fix #17967: For ftp urls CWD to target instead of hopping to each directory
towards target. This fixes a bug where target is accessible, but parent
directories are restricted.
| author | Senthil Kumaran <senthil@uthcode.com> |
|---|---|
| date | Sat, 01 Jun 2013 08:27:06 -0700 |
| parents | bde91dddbcbc |
| children | 6f16fa5223cc |
| rev | line source |
|---|---|
|
66161
ec42112c2100
Backport rev86422 for issue 10304 from py3k to 31-maint
Terry Reedy <tjreedy@udel.edu>
parents:
66076
diff
changeset
|
1 .. _tut-informal: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
2 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
3 ********************************** |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
4 An Informal Introduction to Python |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
5 ********************************** |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
6 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
7 In the following examples, input and output are distinguished by the presence or |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
8 absence of prompts (:term:`>>>` and :term:`...`): to repeat the example, you must type |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
9 everything after the prompt, when the prompt appears; lines that do not begin |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
10 with a prompt are output from the interpreter. Note that a secondary prompt on a |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
11 line by itself in an example means you must type a blank line; this is used to |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
12 end a multi-line command. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
13 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
14 Many of the examples in this manual, even those entered at the interactive |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
15 prompt, include comments. Comments in Python start with the hash character, |
| 49180 | 16 ``#``, and extend to the end of the physical line. A comment may appear at the |
| 17 start of a line or following whitespace or code, but not within a string | |
|
44396
6556f3f22c19
Merged revisions 59605-59624 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
43815
diff
changeset
|
18 literal. A hash character within a string literal is just a hash character. |
| 49180 | 19 Since comments are to clarify code and are not interpreted by Python, they may |
| 20 be omitted when typing in examples. | |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
21 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
22 Some examples:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
23 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
24 # this is the first comment |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
25 spam = 1 # and this is the second comment |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
26 # ... and now a third! |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
27 text = "# This is not a comment because it's inside quotes." |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
28 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
29 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
30 .. _tut-calculator: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
31 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
32 Using Python as a Calculator |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
33 ============================ |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
34 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
35 Let's try some simple Python commands. Start the interpreter and wait for the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
36 primary prompt, ``>>>``. (It shouldn't take long.) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
37 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
38 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
39 .. _tut-numbers: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
40 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
41 Numbers |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
42 ------- |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
43 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
44 The interpreter acts as a simple calculator: you can type an expression at it |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
45 and it will write the value. Expression syntax is straightforward: the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
46 operators ``+``, ``-``, ``*`` and ``/`` work just like in most other languages |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
47 (for example, Pascal or C); parentheses (``()``) can be used for grouping. |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
48 For example:: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
49 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
50 >>> 2 + 2 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
51 4 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
52 >>> 50 - 5*6 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
53 20 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
54 >>> (50 - 5*6) / 4 |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
55 5.0 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
56 >>> 8 / 5 # division always returns a floating point number |
|
54927
e3025a4c0ad7
Merged revisions 73636 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
54548
diff
changeset
|
57 1.6 |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
58 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
59 The integer numbers (e.g. ``2``, ``4``, ``20``) have type :class:`int`, |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
60 the ones with a fractional part (e.g. ``5.0``, ``1.6``) have type |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
61 :class:`float`. We will see more about numberic types later in the tutorial. |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
62 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
63 Division (``/``) always returns a float. To do :term:`floor division` and |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
64 get an integer result (discarding any fractional result) you can use the ``//`` |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
65 operator; to calculate the remainder you can use ``%``:: |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
66 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
67 >>> 17 / 3 # classic division returns a float |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
68 5.666666666666667 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
69 >>> |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
70 >>> 17 // 3 # floor division discards the fractional part |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
71 5 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
72 >>> 17 % 3 # the % operator returns the remainder of the division |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
73 2 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
74 >>> 5 * 3 + 2 # result * divisor + remainder |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
75 17 |
|
50563
93cf3a6ead63
Merged revisions 68226 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49180
diff
changeset
|
76 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
77 With Python is possible to use the ``**`` operator to calculate powers [#]_:: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
78 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
79 >>> 5 ** 2 # 5 squared |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
80 25 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
81 >>> 2 ** 7 # 2 to the power of 7 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
82 128 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
83 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
84 The equal sign (``=``) is used to assign a value to a variable. Afterwards, no |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
85 result is displayed before the next interactive prompt:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
86 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
87 >>> width = 20 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
88 >>> height = 5 * 9 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
89 >>> width * height |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
90 900 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
91 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
92 If a variable is not "defined" (assigned a value), trying to use it will |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
93 give you an error:: |
| 49180 | 94 |
|
79153
5f4841977bee
Issue #16015: Fix NameError doctest example in tutorial introduction.
Chris Jerdonek <chris.jerdonek@gmail.com>
parents:
78507
diff
changeset
|
95 >>> n # try to access an undefined variable |
|
50563
93cf3a6ead63
Merged revisions 68226 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49180
diff
changeset
|
96 Traceback (most recent call last): |
| 49180 | 97 File "<stdin>", line 1, in <module> |
| 98 NameError: name 'n' is not defined | |
| 99 | |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
100 There is full support for floating point; operators with mixed type operands |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
101 convert the integer operand to floating point:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
102 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
103 >>> 3 * 3.75 / 1.5 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
104 7.5 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
105 >>> 7.0 / 2 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
106 3.5 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
107 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
108 In interactive mode, the last printed expression is assigned to the variable |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
109 ``_``. This means that when you are using Python as a desk calculator, it is |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
110 somewhat easier to continue calculations, for example:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
111 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
112 >>> tax = 12.5 / 100 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
113 >>> price = 100.50 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
114 >>> price * tax |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
115 12.5625 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
116 >>> price + _ |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
117 113.0625 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
118 >>> round(_, 2) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
119 113.06 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
120 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
121 This variable should be treated as read-only by the user. Don't explicitly |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
122 assign a value to it --- you would create an independent local variable with the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
123 same name masking the built-in variable with its magic behavior. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
124 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
125 In addition to :class:`int` and :class:`float`, Python supports other types of |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
126 numbers, such as :class:`~decimal.Decimal` and :class:`~fractions.Fraction`. |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
127 Python also has built-in support for :ref:`complex numbers <typesnumeric>`, |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
128 and uses the ``j`` or ``J`` suffix to indicate the imaginary part |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
129 (e.g. ``3+5j``). |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
130 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
131 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
132 .. _tut-strings: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
133 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
134 Strings |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
135 ------- |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
136 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
137 Besides numbers, Python can also manipulate strings, which can be expressed |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
138 in several ways. They can be enclosed in single quotes (``'...'``) or |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
139 double quotes (``"..."``) with the same result [#]_. ``\`` can be used |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
140 to escape quotes:: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
141 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
142 >>> 'spam eggs' # single quotes |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
143 'spam eggs' |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
144 >>> 'doesn\'t' # use \' to escape the single quote... |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
145 "doesn't" |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
146 >>> "doesn't" # ...or use double quotes instead |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
147 "doesn't" |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
148 >>> '"Yes," he said.' |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
149 '"Yes," he said.' |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
150 >>> "\"Yes,\" he said." |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
151 '"Yes," he said.' |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
152 >>> '"Isn\'t," she said.' |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
153 '"Isn\'t," she said.' |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
154 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
155 In the interactive interpreter, the output string is enclosed in quotes and |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
156 special characters are escaped with backslashes. While this might sometimes |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
157 look different from the input (the enclosing quotes could change), the two |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
158 strings are equivalent. The string is enclosed in double quotes if |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
159 the string contains a single quote and no double quotes, otherwise it is |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
160 enclosed in single quotes. The :func:`print` function produces a more |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
161 readable output, by omitting the enclosing quotes and by printing escaped |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
162 and special characters:: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
163 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
164 >>> '"Isn\'t," she said.' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
165 '"Isn\'t," she said.' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
166 >>> print('"Isn\'t," she said.') |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
167 "Isn't," she said. |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
168 >>> s = 'First line.\nSecond line.' # \n means newline |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
169 >>> s # without print(), \n is included in the output |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
170 'First line.\nSecond line.' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
171 >>> print(s) # with print(), \n produces a new line |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
172 First line. |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
173 Second line. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
174 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
175 If you don't want characters prefaced by ``\`` to be interpreted as |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
176 special characters, you can use *raw strings* by adding an ``r`` before |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
177 the first quote:: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
178 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
179 >>> print('C:\some\name') # here \n means newline! |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
180 C:\some |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
181 ame |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
182 >>> print(r'C:\some\name') # note the r before the quote |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
183 C:\some\name |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
184 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
185 String literals can span multiple lines. One way is using triple-quotes: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
186 ``"""..."""`` or ``'''...'''``. End of lines are automatically |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
187 included in the string, but it's possible to prevent this by adding a ``\`` at |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
188 the end of the line. The following example:: |
|
55913
3fb1c1fbe26c
Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54931
diff
changeset
|
189 |
|
66161
ec42112c2100
Backport rev86422 for issue 10304 from py3k to 31-maint
Terry Reedy <tjreedy@udel.edu>
parents:
66076
diff
changeset
|
190 print("""\ |
|
55913
3fb1c1fbe26c
Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54931
diff
changeset
|
191 Usage: thingy [OPTIONS] |
|
3fb1c1fbe26c
Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54931
diff
changeset
|
192 -h Display this usage message |
|
3fb1c1fbe26c
Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54931
diff
changeset
|
193 -H hostname Hostname to connect to |
|
56218
149bed21735e
Merged revisions 75059 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
55913
diff
changeset
|
194 """) |
|
55913
3fb1c1fbe26c
Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54931
diff
changeset
|
195 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
196 produces the following output (note that the initial newline is not included): |
|
55913
3fb1c1fbe26c
Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54931
diff
changeset
|
197 |
|
3fb1c1fbe26c
Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54931
diff
changeset
|
198 .. code-block:: text |
|
3fb1c1fbe26c
Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54931
diff
changeset
|
199 |
|
3fb1c1fbe26c
Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54931
diff
changeset
|
200 Usage: thingy [OPTIONS] |
|
3fb1c1fbe26c
Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54931
diff
changeset
|
201 -h Display this usage message |
|
3fb1c1fbe26c
Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54931
diff
changeset
|
202 -H hostname Hostname to connect to |
|
3fb1c1fbe26c
Merged revisions 74745 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
54931
diff
changeset
|
203 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
204 Strings can be concatenated (glued together) with the ``+`` operator, and |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
205 repeated with ``*``:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
206 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
207 >>> # 3 times 'un', followed by 'ium' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
208 >>> 3 * 'un' + 'ium' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
209 'unununium' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
210 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
211 Two or more *string literals* (i.e. the ones enclosed between quotes) next |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
212 to each other are automatically concatenated. :: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
213 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
214 >>> 'Py' 'thon' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
215 'Python' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
216 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
217 This only works with two literals though, not with variables or expressions:: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
218 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
219 >>> prefix = 'Py' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
220 >>> prefix 'thon' # can't concatenate a variable and a string literal |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
221 ... |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
222 SyntaxError: invalid syntax |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
223 >>> ('un' * 3) 'ium' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
224 ... |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
225 SyntaxError: invalid syntax |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
226 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
227 If you want to concatenate variables or a variable and a literal, use ``+``:: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
228 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
229 >>> prefix + 'thon' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
230 'Python' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
231 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
232 This feature is particularly useful when you want to break long strings:: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
233 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
234 >>> text = ('Put several strings within parentheses ' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
235 'to have them joined together.') |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
236 >>> text |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
237 'Put several strings within parentheses to have them joined together.' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
238 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
239 Strings can be *indexed* (subscripted), with the first character having index 0. |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
240 There is no separate character type; a character is simply a string of size |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
241 one:: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
242 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
243 >>> word = 'Python' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
244 >>> word[0] # character in position 0 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
245 'P' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
246 >>> word[5] # character in position 5 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
247 'n' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
248 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
249 Indices may also be negative numbers, to start counting from the right:: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
250 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
251 >>> word[-1] # last character |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
252 'n' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
253 >>> word[-2] # second-last character |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
254 'o' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
255 >>> word[-6] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
256 'P' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
257 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
258 Note that since -0 is the same as 0, negative indices start from -1. |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
259 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
260 In addition to indexing, *slicing* is also supported. While indexing is used |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
261 to obtain individual characters, *slicing* allows you to obtain substring:: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
262 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
263 >>> word[0:2] # characters from position 0 (included) to 2 (excluded) |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
264 'Py' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
265 >>> word[2:5] # characters from position 2 (included) to 4 (excluded) |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
266 'tho' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
267 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
268 Note how the start is always included, and the end always excluded. This |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
269 makes sure that ``s[:i] + s[i:]`` is always equal to ``s``:: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
270 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
271 >>> word[:2] + word[2:] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
272 'Python' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
273 >>> word[:4] + word[4:] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
274 'Python' |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
275 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
276 Slice indices have useful defaults; an omitted first index defaults to zero, an |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
277 omitted second index defaults to the size of the string being sliced. :: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
278 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
279 >>> word[:2] # character from the beginning to position 2 (excluded) |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
280 'Py' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
281 >>> word[4:] # characters from position 4 (included) to the end |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
282 'on' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
283 >>> word[-2:] # characters from the second-last (included) to the end |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
284 'on' |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
285 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
286 One way to remember how slices work is to think of the indices as pointing |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
287 *between* characters, with the left edge of the first character numbered 0. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
288 Then the right edge of the last character of a string of *n* characters has |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
289 index *n*, for example:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
290 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
291 +---+---+---+---+---+---+ |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
292 | P | y | t | h | o | n | |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
293 +---+---+---+---+---+---+ |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
294 0 1 2 3 4 5 6 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
295 -6 -5 -4 -3 -2 -1 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
296 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
297 The first row of numbers gives the position of the indices 0...6 in the string; |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
298 the second row gives the corresponding negative indices. The slice from *i* to |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
299 *j* consists of all characters between the edges labeled *i* and *j*, |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
300 respectively. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
301 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
302 For non-negative indices, the length of a slice is the difference of the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
303 indices, if both are within bounds. For example, the length of ``word[1:3]`` is |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
304 2. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
305 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
306 Attempting to use a index that is too large will result in an error:: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
307 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
308 >>> word[42] # the word only has 7 characters |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
309 Traceback (most recent call last): |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
310 File "<stdin>", line 1, in <module> |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
311 IndexError: string index out of range |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
312 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
313 However, out of range slice indexes are handled gracefully when used for |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
314 slicing:: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
315 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
316 >>> word[4:42] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
317 'on' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
318 >>> word[42:] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
319 '' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
320 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
321 Python strings cannot be changed --- they are :term:`immutable`. |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
322 Therefore, assigning to an indexed position in the string results in an error:: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
323 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
324 >>> word[0] = 'J' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
325 ... |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
326 TypeError: 'str' object does not support item assignment |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
327 >>> word[2:] = 'py' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
328 ... |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
329 TypeError: 'str' object does not support item assignment |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
330 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
331 If you need a different string, you should create a new one:: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
332 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
333 >>> 'J' + word[1:] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
334 'Jython' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
335 >>> word[:2] + 'py' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
336 'Pypy' |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
337 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
338 The built-in function :func:`len` returns the length of a string:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
339 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
340 >>> s = 'supercalifragilisticexpialidocious' |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
341 >>> len(s) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
342 34 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
343 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
344 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
345 .. seealso:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
346 |
|
79681
06deefec71e3
Fix and add more links.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
79153
diff
changeset
|
347 :ref:`textseq` |
|
50563
93cf3a6ead63
Merged revisions 68226 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49180
diff
changeset
|
348 Strings are examples of *sequence types*, and support the common |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
349 operations supported by such types. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
350 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
351 :ref:`string-methods` |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
352 Strings support a large number of methods for |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
353 basic transformations and searching. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
354 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
355 :ref:`string-formatting` |
|
47283
f28cbab11acd
Merged revisions 63655 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44570
diff
changeset
|
356 Information about string formatting with :meth:`str.format` is described |
|
f28cbab11acd
Merged revisions 63655 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44570
diff
changeset
|
357 here. |
|
f28cbab11acd
Merged revisions 63655 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44570
diff
changeset
|
358 |
|
f28cbab11acd
Merged revisions 63655 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44570
diff
changeset
|
359 :ref:`old-string-formatting` |
|
f28cbab11acd
Merged revisions 63655 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44570
diff
changeset
|
360 The old formatting operations invoked when strings and Unicode strings are |
|
f28cbab11acd
Merged revisions 63655 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
44570
diff
changeset
|
361 the left operand of the ``%`` operator are described in more detail here. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
362 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
363 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
364 .. _tut-lists: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
365 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
366 Lists |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
367 ----- |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
368 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
369 Python knows a number of *compound* data types, used to group together other |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
370 values. The most versatile is the *list*, which can be written as a list of |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
371 comma-separated values (items) between square brackets. Lists might contain |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
372 items of different types, but usually the items all have the same type. :: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
373 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
374 >>> squares = [1, 2, 4, 9, 16, 25] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
375 >>> squares |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
376 [1, 2, 4, 9, 16, 25] |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
377 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
378 Like strings (and all other built-in :term:`sequence` type), lists can be |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
379 indexed and sliced:: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
380 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
381 >>> squares[0] # indexing returns the item |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
382 1 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
383 >>> squares[-1] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
384 25 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
385 >>> squares[-3:] # slicing returns a new list |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
386 [9, 16, 25] |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
387 |
|
59876
90217a86642d
Merged revisions 78966,78970,79018,79026-79027,79055,79156,79159,79163-79164,79173,79176,79194,79208,79212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56218
diff
changeset
|
388 All slice operations return a new list containing the requested elements. This |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
389 means that the following slice returns a new (shallow) copy of the list:: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
390 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
391 >>> squares[:] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
392 [1, 2, 4, 9, 16, 25] |
|
59876
90217a86642d
Merged revisions 78966,78970,79018,79026-79027,79055,79156,79159,79163-79164,79173,79176,79194,79208,79212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56218
diff
changeset
|
393 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
394 Lists also supports operations like concatenation:: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
395 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
396 >>> squares + [36, 49, 64, 81, 100] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
397 [1, 2, 4, 9, 16, 25, 36, 49, 64, 81, 100] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
398 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
399 Unlike strings, which are :term:`immutable`, lists are a :term:`mutable` |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
400 type, i.e. it is possible to change their content:: |
|
59876
90217a86642d
Merged revisions 78966,78970,79018,79026-79027,79055,79156,79159,79163-79164,79173,79176,79194,79208,79212 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
56218
diff
changeset
|
401 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
402 >>> cubes = [1, 8, 27, 65, 125] # something's wrong here |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
403 >>> 4 ** 3 # the cube of 4 is 64, not 65! |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
404 64 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
405 >>> cubes[3] = 64 # replace the wrong value |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
406 >>> cubes |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
407 [1, 8, 27, 64, 125] |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
408 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
409 You can also add new items at the end of the list, by using |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
410 the :meth:`~list.append` *method* (we will see more about methods later):: |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
411 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
412 >>> cubes.append(216) # add the cube of 6 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
413 >>> cubes.append(7 ** 3) # and the cube of 7 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
414 >>> cubes |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
415 [1, 8, 27, 64, 125, 216, 343] |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
416 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
417 Assignment to slices is also possible, and this can even change the size of the |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
418 list or clear it entirely:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
419 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
420 >>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
421 >>> letters |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
422 ['a', 'b', 'c', 'd', 'e', 'f', 'g'] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
423 >>> # replace some values |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
424 >>> letters[2:5] = ['C', 'D', 'E'] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
425 >>> letters |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
426 ['a', 'b', 'C', 'D', 'E', 'f', 'g'] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
427 >>> # now remove them |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
428 >>> letters[2:5] = [] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
429 >>> letters |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
430 ['a', 'b', 'f', 'g'] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
431 >>> # clear the list by replacing all the elements with an empty list |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
432 >>> letters[:] = [] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
433 >>> letters |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
434 [] |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
435 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
436 The built-in function :func:`len` also applies to lists:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
437 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
438 >>> letters = ['a', 'b', 'c', 'd'] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
439 >>> len(letters) |
|
43815
85cf334b32c3
Merged revisions 58886-58929 via svnmerge from
Guido van Rossum <guido@python.org>
parents:
43449
diff
changeset
|
440 4 |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
441 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
442 It is possible to nest lists (create lists containing other lists), for |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
443 example:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
444 |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
445 >>> a = ['a', 'b', 'c'] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
446 >>> n = [1, 2, 3] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
447 >>> x = [a, n] |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
448 >>> x |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
449 [['a', 'b', 'c'], [1, 2, 3]] |
|
83947
bde91dddbcbc
Issue #18079: Fix a typo in the tutorial.
Serhiy Storchaka <storchaka@gmail.com>
parents:
83856
diff
changeset
|
450 >>> x[0] |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
451 ['a', 'b', 'c'] |
|
83947
bde91dddbcbc
Issue #18079: Fix a typo in the tutorial.
Serhiy Storchaka <storchaka@gmail.com>
parents:
83856
diff
changeset
|
452 >>> x[0][1] |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
453 'b' |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
454 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
455 .. _tut-firststeps: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
456 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
457 First Steps Towards Programming |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
458 =============================== |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
459 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
460 Of course, we can use Python for more complicated tasks than adding two and two |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
461 together. For instance, we can write an initial sub-sequence of the *Fibonacci* |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
462 series as follows:: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
463 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
464 >>> # Fibonacci series: |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
465 ... # the sum of two elements defines the next |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
466 ... a, b = 0, 1 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
467 >>> while b < 10: |
| 44570 | 468 ... print(b) |
| 469 ... a, b = b, a+b | |
|
50563
93cf3a6ead63
Merged revisions 68226 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49180
diff
changeset
|
470 ... |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
471 1 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
472 1 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
473 2 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
474 3 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
475 5 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
476 8 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
477 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
478 This example introduces several new features. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
479 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
480 * The first line contains a *multiple assignment*: the variables ``a`` and ``b`` |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
481 simultaneously get the new values 0 and 1. On the last line this is used again, |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
482 demonstrating that the expressions on the right-hand side are all evaluated |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
483 first before any of the assignments take place. The right-hand side expressions |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
484 are evaluated from the left to the right. |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
485 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
486 * The :keyword:`while` loop executes as long as the condition (here: ``b < 10``) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
487 remains true. In Python, like in C, any non-zero integer value is true; zero is |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
488 false. The condition may also be a string or list value, in fact any sequence; |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
489 anything with a non-zero length is true, empty sequences are false. The test |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
490 used in the example is a simple comparison. The standard comparison operators |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
491 are written the same as in C: ``<`` (less than), ``>`` (greater than), ``==`` |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
492 (equal to), ``<=`` (less than or equal to), ``>=`` (greater than or equal to) |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
493 and ``!=`` (not equal to). |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
494 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
495 * The *body* of the loop is *indented*: indentation is Python's way of grouping |
|
74178
f0197f1d158e
I do not think we will ever have auto-indent at the prompt, that is for IPython and the like.
Georg Brandl <georg@python.org>
parents:
68257
diff
changeset
|
496 statements. At the interactive prompt, you have to type a tab or space(s) for |
|
f0197f1d158e
I do not think we will ever have auto-indent at the prompt, that is for IPython and the like.
Georg Brandl <georg@python.org>
parents:
68257
diff
changeset
|
497 each indented line. In practice you will prepare more complicated input |
|
f0197f1d158e
I do not think we will ever have auto-indent at the prompt, that is for IPython and the like.
Georg Brandl <georg@python.org>
parents:
68257
diff
changeset
|
498 for Python with a text editor; all decent text editors have an auto-indent |
|
f0197f1d158e
I do not think we will ever have auto-indent at the prompt, that is for IPython and the like.
Georg Brandl <georg@python.org>
parents:
68257
diff
changeset
|
499 facility. When a compound statement is entered interactively, it must be |
|
f0197f1d158e
I do not think we will ever have auto-indent at the prompt, that is for IPython and the like.
Georg Brandl <georg@python.org>
parents:
68257
diff
changeset
|
500 followed by a blank line to indicate completion (since the parser cannot |
|
f0197f1d158e
I do not think we will ever have auto-indent at the prompt, that is for IPython and the like.
Georg Brandl <georg@python.org>
parents:
68257
diff
changeset
|
501 guess when you have typed the last line). Note that each line within a basic |
|
f0197f1d158e
I do not think we will ever have auto-indent at the prompt, that is for IPython and the like.
Georg Brandl <georg@python.org>
parents:
68257
diff
changeset
|
502 block must be indented by the same amount. |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
503 |
|
81615
add66b51b890
expressions -> arguments
Benjamin Peterson <benjamin@python.org>
parents:
79681
diff
changeset
|
504 * The :func:`print` function writes the value of the argument(s) it is given. |
|
add66b51b890
expressions -> arguments
Benjamin Peterson <benjamin@python.org>
parents:
79681
diff
changeset
|
505 It differs from just writing the expression you want to write (as we did |
|
add66b51b890
expressions -> arguments
Benjamin Peterson <benjamin@python.org>
parents:
79681
diff
changeset
|
506 earlier in the calculator examples) in the way it handles multiple arguments, |
|
add66b51b890
expressions -> arguments
Benjamin Peterson <benjamin@python.org>
parents:
79681
diff
changeset
|
507 floating point quantities, and strings. Strings are printed without quotes, |
|
add66b51b890
expressions -> arguments
Benjamin Peterson <benjamin@python.org>
parents:
79681
diff
changeset
|
508 and a space is inserted between items, so you can format things nicely, like |
|
add66b51b890
expressions -> arguments
Benjamin Peterson <benjamin@python.org>
parents:
79681
diff
changeset
|
509 this:: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
510 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
511 >>> i = 256*256 |
|
43115
95d6fe2e8e0e
Tutorial update for 3.0 by Paul Dubois.
Guido van Rossum <guido@python.org>
parents:
42604
diff
changeset
|
512 >>> print('The value of i is', i) |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
513 The value of i is 65536 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
514 |
|
81617
66a22b6a3414
end is a keyword argument
Benjamin Peterson <benjamin@python.org>
parents:
81615
diff
changeset
|
515 The keyword argument *end* can be used to avoid the newline after the output, |
|
66a22b6a3414
end is a keyword argument
Benjamin Peterson <benjamin@python.org>
parents:
81615
diff
changeset
|
516 or end the output with a different string:: |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
517 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
518 >>> a, b = 0, 1 |
|
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
519 >>> while b < 1000: |
|
66161
ec42112c2100
Backport rev86422 for issue 10304 from py3k to 31-maint
Terry Reedy <tjreedy@udel.edu>
parents:
66076
diff
changeset
|
520 ... print(b, end=',') |
|
42604
9e1529bf0442
Move the 3k reST doc tree in place.
Georg Brandl <georg@python.org>
parents:
diff
changeset
|
521 ... a, b = b, a+b |
|
50563
93cf3a6ead63
Merged revisions 68226 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49180
diff
changeset
|
522 ... |
|
66161
ec42112c2100
Backport rev86422 for issue 10304 from py3k to 31-maint
Terry Reedy <tjreedy@udel.edu>
parents:
66076
diff
changeset
|
523 1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987, |
|
83856
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
524 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
525 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
526 .. rubric:: Footnotes |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
527 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
528 .. [#] Since ``**`` has higher precedence than ``-``, ``-3**2`` will be |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
529 interpreted as ``-(3**2)`` and thus result in ``-9``. To avoid this |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
530 and get ``9``, you can use ``(-3)**2``. |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
531 |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
532 .. [#] Unlike other languages, special characters such as ``\n`` have the |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
533 same meaning with both single (``'...'``) and double (``"..."``) quotes. |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
534 The only difference between the two is that within single quotes you don't |
|
796d1371605d
#14097: improve the "introduction" page of the tutorial.
Ezio Melotti <ezio.melotti@gmail.com>
parents:
81617
diff
changeset
|
535 need to escape ``"`` (but you have to escape ``\'``) and vice versa. |
