Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 5d955ed

Browse files
committed
Forward-port of r66447.
1 parent d7b0328 commit 5d955ed

4 files changed

Lines changed: 45 additions & 30 deletions

File tree

Doc/tutorial/classes.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ the class's namespace when the class object was created. So, if the class
261261
definition looked like this::
262262

263263
class MyClass:
264-
"A simple example class"
264+
"""A simple example class"""
265265
i = 12345
266266
def f(self):
267267
return 'hello world'

Doc/tutorial/controlflow.rst

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Perhaps the most well-known statement type is the :keyword:`if` statement. For
1717
example::
1818

1919
>>> x = int(input("Please enter an integer: "))
20+
Please enter an integer: 42
2021
>>> if x < 0:
2122
... x = 0
2223
... print('Negative changed to zero')
@@ -26,7 +27,8 @@ example::
2627
... print('Single')
2728
... else:
2829
... print('More')
29-
...
30+
...
31+
More
3032

3133
There can be zero or more :keyword:`elif` parts, and the :keyword:`else` part is
3234
optional. The keyword ':keyword:`elif`' is short for 'else if', and is useful
@@ -191,7 +193,7 @@ The :keyword:`pass` statement does nothing. It can be used when a statement is
191193
required syntactically but the program requires no action. For example::
192194

193195
>>> while True:
194-
... pass # Busy-wait for keyboard interrupt
196+
... pass # Busy-wait for keyboard interrupt (Ctrl+C)
195197
...
196198

197199

@@ -223,14 +225,14 @@ boundary::
223225
The keyword :keyword:`def` introduces a function *definition*. It must be
224226
followed by the function name and the parenthesized list of formal parameters.
225227
The statements that form the body of the function start at the next line, and
226-
must be indented. The first statement of the function body can optionally be a
227-
string literal; this string literal is the function's documentation string, or
228-
:dfn:`docstring`.
228+
must be indented.
229229

230+
The first statement of the function body can optionally be a string literal;
231+
this string literal is the function's documentation string, or :dfn:`docstring`.
232+
(More about docstrings can be found in the section :ref:`tut-docstrings`.)
230233
There are tools which use docstrings to automatically produce online or printed
231234
documentation, or to let the user interactively browse through code; it's good
232-
practice to include docstrings in code that you write, so try to make a habit of
233-
it.
235+
practice to include docstrings in code that you write, so make a habit of it.
234236

235237
The *execution* of a function introduces a new symbol table used for the local
236238
variables of the function. More precisely, all variable assignments in a
@@ -259,12 +261,12 @@ mechanism::
259261
>>> f(100)
260262
1 1 2 3 5 8 13 21 34 55 89
261263

262-
You might object that ``fib`` is not a function but a procedure. In Python,
263-
like in C, procedures are just functions that don't return a value. In fact,
264-
technically speaking, procedures do return a value, albeit a rather boring one.
265-
This value is called ``None`` (it's a built-in name). Writing the value
266-
``None`` is normally suppressed by the interpreter if it would be the only value
267-
written. You can see it if you really want to using :func:`print`::
264+
Coming from other languages, you might object that ``fib`` is not a function but
265+
a procedure since it doesn't return a value. In fact, even functions without a
266+
:keyword:`return` statement do return a value, albeit a rather boring one. This
267+
value is called ``None`` (it's a built-in name). Writing the value ``None`` is
268+
normally suppressed by the interpreter if it would be the only value written.
269+
You can see it if you really want to using :func:`print`::
268270

269271
>>> fib(0)
270272
>>> print(fib(0))
@@ -290,7 +292,7 @@ This example, as usual, demonstrates some new Python features:
290292

291293
* The :keyword:`return` statement returns with a value from a function.
292294
:keyword:`return` without an expression argument returns ``None``. Falling off
293-
the end of a procedure also returns ``None``.
295+
the end of a function also returns ``None``.
294296

295297
* The statement ``result.append(b)`` calls a *method* of the list object
296298
``result``. A method is a function that 'belongs' to an object and is named
@@ -432,20 +434,20 @@ list. (``*name`` must occur before ``**name``.) For example, if we define a
432434
function like this::
433435

434436
def cheeseshop(kind, *arguments, **keywords):
435-
print("-- Do you have any", kind, '?')
437+
print("-- Do you have any", kind, "?")
436438
print("-- I'm sorry, we're all out of", kind)
437439
for arg in arguments: print(arg)
438-
print('-'*40)
440+
print("-" * 40)
439441
keys = sorted(keywords.keys())
440-
for kw in keys: print(kw, ':', keywords[kw])
442+
for kw in keys: print(kw, ":", keywords[kw])
441443

442444
It could be called like this::
443445

444-
cheeseshop('Limburger', "It's very runny, sir.",
446+
cheeseshop("Limburger", "It's very runny, sir.",
445447
"It's really very, VERY runny, sir.",
446-
client='John Cleese',
447-
shopkeeper='Michael Palin',
448-
sketch='Cheese Shop Sketch')
448+
shopkeeper="Michael Palin",
449+
client="John Cleese",
450+
sketch="Cheese Shop Sketch")
449451

450452
and of course it would print::
451453

@@ -472,8 +474,8 @@ Arbitrary Argument Lists
472474

473475
Finally, the least frequently used option is to specify that a function can be
474476
called with an arbitrary number of arguments. These arguments will be wrapped
475-
up in a tuple. Before the variable number of arguments, zero or more normal
476-
arguments may occur. ::
477+
up in a tuple (see :ref:`tut-tuples`). Before the variable number of arguments,
478+
zero or more normal arguments may occur. ::
477479

478480
def write_multiple_items(file, separator, *args):
479481
file.write(separator.join(args))
@@ -644,7 +646,8 @@ extracted for you:
644646

645647
* Name your classes and functions consistently; the convention is to use
646648
``CamelCase`` for classes and ``lower_case_with_underscores`` for functions
647-
and methods. Always use ``self`` as the name for the first method argument.
649+
and methods. Always use ``self`` as the name for the first method argument
650+
(see :ref:`tut-firstclasses` for more on classes and methods).
648651

649652
* Don't use fancy encodings if your code is meant to be used in international
650653
environments. Plain ASCII works best in any case.

Doc/tutorial/datastructures.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ Referencing the name ``a`` hereafter is an error (at least until another value
292292
is assigned to it). We'll find other uses for :keyword:`del` later.
293293

294294

295+
.. _tut-tuples:
295296

296297
Tuples and Sequences
297298
====================

Doc/tutorial/introduction.rst

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@ end a multi-line command.
1313

1414
Many of the examples in this manual, even those entered at the interactive
1515
prompt, include comments. Comments in Python start with the hash character,
16-
``#``, and extend to the end of the physical line. A comment may appear at
17-
the start of a line or following whitespace or code, but not within a string
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
1818
literal. A hash character within a string literal is just a hash character.
19+
Since comments are to clarify code and are not interpreted by Python, they may
20+
be omitted when typing in examples.
1921

2022
Some examples::
2123

@@ -96,6 +98,15 @@ A value can be assigned to several variables simultaneously::
9698
>>> z
9799
0
98100

101+
Variables must be "defined" (assigned a value) before they can be used, or an
102+
error will occur::
103+
104+
>>> # try to access an undefined variable
105+
... n
106+
Traceback (most recent call last):
107+
File "<stdin>", line 1, in <module>
108+
NameError: name 'n' is not defined
109+
99110
There is full support for floating point; operators with mixed type operands
100111
convert the integer operand to floating point::
101112

@@ -290,7 +301,7 @@ omitted second index defaults to the size of the string being sliced. ::
290301
>>> word[2:] # Everything except the first two characters
291302
'lpA'
292303

293-
Unlike a C string, Python strings cannot be changed. Assigning to an indexed
304+
Unlike a C string, Python strings cannot be changed. Assigning to an indexed
294305
position in the string results in an error::
295306

296307
>>> word[0] = 'x'
@@ -409,8 +420,8 @@ About Unicode
409420
.. sectionauthor:: Marc-Andre Lemburg <[email protected]>
410421

411422

412-
Starting with Python 3.0 all strings support Unicode.
413-
(See http://www.unicode.org/)
423+
Starting with Python 3.0 all strings support Unicode (see
424+
http://www.unicode.org/).
414425

415426
Unicode has the advantage of providing one ordinal for every character in every
416427
script used in modern and ancient texts. Previously, there were only 256

0 commit comments

Comments
 (0)