Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
5d955ed1
Commit
5d955ed1
authored
Sep 13, 2008
by
Georg Brandl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Forward-port of r66447.
parent
d7b03284
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
45 additions
and
30 deletions
+45
-30
Doc/tutorial/classes.rst
Doc/tutorial/classes.rst
+1
-1
Doc/tutorial/controlflow.rst
Doc/tutorial/controlflow.rst
+27
-24
Doc/tutorial/datastructures.rst
Doc/tutorial/datastructures.rst
+1
-0
Doc/tutorial/introduction.rst
Doc/tutorial/introduction.rst
+16
-5
No files found.
Doc/tutorial/classes.rst
View file @
5d955ed1
...
@@ -261,7 +261,7 @@ the class's namespace when the class object was created. So, if the class
...
@@ -261,7 +261,7 @@ the class's namespace when the class object was created. So, if the class
definition looked like this::
definition looked like this::
class MyClass:
class MyClass:
"
A simple example class
"
"
""A simple example class""
"
i = 12345
i = 12345
def f(self):
def f(self):
return 'hello world'
return 'hello world'
...
...
Doc/tutorial/controlflow.rst
View file @
5d955ed1
...
@@ -17,6 +17,7 @@ Perhaps the most well-known statement type is the :keyword:`if` statement. For
...
@@ -17,6 +17,7 @@ Perhaps the most well-known statement type is the :keyword:`if` statement. For
example::
example::
>>> x = int(input("Please enter an integer: "))
>>> x = int(input("Please enter an integer: "))
Please enter an integer: 42
>>> if x < 0:
>>> if x < 0:
... x = 0
... x = 0
... print('Negative changed to zero')
... print('Negative changed to zero')
...
@@ -26,7 +27,8 @@ example::
...
@@ -26,7 +27,8 @@ example::
... print('Single')
... print('Single')
... else:
... else:
... print('More')
... print('More')
...
...
More
There can be zero or more :keyword:`elif` parts, and the :keyword:`else` part is
There can be zero or more :keyword:`elif` parts, and the :keyword:`else` part is
optional. The keyword ':keyword:`elif`' is short for 'else if', and is useful
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
...
@@ -191,7 +193,7 @@ The :keyword:`pass` statement does nothing. It can be used when a statement is
required syntactically but the program requires no action. For example::
required syntactically but the program requires no action. For example::
>>> while True:
>>> while True:
...
pass # Busy-wait for keyboard interrupt
...
pass # Busy-wait for keyboard interrupt (Ctrl+C)
...
...
...
@@ -223,14 +225,14 @@ boundary::
...
@@ -223,14 +225,14 @@ boundary::
The keyword :keyword:`def` introduces a function *definition*. It must be
The keyword :keyword:`def` introduces a function *definition*. It must be
followed by the function name and the parenthesized list of formal parameters.
followed by the function name and the parenthesized list of formal parameters.
The statements that form the body of the function start at the next line, and
The statements that form the body of the function start at the next line, and
must be indented. The first statement of the function body can optionally be a
must be indented.
string literal; this string literal is the function's documentation string, or
:dfn:`docstring`.
The first statement of the function body can optionally be a string literal;
this string literal is the function's documentation string, or :dfn:`docstring`.
(More about docstrings can be found in the section :ref:`tut-docstrings`.)
There are tools which use docstrings to automatically produce online or printed
There are tools which use docstrings to automatically produce online or printed
documentation, or to let the user interactively browse through code; it's good
documentation, or to let the user interactively browse through code; it's good
practice to include docstrings in code that you write, so try to make a habit of
practice to include docstrings in code that you write, so make a habit of it.
it.
The *execution* of a function introduces a new symbol table used for the local
The *execution* of a function introduces a new symbol table used for the local
variables of the function. More precisely, all variable assignments in a
variables of the function. More precisely, all variable assignments in a
...
@@ -259,12 +261,12 @@ mechanism::
...
@@ -259,12 +261,12 @@ mechanism::
>>> f(100)
>>> f(100)
1 1 2 3 5 8 13 21 34 55 89
1 1 2 3 5 8 13 21 34 55 89
You might object that ``fib`` is not a function but a procedure. In Python,
Coming from other languages, you might object that ``fib`` is not a function but
like in C, procedures are just functions that don't return a value. In fact,
a procedure since it doesn't return a value. In fact, even functions without a
technically speaking, procedures do return a value, albeit a rather boring one.
:keyword:`return` statement do return a value, albeit a rather boring one. This
This value is called ``None`` (it's a built-in name). Writing the value
value is called ``None`` (it's a built-in name). Writing the value ``None`` is
``None`` is normally suppressed by the interpreter if it would be the only value
normally suppressed by the interpreter if it would be the only value written.
written.
You can see it if you really want to using :func:`print`::
You can see it if you really want to using :func:`print`::
>>> fib(0)
>>> fib(0)
>>> print(fib(0))
>>> print(fib(0))
...
@@ -290,7 +292,7 @@ This example, as usual, demonstrates some new Python features:
...
@@ -290,7 +292,7 @@ This example, as usual, demonstrates some new Python features:
* The :keyword:`return` statement returns with a value from a function.
* The :keyword:`return` statement returns with a value from a function.
:keyword:`return` without an expression argument returns ``None``. Falling off
:keyword:`return` without an expression argument returns ``None``. Falling off
the end of a
procedure
also returns ``None``.
the end of a
function
also returns ``None``.
* The statement ``result.append(b)`` calls a *method* of the list object
* The statement ``result.append(b)`` calls a *method* of the list object
``result``. A method is a function that 'belongs' to an object and is named
``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
...
@@ -432,20 +434,20 @@ list. (``*name`` must occur before ``**name``.) For example, if we define a
function like this::
function like this::
def cheeseshop(kind, *arguments, **keywords):
def cheeseshop(kind, *arguments, **keywords):
print("-- Do you have any", kind,
'?'
)
print("-- Do you have any", kind,
"?"
)
print("-- I'm sorry, we're all out of", kind)
print("-- I'm sorry, we're all out of", kind)
for arg in arguments: print(arg)
for arg in arguments: print(arg)
print(
'-'*
40)
print(
"-" *
40)
keys = sorted(keywords.keys())
keys = sorted(keywords.keys())
for kw in keys: print(kw,
':'
, keywords[kw])
for kw in keys: print(kw,
":"
, keywords[kw])
It could be called like this::
It could be called like this::
cheeseshop(
'Limburger'
, "It's very runny, sir.",
cheeseshop(
"Limburger"
, "It's very runny, sir.",
"It's really very, VERY runny, sir.",
"It's really very, VERY runny, sir.",
client='John Cleese'
,
shopkeeper="Michael Palin"
,
shopkeeper='Michael Palin'
,
client="John Cleese"
,
sketch=
'Cheese Shop Sketch'
)
sketch=
"Cheese Shop Sketch"
)
and of course it would print::
and of course it would print::
...
@@ -472,8 +474,8 @@ Arbitrary Argument Lists
...
@@ -472,8 +474,8 @@ Arbitrary Argument Lists
Finally, the least frequently used option is to specify that a function can be
Finally, the least frequently used option is to specify that a function can be
called with an arbitrary number of arguments. These arguments will be wrapped
called with an arbitrary number of arguments. These arguments will be wrapped
up in a tuple
. Before the variable number of arguments, zero or more normal
up in a tuple
(see :ref:`tut-tuples`). Before the variable number of arguments,
arguments may occur. ::
zero or more normal
arguments may occur. ::
def write_multiple_items(file, separator, *args):
def write_multiple_items(file, separator, *args):
file.write(separator.join(args))
file.write(separator.join(args))
...
@@ -644,7 +646,8 @@ extracted for you:
...
@@ -644,7 +646,8 @@ extracted for you:
* Name your classes and functions consistently; the convention is to use
* Name your classes and functions consistently; the convention is to use
``CamelCase`` for classes and ``lower_case_with_underscores`` for functions
``CamelCase`` for classes and ``lower_case_with_underscores`` for functions
and methods. Always use ``self`` as the name for the first method argument.
and methods. Always use ``self`` as the name for the first method argument
(see :ref:`tut-firstclasses` for more on classes and methods).
* Don't use fancy encodings if your code is meant to be used in international
* Don't use fancy encodings if your code is meant to be used in international
environments. Plain ASCII works best in any case.
environments. Plain ASCII works best in any case.
...
...
Doc/tutorial/datastructures.rst
View file @
5d955ed1
...
@@ -292,6 +292,7 @@ Referencing the name ``a`` hereafter is an error (at least until another value
...
@@ -292,6 +292,7 @@ Referencing the name ``a`` hereafter is an error (at least until another value
is assigned to it). We'll find other uses for :keyword:`del` later.
is assigned to it). We'll find other uses for :keyword:`del` later.
.. _tut-tuples:
Tuples and Sequences
Tuples and Sequences
====================
====================
...
...
Doc/tutorial/introduction.rst
View file @
5d955ed1
...
@@ -13,9 +13,11 @@ end a multi-line command.
...
@@ -13,9 +13,11 @@ end a multi-line command.
Many of the examples in this manual, even those entered at the interactive
Many of the examples in this manual, even those entered at the interactive
prompt, include comments. Comments in Python start with the hash character,
prompt, include comments. Comments in Python start with the hash character,
``#``, and extend to the end of the physical line. A comment may appear at
``#``, and extend to the end of the physical line. A comment may appear at
the
the
start of a line or following whitespace or code, but not within a string
start of a line or following whitespace or code, but not within a string
literal. A hash character within a string literal is just a hash character.
literal. A hash character within a string literal is just a hash character.
Since comments are to clarify code and are not interpreted by Python, they may
be omitted when typing in examples.
Some examples::
Some examples::
...
@@ -96,6 +98,15 @@ A value can be assigned to several variables simultaneously::
...
@@ -96,6 +98,15 @@ A value can be assigned to several variables simultaneously::
>>> z
>>> z
0
0
Variables must be "defined" (assigned a value) before they can be used, or an
error will occur::
>>> # try to access an undefined variable
... n
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'n' is not defined
There is full support for floating point; operators with mixed type operands
There is full support for floating point; operators with mixed type operands
convert the integer operand to floating point::
convert the integer operand to floating point::
...
@@ -290,7 +301,7 @@ omitted second index defaults to the size of the string being sliced. ::
...
@@ -290,7 +301,7 @@ omitted second index defaults to the size of the string being sliced. ::
>>> word[2:] # Everything except the first two characters
>>> word[2:] # Everything except the first two characters
'lpA'
'lpA'
Unlike a C string, Python strings cannot be changed. Assigning to an
indexed
Unlike a C string, Python strings cannot be changed. Assigning to an indexed
position in the string results in an error::
position in the string results in an error::
>>> word[0] = 'x'
>>> word[0] = 'x'
...
@@ -409,8 +420,8 @@ About Unicode
...
@@ -409,8 +420,8 @@ About Unicode
.. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com>
.. sectionauthor:: Marc-Andre Lemburg <mal@lemburg.com>
Starting with Python 3.0 all strings support Unicode
.
Starting with Python 3.0 all strings support Unicode
(see
(See http://www.unicode.org/)
http://www.unicode.org/).
Unicode has the advantage of providing one ordinal for every character in every
Unicode has the advantage of providing one ordinal for every character in every
script used in modern and ancient texts. Previously, there were only 256
script used in modern and ancient texts. Previously, there were only 256
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment