title stringlengths 4 62 | text stringlengths 12 9.02k | python_version stringclasses 1
value | length int64 12 9.02k | url stringlengths 59 91 |
|---|---|---|---|---|
Lists | ---
node24.html node20.html node22.html node1.html
---
## Lists
Python knows a number of compound data types, used to group
together other values. The most versatile is the list, which
can be written as a list of comma-separated values (items) between
square brackets. List items need not all have the same type.
```text... | 1.2 | 1,850 | https://docs.python.org/{python_version}/python-tut/node23.html |
First Steps Towards Programming | ---
node25.html node19.html node23.html node1.html
---
# First Steps Towards Programming
Of course, we can use Python for more complicated tasks than adding
two and two together. For instance, we can write an initial
subsequence of the Fibonacci series as follows:
```text
>>> # Fibonacci series:
... # the sum of two el... | 1.2 | 2,534 | https://docs.python.org/{python_version}/python-tut/node24.html |
More Control Flow Tools | ---
node26.html tut.html node24.html node1.html
---
# More Control Flow Tools
Besides the while statement just introduced, Python knows the
usual control flow statements known from other languages, with some
twists.
---
- If Statements (node26.html#SECTION00510000000000000000)
For Statements (node27.html#SECTION0052000... | 1.2 | 632 | https://docs.python.org/{python_version}/python-tut/node25.html |
If Statements | ---
node27.html node25.html node25.html node1.html
---
# If Statements
Perhaps the most well-known statement type is the if statement.
For example:
```text
>>> if x < 0:
... x = 0
... print 'Negative changed to zero'
... elif x == 0:
... print 'Zero'
... elif x == 1:
... print 'Single'
... else:
... print 'More'
...
``... | 1.2 | 604 | https://docs.python.org/{python_version}/python-tut/node26.html |
For Statements | ---
node28.html node25.html node26.html node1.html
---
# For Statements
The for statement in Python differs a bit from what you may be
used to in C or Pascal. Rather than always iterating over an
arithmetic progression of numbers (like in Pascal), or leaving the user
completely free in the iteration test and step (as C... | 1.2 | 1,139 | https://docs.python.org/{python_version}/python-tut/node27.html |
The range() Function | ---
node29.html node25.html node27.html node1.html
---
# The range() Function
If you do need to iterate over a sequence of numbers, the built-in
function range() comes in handy. It generates lists containing
arithmetic progressions, e.g.:
```text
>>> range(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>>
```
The given end point ... | 1.2 | 939 | https://docs.python.org/{python_version}/python-tut/node28.html |
Break and Continue Statements, and Else Clauses on Loops | ---
node30.html node25.html node28.html node1.html
---
# Break and Continue Statements, and Else Clauses on Loops
The break statement, like in C, breaks out of the smallest
enclosing for or while loop.
The continue statement, also borrowed from C, continues with the
next iteration of the loop.
Loop statements may have ... | 1.2 | 939 | https://docs.python.org/{python_version}/python-tut/node29.html |
Where From Here | ---
node4.html node2.html node2.html node1.html
---
# Where From Here
Now that you are all excited about Python, you'll want to examine it
in some more detail. Since the best way to learn a language is
using it, you are invited here to do so.
In the next chapter, the mechanics of using the interpreter are
explained. Th... | 1.2 | 984 | https://docs.python.org/{python_version}/python-tut/node3.html |
Pass Statements | ---
node31.html node25.html node29.html node1.html
---
# Pass Statements
The pass statement does nothing.
It can be used when a statement is required syntactically but the
program requires no action.
For example:
```text
>>> while 1:
... pass # Busy-wait for keyboard interrupt
...
```
---
guido@cwi.nl | 1.2 | 302 | https://docs.python.org/{python_version}/python-tut/node30.html |
Defining Functions | ---
node32.html node25.html node30.html node1.html
---
# Defining Functions
We can create a function that writes the Fibonacci series to an
arbitrary boundary:
```text
>>> def fib(n): # write Fibonacci series up to n
... a, b = 0, 1
... while b < n:
... print b,
... a, b = b, a+b
...
>>> # Now call the function we just... | 1.2 | 3,800 | https://docs.python.org/{python_version}/python-tut/node31.html |
Odds and Ends | ---
node33.html tut.html node31.html node1.html
---
# Odds and Ends
This chapter describes some things you've learned about already in
more detail, and adds some new things as well.
---
- More on Lists (node33.html#SECTION00610000000000000000)
The del statement (node34.html#SECTION00620000000000000000)
Tuples and Seque... | 1.2 | 577 | https://docs.python.org/{python_version}/python-tut/node32.html |
More on Lists | ---
node34.html node32.html node32.html node1.html
---
# More on Lists
The list data type has some more methods. Here are all of the methods
of lists objects:
An example that uses all list methods:
```text
>>> a = [66.6, 333, 333, 1, 1234.5]
>>> print a.count(333), a.count(66.6), a.count('x')
2 1 0
>>> a.insert(2, -1)
... | 1.2 | 585 | https://docs.python.org/{python_version}/python-tut/node33.html |
The del statement | ---
node35.html node32.html node33.html node1.html
---
# The del statement
There is a way to remove an item from a list given its index instead
of its value: the del statement. This can also be used to
remove slices from a list (which we did earlier by assignment of an
empty list to the slice). For example:
```text
>>>... | 1.2 | 672 | https://docs.python.org/{python_version}/python-tut/node34.html |
Tuples and Sequences | ---
node36.html node32.html node34.html node1.html
---
# Tuples and Sequences
We saw that lists and strings have many common properties, e.g.,
indexing and slicing operations. They are two examples of
sequence data types. Since Python is an evolving language, other
sequence data types may be added. There is also anothe... | 1.2 | 2,478 | https://docs.python.org/{python_version}/python-tut/node35.html |
Dictionaries | ---
node37.html node32.html node35.html node1.html
---
# Dictionaries
Another useful data type built into Python is the dictionary.
Dictionaries are sometimes found in other languages as ``associative
memories'' or ``associative arrays''. Unlike sequences, which are
indexed by a range of numbers, dictionaries are index... | 1.2 | 1,800 | https://docs.python.org/{python_version}/python-tut/node36.html |
More on Conditions | ---
node38.html node32.html node36.html node1.html
---
# More on Conditions
The conditions used in while and if statements above can
contain other operators besides comparisons.
The comparison operators in and not in check whether a value
occurs (does not occur) in a sequence. The operators is and
is not compare whethe... | 1.2 | 1,790 | https://docs.python.org/{python_version}/python-tut/node37.html |
Comparing Sequences and Other Types | ---
node39.html node32.html node37.html node1.html
---
# Comparing Sequences and Other Types
Sequence objects may be compared to other objects with the same
sequence type. The comparison uses lexicographical ordering:
first the first two items are compared, and if they differ this
determines the outcome of the comparis... | 1.2 | 1,450 | https://docs.python.org/{python_version}/python-tut/node38.html |
Modules | ---
node40.html tut.html node38.html node1.html
---
# Modules
If you quit from the Python interpreter and enter it again, the
definitions you have made (functions and variables) are lost.
Therefore, if you want to write a somewhat longer program, you are
better off using a text editor to prepare the input for the inter... | 1.2 | 2,529 | https://docs.python.org/{python_version}/python-tut/node39.html |
Using the Python Interpreter | ---
node5.html tut.html node3.html node1.html
---
# Using the Python Interpreter
---
- Invoking the Interpreter (node5.html#SECTION00310000000000000000)
- Argument Passing (node6.html#SECTION00311000000000000000)
Interactive Mode (node7.html#SECTION00312000000000000000)
The Interpreter and its Environment (node8.html#S... | 1.2 | 1,015 | https://docs.python.org/{python_version}/python-tut/node4.html |
More on Modules | ---
node41.html node39.html node39.html node1.html
---
# More on Modules
A module can contain executable statements as well as function
definitions.
These statements are intended to initialize the module.
They are executed only the
first
time the module is imported somewhere.
footnode.html#288
Each module has its own p... | 1.2 | 1,594 | https://docs.python.org/{python_version}/python-tut/node40.html |
Standard Modules | ---
node42.html node39.html node40.html node1.html
---
# Standard Modules
Python comes with a library of standard modules, described in a separate
document (Python Library Reference). Some modules are built into the
interpreter; these provide access to operations that are not part of the
core of the language but are ne... | 1.2 | 1,347 | https://docs.python.org/{python_version}/python-tut/node41.html |
The dir() function | ---
node43.html node39.html node41.html node1.html
---
# The dir() function
The built-in function dir is used to find out which names a module
defines. It returns a sorted list of strings:
```text
>>> import fibo, sys
>>> dir(fibo)
['__name__', 'fib', 'fib2']
>>> dir(sys)
['__name__', 'argv', 'builtin_module_names', 'c... | 1.2 | 1,590 | https://docs.python.org/{python_version}/python-tut/node42.html |
Output Formatting | ---
node44.html tut.html node42.html node1.html
---
# Output Formatting
So far we've encountered two ways of writing values: expression
statements and the print statement. (A third way is using the
write method of file objects; the standard output file can be
referenced as sys.stdout. See the Library Reference for more... | 1.2 | 3,277 | https://docs.python.org/{python_version}/python-tut/node43.html |
Errors and Exceptions | ---
node45.html tut.html node43.html node1.html
---
# Errors and Exceptions
Until now error messages haven't been more than mentioned, but if you
have tried out the examples you have probably seen some. There are
(at least) two distinguishable kinds of errors: syntax errors
and exceptions.
---
- Syntax Errors (node45.h... | 1.2 | 679 | https://docs.python.org/{python_version}/python-tut/node44.html |
Syntax Errors | ---
node46.html node44.html node44.html node1.html
---
# Syntax Errors
Syntax errors, also known as parsing errors, are perhaps the most common
kind of complaint you get while you are still learning Python:
```text
>>> while 1 print 'Hello world'
File "<stdin>", line 1
while 1 print 'Hello world'
^
SyntaxError: invalid... | 1.2 | 775 | https://docs.python.org/{python_version}/python-tut/node45.html |
Exceptions | ---
node47.html node44.html node45.html node1.html
---
# Exceptions
Even if a statement or expression is syntactically correct, it may
cause an error when an attempt is made to execute it.
Errors detected during execution are called exceptions and are
not unconditionally fatal: you will soon learn how to handle them in... | 1.2 | 1,774 | https://docs.python.org/{python_version}/python-tut/node46.html |
Handling Exceptions | ---
node48.html node44.html node46.html node1.html
---
# Handling Exceptions
It is possible to write programs that handle selected exceptions.
Look at the following example, which prints a table of inverses of
some floating point numbers:
```text
>>> numbers = [0.3333, 2.5, 0, 10]
>>> for x in numbers:
... print x,
...... | 1.2 | 2,867 | https://docs.python.org/{python_version}/python-tut/node47.html |
Raising Exceptions | ---
node49.html node44.html node47.html node1.html
---
# Raising Exceptions
The raise statement allows the programmer to force a specified
exception to occur.
For example:
```text
>>> raise NameError, 'HiThere'
Traceback (innermost last):
File "<stdin>", line 1
NameError: HiThere
>>>
```
The first argument to raise nam... | 1.2 | 432 | https://docs.python.org/{python_version}/python-tut/node48.html |
User-defined Exceptions | ---
node50.html node44.html node48.html node1.html
---
# User-defined Exceptions
Programs may name their own exceptions by assigning a string to a
variable.
For example:
```text
>>> my_exc = 'my_exc'
>>> try:
... raise my_exc, 2*2
... except my_exc, val:
... print 'My exception occurred, value:', val
...
My exception o... | 1.2 | 532 | https://docs.python.org/{python_version}/python-tut/node49.html |
Invoking the Interpreter | ---
node6.html node4.html node4.html node1.html
---
# Invoking the Interpreter
The Python interpreter is usually installed as /usr/local/bin/python
on those machines where it is available; putting /usr/local/bin in
your Unix shell's search path makes it possible to start it by
typing the command
```text
python
```
to t... | 1.2 | 2,147 | https://docs.python.org/{python_version}/python-tut/node5.html |
Defining Clean-up Actions | ---
node51.html node44.html node49.html node1.html
---
# Defining Clean-up Actions
The try statement has another optional clause which is intended to
define clean-up actions that must be executed under all circumstances.
For example:
```text
>>> try:
... raise KeyboardInterrupt
... finally:
... print 'Goodbye, world!'
... | 1.2 | 824 | https://docs.python.org/{python_version}/python-tut/node50.html |
Classes | ---
node52.html tut.html node50.html node1.html
---
# Classes
Python's class mechanism adds classes to the language with a minimum
of new syntax and semantics. It is a mixture of the class mechanisms
found in C++ and Modula-3. As is true for modules, classes in Python
do not put an absolute barrier between definition a... | 1.2 | 2,293 | https://docs.python.org/{python_version}/python-tut/node51.html |
A word about terminology | ---
node53.html node51.html node51.html node1.html
---
# A word about terminology
Lacking universally accepted terminology to talk about classes, I'll
make occasional use of Smalltalk and C++ terms. (I'd use Modula-3
terms, since its object-oriented semantics are closer to those of
Python than C++, but I expect that fe... | 1.2 | 1,731 | https://docs.python.org/{python_version}/python-tut/node52.html |
Python scopes and name spaces | ---
node54.html node51.html node52.html node1.html
---
# Python scopes and name spaces
Before introducing classes, I first have to tell you something about
Python's scope rules. Class definitions play some neat tricks with
name spaces, and you need to know how scopes and name spaces work to
fully understand what's goin... | 1.2 | 5,008 | https://docs.python.org/{python_version}/python-tut/node53.html |
A first look at classes | ---
node55.html node51.html node53.html node1.html
---
# A first look at classes
Classes introduce a little bit of new syntax, three new object types,
and some new semantics.
---
- Class definition syntax (node55.html#SECTION001031000000000000000)
Class objects (node56.html#SECTION001032000000000000000)
Instance object... | 1.2 | 439 | https://docs.python.org/{python_version}/python-tut/node54.html |
Class definition syntax | ---
node56.html node54.html node54.html node1.html
---
## Class definition syntax
The simplest form of class definition looks like this:
```text
class ClassName:
<statement-1>
.
.
.
<statement-N>
```
Class definitions, like function definitions (`def` statements)
must be executed before they have any effect. (You could... | 1.2 | 1,488 | https://docs.python.org/{python_version}/python-tut/node55.html |
Class objects | ---
node57.html node54.html node55.html node1.html
---
## Class objects
Class objects support two kinds of operations: attribute references
and instantiation.
Attribute references use the standard syntax used for all
attribute references in Python: `obj.name`. Valid attribute
names are all the names that were in the cl... | 1.2 | 1,028 | https://docs.python.org/{python_version}/python-tut/node56.html |
Instance objects | ---
node58.html node54.html node56.html node1.html
---
## Instance objects
Now what can we do with instance objects? The only operations
understood by instance objects are attribute references. There are
two kinds of valid attribute names.
The first I'll call data attributes. These correspond to
``instance variables'' ... | 1.2 | 1,626 | https://docs.python.org/{python_version}/python-tut/node57.html |
Method objects | ---
node59.html node54.html node57.html node1.html
---
## Method objects
Usually, a method is called immediately, e.g.:
```text
x.f()
```
In our example, this will return the string `'hello world'`.
However, it is not necessary to call a method right away: `x.f`
is a method object, and can be stored away and called at ... | 1.2 | 1,975 | https://docs.python.org/{python_version}/python-tut/node58.html |
Random remarks | ---
node60.html node51.html node58.html node1.html
---
# Random remarks
[These should perhaps be placed more carefully...]
Data attributes override method attributes with the same name; to
avoid accidental name conflicts, which may cause hard-to-find bugs in
large programs, it is wise to use some kind of convention tha... | 1.2 | 4,553 | https://docs.python.org/{python_version}/python-tut/node59.html |
Argument Passing | ---
node7.html node5.html node5.html node1.html
---
## Argument Passing
When known to the interpreter, the script name and additional
arguments thereafter are passed to the script in the variable
sys.argv, which is a list of strings. Its length is at least one;
when no script and no arguments are given, sys.argv[0] is ... | 1.2 | 638 | https://docs.python.org/{python_version}/python-tut/node6.html |
Inheritance | ---
node61.html node51.html node59.html node1.html
---
# Inheritance
Of course, a language feature would not be worthy of the name ``class''
without supporting inheritance. The syntax for a derived class
definition looks as follows:
```text
class DerivedClassName(BaseClassName):
<statement-1>
.
.
.
<statement-N>
```
Th... | 1.2 | 2,230 | https://docs.python.org/{python_version}/python-tut/node60.html |
Multiple inheritance | ---
node62.html node60.html node60.html node1.html
---
## Multiple inheritance
Python supports a limited form of multiple inheritance as well. A
class definition with multiple base classes looks as follows:
```text
class DerivedClassName(Base1, Base2, Base3):
<statement-1>
.
.
.
<statement-N>
```
The only rule necessar... | 1.2 | 1,648 | https://docs.python.org/{python_version}/python-tut/node61.html |
Odds and ends | ---
node63.html node51.html node61.html node1.html
---
# Odds and ends
Sometimes it is useful to have a data type similar to the Pascal
``record'' or C ``struct'', bundling together a couple of named data
items. An empty class definition will do nicely, e.g.:
```text
class Employee:
pass
john = Employee() # Create an ... | 1.2 | 1,298 | https://docs.python.org/{python_version}/python-tut/node62.html |
Recent Additions | ---
node64.html tut.html node62.html node1.html
---
# Recent Additions
Python is an evolving language. Since this tutorial was last
thoroughly revised, several new features have been added to the
language. While ideally I should revise the tutorial to incorporate
them in the mainline of the text, lack of time currently... | 1.2 | 2,333 | https://docs.python.org/{python_version}/python-tut/node63.html |
The Last Printed Expression | ---
node65.html node63.html node63.html node1.html
---
# The Last Printed Expression
In interactive mode, the last printed expression is assigned to the
variable `_`. This means that when you are using Python as a
desk calculator, it is somewhat easier to continue calculations, for
example:
```text
>>> tax = 17.5 / 100... | 1.2 | 767 | https://docs.python.org/{python_version}/python-tut/node64.html |
String Literals | ---
node66.html node63.html node64.html node1.html
---
# String Literals
---
- Double Quotes (node66.html#SECTION001121000000000000000)
Continuation Of String Literals (node67.html#SECTION001122000000000000000)
Triple-quoted strings (node68.html#SECTION001123000000000000000)
String Literal Juxtaposition (node69.html#SE... | 1.2 | 364 | https://docs.python.org/{python_version}/python-tut/node65.html |
Double Quotes | ---
node67.html node65.html node65.html node1.html
---
## Double Quotes
Python can now also use double quotes to surround string literals,
e.g. `"this doesn't hurt a bit"`. There is no semantic
difference between strings surrounded by single or double quotes.
---
guido@cwi.nl | 1.2 | 276 | https://docs.python.org/{python_version}/python-tut/node66.html |
Continuation Of String Literals | ---
node68.html node65.html node66.html node1.html
---
## Continuation Of String Literals
String literals can span multiple lines by escaping newlines with
backslashes, e.g.
```text
hello = "This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
Note that whitespace at the beginn... | 1.2 | 588 | https://docs.python.org/{python_version}/python-tut/node67.html |
Triple-quoted strings | ---
node69.html node65.html node67.html node1.html
---
## Triple-quoted strings
In some cases, when you need to include really long strings (e.g.
containing several paragraphs of informational text), it is annoying
that you have to terminate each line with `\n\`, especially if
you would like to reformat the text occasi... | 1.2 | 941 | https://docs.python.org/{python_version}/python-tut/node68.html |
String Literal Juxtaposition | ---
node70.html node65.html node68.html node1.html
---
## String Literal Juxtaposition
One final twist: you can juxtapose multiple string literals. Two or
more adjacent string literals (but not arbitrary expressions!)
separated only by whitespace will be concatenated (without intervening
whitespace) into a single strin... | 1.2 | 778 | https://docs.python.org/{python_version}/python-tut/node69.html |
Interactive Mode | ---
node8.html node5.html node6.html node1.html
---
## Interactive Mode
When commands are read from a tty, the interpreter is said to be in
interactive mode. In this mode it prompts for the next command
with the primary prompt, usually three greater-than signs (
>>>); for continuation lines it prompts with the secondar... | 1.2 | 721 | https://docs.python.org/{python_version}/python-tut/node7.html |
The Formatting Operator | ---
node71.html node63.html node69.html node1.html
---
# The Formatting Operator
---
- Basic Usage (node71.html#SECTION001131000000000000000)
Referencing Variables By Name (node72.html#SECTION001132000000000000000)
---
guido@cwi.nl | 1.2 | 231 | https://docs.python.org/{python_version}/python-tut/node70.html |
Basic Usage | ---
node72.html node70.html node70.html node1.html
---
## Basic Usage
The chapter on output formatting is really out of date: there is now
an almost complete interface to C-style printf formats. This is done
by overloading the modulo operator (`%`) for a left operand
which is a string, e.g.
```text
>>> import math
>>> ... | 1.2 | 1,164 | https://docs.python.org/{python_version}/python-tut/node71.html |
Referencing Variables By Name | ---
node73.html node70.html node71.html node1.html
---
## Referencing Variables By Name
If you have a really long format string that you don't want to split
up, it would be nice if you could reference the variables to be
formatted by name instead of by position. This can be done by using
an extension of C formats using... | 1.2 | 698 | https://docs.python.org/{python_version}/python-tut/node72.html |
Optional Function Arguments | ---
node74.html node63.html node72.html node1.html
---
# Optional Function Arguments
It is now possible to define functions with a variable number of
arguments. There are two forms, which can be combined.
---
- Default Argument Values (node74.html#SECTION001141000000000000000)
Arbitrary Argument Lists (node75.html#SECT... | 1.2 | 362 | https://docs.python.org/{python_version}/python-tut/node73.html |
Default Argument Values | ---
node75.html node73.html node73.html node1.html
---
## Default Argument Values
The most useful form is to specify a default value for one or more
arguments. This creates a function that can be called with fewer
arguments than it is defined, e.g.
```text
def ask_ok(prompt, retries = 4, complaint = 'Yes or no, please!... | 1.2 | 863 | https://docs.python.org/{python_version}/python-tut/node74.html |
Arbitrary Argument Lists | ---
node76.html node73.html node74.html node1.html
---
## Arbitrary Argument Lists
It is also possible to specify that a function can be 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
arguments may occur, e.g.
```... | 1.2 | 550 | https://docs.python.org/{python_version}/python-tut/node75.html |
Lambda And Functional Programming Tools | ---
node77.html node63.html node75.html node1.html
---
# Lambda And Functional Programming Tools
---
- Lambda Forms (node77.html#SECTION001151000000000000000)
Map, Reduce and Filter (node78.html#SECTION001152000000000000000)
- Map. (node79.html#SECTION001152100000000000000)
Filter. (node80.html#SECTION00115220000000000... | 1.2 | 393 | https://docs.python.org/{python_version}/python-tut/node76.html |
Lambda Forms | ---
node78.html node76.html node76.html node1.html
---
## Lambda Forms
By popular demand, a few features commonly found in functional
programming languages and Lisp have been added to Python. With the
`lambda` keyword, small anonymous functions can be created.
Here's a function that returns the sum of its two arguments... | 1.2 | 813 | https://docs.python.org/{python_version}/python-tut/node77.html |
Map, Reduce and Filter | ---
node79.html node76.html node77.html node1.html
---
## Map, Reduce and Filter
Three new built-in functions on sequences are good candidate to pass
lambda forms.
---
- Map. (node79.html#SECTION001152100000000000000)
Filter. (node80.html#SECTION001152200000000000000)
Reduce. (node81.html#SECTION001152300000000000000)
... | 1.2 | 336 | https://docs.python.org/{python_version}/python-tut/node78.html |
Map. | ---
node80.html node78.html node78.html node1.html
---
### Map.
`map(function, sequence)` calls `function(item)` for each of
the sequence's items and returns a list of the return values. For
example, to compute some cubes:
```text
>>> map(lambda x: x*x*x, range(1, 11))
[1, 8, 27, 64, 125, 216, 343, 512, 729, 1000]
>>>
... | 1.2 | 956 | https://docs.python.org/{python_version}/python-tut/node79.html |
The Interpreter and its Environment | ---
node9.html node4.html node7.html node1.html
---
# The Interpreter and its Environment
---
- Error Handling (node9.html#SECTION00321000000000000000)
The Module Search Path (node10.html#SECTION00322000000000000000)
``Compiled'' Python files (node11.html#SECTION00323000000000000000)
Executable Python scripts (node12.h... | 1.2 | 440 | https://docs.python.org/{python_version}/python-tut/node8.html |
Filter. | ---
node81.html node78.html node79.html node1.html
---
### Filter.
`filter(function, sequence)` returns a sequence (of the same
type, if possible) consisting of those items from the sequence for
which `function(item)` is true. For example, to compute some
primes:
```text
>>> filter(lambda x: x%2 != 0 and x%3 != 0, rang... | 1.2 | 381 | https://docs.python.org/{python_version}/python-tut/node80.html |
Reduce. | ---
node82.html node78.html node80.html node1.html
---
### Reduce.
`reduce(function, sequence)` returns a single value constructed
by calling the (binary) function on the first two items of the
sequence, then on the result and the next item, and so on. For
example, to compute the sum of the numbers 1 through 10:
```tex... | 1.2 | 905 | https://docs.python.org/{python_version}/python-tut/node81.html |
Continuation Lines Without Backslashes | ---
node83.html node63.html node81.html node1.html
---
# Continuation Lines Without Backslashes
While the general mechanism for continuation of a source line on the
next physical line remains to place a backslash on the end of the
line, expressions inside matched parentheses (or square brackets, or
curly braces) can no... | 1.2 | 759 | https://docs.python.org/{python_version}/python-tut/node82.html |
Regular Expressions | ---
node84.html node63.html node82.html node1.html
---
# Regular Expressions
While C's printf-style output formats, transformed into Python, are
adequate for most output formatting jobs, C's scanf-style input
formats are not very powerful. Instead of scanf-style input, Python
offers Emacs-style regular expressions as a... | 1.2 | 456 | https://docs.python.org/{python_version}/python-tut/node83.html |
Generalized Dictionaries | ---
node85.html node63.html node83.html node1.html
---
# Generalized Dictionaries
The keys of dictionaries are no longer restricted to strings --- they
can be any immutable basic type including strings, numbers, tuples, or
(certain) class instances. (Lists and dictionaries are not acceptable
as dictionary keys, in orde... | 1.2 | 850 | https://docs.python.org/{python_version}/python-tut/node84.html |
Miscellaneous New Built-in Functions | ---
node86.html node63.html node84.html node1.html
---
# Miscellaneous New Built-in Functions
The function `vars()` returns a dictionary containing the current
local variables. With a module argument, it returns that module's
global variables. The old function `dir(x)` returns
`vars(x).keys()`.
The function `round(x)` ... | 1.2 | 1,618 | https://docs.python.org/{python_version}/python-tut/node85.html |
Else Clause For Try Statement | ---
node87.html node63.html node85.html node1.html
---
# Else Clause For Try Statement
The `try...except` statement now has an optional `else`
clause, which must follow all `except` clauses. It is useful to
place code that must be executed if the `try` clause does not
raise an exception. For example:
```text
for arg in... | 1.2 | 478 | https://docs.python.org/{python_version}/python-tut/node86.html |
New Class Features in Release 1.1 | ---
node88.html node63.html node86.html node1.html
---
# New Class Features in Release 1.1
Some changes have been made to classes: the operator overloading
mechanism is more flexible, providing more support for non-numeric use
of operators (including calling an object as if it were a function),
and it is possible to tr... | 1.2 | 573 | https://docs.python.org/{python_version}/python-tut/node87.html |
New Operator Overloading | ---
node89.html node87.html node87.html node1.html
---
## New Operator Overloading
It is no longer necessary to coerce both sides of an operator to the
same class or type. A class may still provide a `__coerce__`
method, but this method may return objects of different types or
classes if it feels like it. If no `__coer... | 1.2 | 1,535 | https://docs.python.org/{python_version}/python-tut/node88.html |
Trapping Attribute Access | ---
node90.html node87.html node88.html node1.html
---
## Trapping Attribute Access
You can define three new ``magic'' methods in a class now:
`__getattr__(self, name)`, `__setattr__(self, name, value)`
and `__delattr__(self, name)`.
The `__getattr__` method is called when an attribute access fails,
i.e. when an attrib... | 1.2 | 2,140 | https://docs.python.org/{python_version}/python-tut/node89.html |
Error Handling | ---
node10.html node8.html node8.html node1.html
---
## Error Handling
When an error occurs, the interpreter prints an error
message and a stack trace. In interactive mode, it then returns to
the primary prompt; when input came from a file, it exits with a
nonzero exit status after printing
the stack trace. (Exceptions... | 1.2 | 1,002 | https://docs.python.org/{python_version}/python-tut/node9.html |
Calling a Class Instance | ---
node91.html node87.html node89.html node1.html
---
## Calling a Class Instance
If a class defines a method `__call__` it is possible to call its
instances as if they were functions. For example:
```text
class PresetSomeArguments:
def __init__(self, func, *args):
self.func, self.args = func, args
def __call__(self, ... | 1.2 | 544 | https://docs.python.org/{python_version}/python-tut/node90.html |
New in Release 1.2 | ---
node92.html tut.html node90.html node1.html
---
# New in Release 1.2
This chapter describes even more recent additions to the Python
language and library.
---
- New Class Features (node92.html#SECTION001210000000000000000)
Unix Signal Handling (node93.html#SECTION001220000000000000000)
Exceptions Can Be Classes (no... | 1.2 | 853 | https://docs.python.org/{python_version}/python-tut/node91.html |
New Class Features | ---
node93.html node91.html node91.html node1.html
---
# New Class Features
The semantics of `__coerce__` have been changed to be more
reasonable. As an example, the new standard module `Complex`
implements fairly complete complex numbers using this. Additional
examples of classes with and without `__coerce__` methods ... | 1.2 | 885 | https://docs.python.org/{python_version}/python-tut/node92.html |
Unix Signal Handling | ---
node94.html node91.html node92.html node1.html
---
# Unix Signal Handling
On Unix, Python now supports signal handling. The module
`signal` exports functions `signal`, `pause` and
`alarm`, which act similar to their Unix counterparts. The
module also exports the conventional names for the various signal
classes (al... | 1.2 | 475 | https://docs.python.org/{python_version}/python-tut/node93.html |
Exceptions Can Be Classes | ---
node95.html node91.html node93.html node1.html
---
# Exceptions Can Be Classes
User-defined exceptions are no longer limited to being string objects
--- they can be identified by classes as well. Using this mechanism it
is possible to create extensible hierarchies of exceptions.
There are two new valid (semantic) f... | 1.2 | 1,525 | https://docs.python.org/{python_version}/python-tut/node94.html |
Object Persistency and Object Copying | ---
node96.html node91.html node94.html node1.html
---
# Object Persistency and Object Copying
Two new modules, `pickle` and `shelve`, support storage and
retrieval of (almost) arbitrary Python objects on disk, using the
`dbm` package. A third module, `copy`, provides flexible
object copying operations. More informatio... | 1.2 | 527 | https://docs.python.org/{python_version}/python-tut/node95.html |
Persistent Objects | ---
node97.html node95.html node95.html node1.html
---
## Persistent Objects
The module `pickle` provides a general framework for objects to
disassemble themselves into a stream of bytes and to reassemble such a
stream back into an object. It copes with reference sharing,
recursive objects and instances of user-defined... | 1.2 | 1,592 | https://docs.python.org/{python_version}/python-tut/node96.html |
Copying Objects | ---
node98.html node95.html node96.html node1.html
---
## Copying Objects
The module `copy` exports two functions: `copy()` and
`deepcopy()`. The `copy()` function returns a ``shallow''
copy of an object; `deepcopy()` returns a ``deep'' copy. The
difference between shallow and deep copying is only relevant for
compound... | 1.2 | 913 | https://docs.python.org/{python_version}/python-tut/node97.html |
Documentation Strings | ---
node99.html node91.html node97.html node1.html
---
# Documentation Strings
A variety of objects now have a new attribute, `__doc__`, which
is supposed to contain a documentation string (if no documentation is
present, the attribute is `None`). New syntax, compatible with
the old interpreter, allows for convenient i... | 1.2 | 4,088 | https://docs.python.org/{python_version}/python-tut/node98.html |
Customizing Import and Built-Ins | ---
node100.html node91.html node98.html node1.html
---
# Customizing Import and Built-Ins
In preparation for a ``restricted execution mode'' which will be
usable to run code received from an untrusted source (such as a WWW
server or client), the mechanism by which modules are imported has
been redesigned. It is now po... | 1.2 | 1,369 | https://docs.python.org/{python_version}/python-tut/node99.html |
Python Tutorial | ---
node1.html node1.html
---
# Python Tutorial
Guido van Rossum
Dept. AA, CWI, P.O. Box 94079
1090 GB Amsterdam, The Netherlands
E-mail: guido@cwi.nl
10 April 1995
Release 1.2
Copyright © 1991-1995 by Stichting Mathematisch Centrum,
Amsterdam, The Netherlands.
All Rights Reserved
Permission to use, copy, modify, and d... | 1.2 | 9,016 | https://docs.python.org/{python_version}/python-tut/tut.html |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.