@@ -17,6 +17,7 @@ Perhaps the most well-known statement type is the :keyword:`if` statement. For
1717example::
1818
1919 >>> x = int(raw_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
3133There can be zero or more :keyword: `elif ` parts, and the :keyword: `else ` part is
3234optional. The keyword ':keyword: `elif `' is short for 'else if', and is useful
@@ -161,7 +163,7 @@ The :keyword:`pass` statement does nothing. It can be used when a statement is
161163required syntactically but the program requires no action. For example::
162164
163165 >>> while True:
164- ... pass # Busy-wait for keyboard interrupt
166+ ... pass # Busy-wait for keyboard interrupt (Ctrl+C)
165167 ...
166168
167169
@@ -192,14 +194,14 @@ boundary::
192194The keyword :keyword: `def ` introduces a function *definition *. It must be
193195followed by the function name and the parenthesized list of formal parameters.
194196The statements that form the body of the function start at the next line, and
195- must be indented. The first statement of the function body can optionally be a
196- string literal; this string literal is the function's documentation string, or
197- :dfn: `docstring `.
197+ must be indented.
198198
199+ The first statement of the function body can optionally be a string literal;
200+ this string literal is the function's documentation string, or :dfn: `docstring `.
201+ (More about docstrings can be found in the section :ref: `tut-docstrings `.)
199202There are tools which use docstrings to automatically produce online or printed
200203documentation, or to let the user interactively browse through code; it's good
201- practice to include docstrings in code that you write, so try to make a habit of
202- it.
204+ practice to include docstrings in code that you write, so make a habit of it.
203205
204206The *execution * of a function introduces a new symbol table used for the local
205207variables of the function. More precisely, all variable assignments in a
@@ -228,12 +230,12 @@ mechanism::
228230 >>> f(100)
229231 1 1 2 3 5 8 13 21 34 55 89
230232
231- You might object that ``fib `` is not a function but a procedure. In Python,
232- like in C, procedures are just functions that don 't return a value. In fact,
233- technically speaking, procedures do return a value, albeit a rather boring one.
234- This value is called ``None `` (it's a built-in name). Writing the value
235- `` None `` is normally suppressed by the interpreter if it would be the only value
236- written. You can see it if you really want to using :keyword: `print `::
233+ Coming from other languages, you might object that ``fib `` is not a function but
234+ a procedure since it doesn 't return a value. In fact, even functions without a
235+ :keyword: ` return ` statement do return a value, albeit a rather boring one. This
236+ value is called ``None `` (it's a built-in name). Writing the value `` None `` is
237+ normally suppressed by the interpreter if it would be the only value written.
238+ You can see it if you really want to using :keyword: `print `::
237239
238240 >>> fib(0)
239241 >>> print fib(0)
@@ -259,7 +261,7 @@ This example, as usual, demonstrates some new Python features:
259261
260262* The :keyword: `return ` statement returns with a value from a function.
261263 :keyword: `return ` without an expression argument returns ``None ``. Falling off
262- the end of a procedure also returns ``None ``.
264+ the end of a function also returns ``None ``.
263265
264266* The statement ``result.append(b) `` calls a *method * of the list object
265267 ``result ``. A method is a function that 'belongs' to an object and is named
@@ -400,21 +402,21 @@ list. (``*name`` must occur before ``**name``.) For example, if we define a
400402function like this::
401403
402404 def cheeseshop(kind, *arguments, **keywords):
403- print "-- Do you have any", kind, '?'
405+ print "-- Do you have any", kind, "?"
404406 print "-- I'm sorry, we're all out of", kind
405407 for arg in arguments: print arg
406- print '-'* 40
408+ print "-" * 40
407409 keys = keywords.keys()
408410 keys.sort()
409- for kw in keys: print kw, ':' , keywords[kw]
411+ for kw in keys: print kw, ":" , keywords[kw]
410412
411413It could be called like this::
412414
413- cheeseshop(' Limburger' , "It's very runny, sir.",
415+ cheeseshop(" Limburger" , "It's very runny, sir.",
414416 "It's really very, VERY runny, sir.",
415- client='John Cleese',
416417 shopkeeper='Michael Palin',
417- sketch='Cheese Shop Sketch')
418+ client="John Cleese",
419+ sketch="Cheese Shop Sketch")
418420
419421and of course it would print::
420422
@@ -442,8 +444,8 @@ Arbitrary Argument Lists
442444
443445Finally, the least frequently used option is to specify that a function can be
444446called with an arbitrary number of arguments. These arguments will be wrapped
445- up in a tuple. Before the variable number of arguments, zero or more normal
446- arguments may occur. ::
447+ up in a tuple (see :ref: ` tut-tuples `) . Before the variable number of arguments,
448+ zero or more normal arguments may occur. ::
447449
448450 def write_multiple_items(file, separator, *args):
449451 file.write(separator.join(args))
@@ -600,7 +602,8 @@ extracted for you:
600602
601603* Name your classes and functions consistently; the convention is to use
602604 ``CamelCase `` for classes and ``lower_case_with_underscores `` for functions
603- and methods. Always use ``self `` as the name for the first method argument.
605+ and methods. Always use ``self `` as the name for the first method argument
606+ (see :ref: `tut-firstclasses ` for more on classes and methods).
604607
605608* Don't use fancy encodings if your code is meant to be used in international
606609 environments. Plain ASCII works best in any case.
0 commit comments