Reviews & Opinions
Independent and trusted. Read before buy Python Tutorial Python 2 2!

Python Tutorial Python 2 2


Bookmark
Python Tutorial Python 2 2

Bookmark and Share

 

Python Tutorial Python 2 2About Python Tutorial Python 2 2
Here you can find all about Python Tutorial Python 2 2 like manual and other informations. For example: review.

Python Tutorial Python 2 2 manual (user guide) is ready to download for free.

On the bottom of page users can write a review. If you own a Python Tutorial Python 2 2 please write about it to help other people.
[ Report abuse or wrong photo | Share your Python Tutorial Python 2 2 photo ]

 

 

Manual

Download (Romanian)
Python Tutorial Python 2.2, size: 273 KB
Download (English)
Check if your language version is avaliable.
Most of manuals are avaliable in many languages.

 

Python Tutorial Python 2 2

 

 

Video review

Python Programming Tutorial 2 Numbers and Math

 

User reviews and opinions

<== Click here to post a new opinion, comment, review, etc.

Comments to date: 7. Page 1 of 1. Average Rating:
knomkrad 5:27pm on Wednesday, October 13th, 2010 
battery works just as good as the more expensive one tmobile sells....great price and works perfectly I got this battery because my original battery was really drained out and died within a couple hours.
Christian.M 8:43pm on Tuesday, October 12th, 2010 
Hi I was a Sidekick user before I got the G1. The G1 is not what I expected. The battery really sucks. Then the MSN and AIM. Okay;; Soo I was really excited wen this G1 came out...I left my old sidekick for this one...at first it was all good...
RealNitro 1:11am on Thursday, October 7th, 2010 
I will keep this review short and straight to the point, folks. You know how Google likes to keep things in Beta for years and years? Well. Google phone will be sold starting in the United States in October for U.S. $ 179 and in the UK in early November. T-Mobile G1 from Google est le premier modèle of Sild avec un téléphone avec clavier azerty complete. Ce téléphone have a screen size of 3.17 POUCES.
sinha_ashim 1:39am on Sunday, July 25th, 2010 
I love this G1 phone I got from T-Mobile. Extremely fast and handy. Fast internet and user-friendly. Free applications are so cool! I think the most exciting thing about this phone is the future of it. It allows others to create applications for it.
sponsor3211 10:08am on Friday, June 25th, 2010 
Wow this phone feels like if you are carrying a portable computer . Everything is from a finger touch away. Its awesome its fast.
Cemberly22si 12:20am on Monday, June 7th, 2010 
Fit my phone perfectly, good replacement for the back door. Battery life is great, no problems, would buy again if needed.
gemal 12:01am on Friday, May 21st, 2010 
I love this phone! But, I text a lot, and my battery life does not last much more than 6 hours, at 7 hours, its dead. Its so frustrating.

Comments posted on www.ps2netdrivers.net are solely the views and opinions of the people posting them and do not necessarily reflect the views or opinions of us.

 

Documents

doc0

import os filename = os.environ.get(PYTHONSTARTUP) if filename and os.path.isfile(filename): execfile(filename)
2.2. The Interpreter and Its Environment
An Informal Introduction to Python
In the following examples, input and output are distinguished by the presence or absence of prompts (>>> and. ): to repeat the example, you must type everything after the prompt, when the prompt appears; lines that do not begin with a prompt are output from the interpreter. Note that a secondary prompt on a line by itself in an example means you must type a blank line; this is used to end a multi-line command. Many of the examples in this manual, even those entered at the interactive 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 the 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. Some examples:
# this is the first comment SPAM = 1 # and this is the second comment #. and now a third! STRING = "# This is not a comment."
Using Python as a Calculator
Lets try some simple Python commands. Start the interpreter and wait for the primary prompt, >>>. (It shouldnt take long.)

3.1.1 Numbers

The interpreter acts as a simple calculator: you can type an expression at it and it will write the value. Expression syntax is straightforward: the operators +, -, * and / work just like in most other languages (for example, Pascal or C); parentheses can be used for grouping. For example:
>>> 4 >>>. 4 >>> 4 >>> 5 >>>. 2 >>> -3
2+2 # This is a comment 2+2 2+2 # and a comment on the same line as code
(50-5*6)/4 # Integer division returns the floor: 7/3 7/-3
Like in C, the equal sign (=) is used to assign a value to a variable. The value of an assignment is not written:
>>> width = 20 >>> height = 5*9 >>> width * height 900
A value can be assigned to several variables simultaneously:
>>> >>> 0 >>> 0 >>> 0 x = y = z = 0 x y z # Zero x, y and z
There is full support for oating point; operators with mixed type operands convert the integer operand to oating point:
>>> 3 * 3.75 / 1.5 7.5 >>> 7.0 / 2 3.5
Complex numbers are also supported; imaginary numbers are written with a sufx of j or J. Complex numbers with a nonzero real component are written as (real+imagj), or can be created with the complex(real, imag) function.

String literals can span multiple lines in several ways. Continuation lines can be used, with a backslash as the last character on the line indicating that the next line is a logical continuation of the line:
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 beginning of the line is\ significant." print hello
Note that newlines would still need to be embedded in the string using \n; the newline following the trailing backslash is discarded. This example would print the following:
This is a rather long string containing several lines of text just as you would do in C. Note that whitespace at the beginning of the line is significant.
If we make the string literal a raw string, however, the \n sequences are not converted to newlines, but the backslash at the end of the line, and the newline character in the source, are both included in the string as data. Thus, the example:
hello = r"This is a rather long string containing\n\ several lines of text much as you would do in C." print hello

would print:

This is a rather long string containing\n\ several lines of text much as you would do in C.
Or, strings can be surrounded in a pair of matching triple-quotes: """ or. End of lines do not need to be escaped when using triple-quotes, but they will be included in the string.
print """ Usage: thingy [OPTIONS] -h -H hostname """
Display this usage message Hostname to connect to
produces the following output:
Usage: thingy [OPTIONS] -h -H hostname
The interpreter prints the result of string operations in the same way as they are typed for input: inside quotes, and with quotes and other funny characters escaped by backslashes, to show the precise value. The string is enclosed in double quotes if the string contains a single quote and no double quotes, else its enclosed in single quotes. (The print statement, described later, can be used to write strings without quotes or escapes.) Strings can be concatenated (glued together) with the + operator, and repeated with *:
>>> word = Help + A >>> word HelpA >>> < + word*5 + > <HelpAHelpAHelpAHelpAHelpA>
Two string literals next to each other are automatically concatenated; the rst line above could also have been written word = Help A; this only works with two literals, not with arbitrary string expressions:

But note that -0 is really the same as 0, so it does not count from the right!
>>> word[-0] H # (since -0 equals 0)
Out-of-range negative slice indices are truncated, but dont try this for single-element (non-slice) indices:
>>> word[-100:] HelpA >>> word[-10] # error Traceback (most recent call last): File "<stdin>", line 1, in ? IndexError: string index out of range
The best way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the rst character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:
+---+---+---+---+---+ | H | e | l | p | A | +---+---+---+---+---+ -5 -4 -3 -2 -1
The rst row of numbers gives the position of the indices 0.5 in the string; the second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively. For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of word[1:3] is 2. The built-in function len() returns the length of a string:
>>> s = supercalifragilisticexpialidocious >>> len(s) 34

See Also: Sequence Types

(./lib/typesseq.html)
Strings, and the Unicode strings described in the next section, are examples of sequence types, and support the common operations supported by such types. String Methods
(./lib/string-methods.html)
Both strings and Unicode strings support a large number of methods for basic transformations and searching. String Formatting Operations
(./lib/typesseq-strings.html)
The formatting operations invoked when strings and Unicode strings are the left operand of the % operator are described in more detail here.

3.1.3 Unicode Strings

Starting with Python 2.0 a new data type for storing text data is available to the programmer: the Unicode object. It can be used to store and manipulate Unicode data (see http://www.unicode.org/) and integrates well with the existing string objects providing auto-conversions where necessary. 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 possible ordinals for script characters and texts were typically bound to a code page which mapped the ordinals to script characters. This lead to very much confusion especially with respect to internationalization (usually written as i18n i + 18 characters + n) of software. Unicode solves these problems by dening one code page for all scripts. Creating Unicode strings in Python is just as simple as creating normal strings:
>>> uHello World ! uHello World !
The small u in front of the quote indicates that an Unicode string is supposed to be created. If you want to include special characters in the string, you can do so by using the Python Unicode-Escape encoding. The following example shows how:

>>> a = [spam, eggs, 100, 1234] >>> a [spam, eggs, 100, 1234]
Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on:
>>> a[0] spam >>> a[3] 1234 >>> a[-2] 100 >>> a[1:-1] [eggs, 100] >>> a[:2] + [bacon, 2*2] [spam, eggs, bacon, 4] >>> 3*a[:3] + [Boe!] [spam, eggs, 100, spam, eggs, 100, spam, eggs, 100, Boe!]
Unlike strings, which are immutable, it is possible to change individual elements of a list:
>>> a [spam, eggs, 100, 1234] >>> a[2] = a[2] + 23 >>> a [spam, eggs, 123, 1234]
Assignment to slices is also possible, and this can even change the size of the list:
>>> # Replace some items:. a[0:2] = [1, 12] >>> a [1, 12, 123, 1234] >>> # Remove some:. a[0:2] = [] >>> a [123, 1234] >>> # Insert some:. a[1:1] = [bletch, xyzzy] >>> a [123, bletch, xyzzy, 1234] >>> a[:0] = a # Insert (a copy of) itself at the beginning >>> a [123, bletch, xyzzy, 1234, 123, bletch, xyzzy, 1234]
The built-in function len() also applies to lists:

>>> len(a) 8

It is possible to nest lists (create lists containing other lists), for example:
>>> >>> >>> 3 >>> [2, >>> 2 >>> >>> [1, >>> [2, q = [2, 3] p = [1, q, 4] len(p) p[1] 3] p[1][0] p[1].append(xtra) p [2, 3, xtra], 4] q 3, xtra] # See section 5.1
Note that in the last example, p[1] and q really refer to the same object! Well come back to object semantics later.
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 sub-sequence of the Fibonacci series as follows:
>>>.. >>>. 5 8
# Fibonacci series: # the sum of two elements defines the next a, b = 0, 1 while b < 10: print b a, b = b, a+b
This example introduces several new features. The rst line contains a multiple assignment: the variables a and b simultaneously get the new values 0 and 1. On the last line this is used again, demonstrating that the expressions on the right-hand side are all evaluated rst before any of the assignments take place. The right-hand side expressions are evaluated from the left to the right. The while loop executes as long as the condition (here: b < 10) remains true. In Python, like in C, any non-zero integer value is true; zero is false. The condition may also be a string or list value, in fact any sequence; anything with a non-zero length is true, empty sequences are false. The test used in the example is a simple comparison. The standard comparison operators are written the same as in C: < (less than), > (greater than), == (equal to), <= (less than or equal to), >= (greater than or equal to) and != (not equal to). The body of the loop is indented: indentation is Pythons way of grouping statements. Python does not (yet!) provide an intelligent input line editing facility, so you have to type a tab or space(s) for each indented line. In practice you will prepare more complicated input for Python with a text editor; most text editors have an auto-indent facility. When a compound statement is entered interactively, it must be followed by a blank line to indicate completion (since the parser cannot guess when you have typed the last line). Note that each line within a basic block must be indented by the same amount. The print statement writes the value of the expression(s) it is given. It differs from just writing the expression you want to write (as we did earlier in the calculator examples) in the way it handles multiple expressions and strings. Strings are printed without quotes, and a space is inserted between items, so you can format things nicely, like this:

>>> i = 256*256 >>> print The value of i is, i The value of i is 65536
A trailing comma avoids the newline after the output:
>>> a, b = 0, 1 >>> while b < 1000:. print b,. a, b = b, a+b. 610 987
Note that the interpreter inserts a newline before it prints the next prompt if the last line was not completed.
3.2. First Steps Towards Programming

More Control Flow Tools

Besides the while statement just introduced, Python knows the usual control ow statements known from other languages, with some twists.

if Statements

Perhaps the most well-known statement type is the if statement. For example:
>>> >>>. x = int(raw_input("Please enter an integer: ")) if x < 0: x = 0 print Negative changed to zero elif x == 0: print Zero elif x == 1: print Single else: print More
There can be zero or more elif parts, and the else part is optional. The keyword elif is short for else if, and is useful to avoid excessive indentation. An if. elif. elif. sequence is a substitute for the switch or case statements found in other languages.

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 giving the user the ability to dene both the iteration step and halting condition (as C), Pythons for statement iterates over the items of any sequence (a list or a string), in the order that they appear in the sequence. For example (no pun intended):
>>> # Measure some strings:. a = [cat, window, defenestrate] >>> for x in a:. print x, len(x). cat 3 window 6 defenestrate 12
It is not safe to modify the sequence being iterated over in the loop (this can only happen for mutable sequence
types, such as lists). If you need to modify the list you are iterating over (for example, to duplicate selected items) you must iterate over a copy. The slice notation makes this particularly convenient:
>>> for x in a[:]: # make a slice copy of the entire list. if len(x) > 6: a.insert(0, x). >>> a [defenestrate, cat, window, defenestrate]

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:
>>> range(10) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
The given end point is never part of the generated list; range(10) generates a list of 10 values, exactly the legal indices for items of a sequence of length 10. It is possible to let the range start at another number, or to specify a different increment (even negative; sometimes this is called the step):

>>> range(5, 10) [5, 6, 7, 8, 9] >>> range(0, 10, 3) [0, 3, 6, 9] >>> range(-10, -100, -30) [-10, -40, -70]
To iterate over the indices of a sequence, combine range() and len() as follows:
>>> a = [Mary, had, a, little, lamb] >>> for i in range(len(a)):. print i, a[i]. 0 Mary 1 had 2 a 3 little 4 lamb
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 an else clause; it is executed when the loop terminates through exhaustion of the list (with for) or when the condition becomes false (with while), but not when the loop is terminated by a break statement. This is exemplied by the following loop, which searches for prime numbers:
Chapter 4. More Control Flow Tools
>>> for n in range(2, 10):. for x in range(2, n):. if n % x == 0:. print n, equals, x, *, n/x. break. else:. # loop fell through without finding a factor. print n, is a prime number. 2 is a prime number 3 is a prime number 4 equals 2 * is a prime number 6 equals 2 * is a prime number 8 equals 2 * equals 3 * 3

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:
>>> while True:. pass # Busy-wait for keyboard interrupt.

Dening Functions

We can create a function that writes the Fibonacci series to an arbitrary boundary:
>>>. >>>. def fib(n): # write Fibonacci series up to n """Print a 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 defined: fib(2000) 1597
The keyword def introduces a function denition. It must be 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 must be indented. The rst statement of the function body can optionally be a string literal; this string literal is the functions documentation string, or docstring. There are tools which use docstrings to automatically produce online or printed documentation, or to let the user interactively browse through code; its good practice to include docstrings in code that you write, so try to make a habit of it. 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 function store the value in the local symbol table; whereas variable references

>>> [3, >>> >>> [3, range(3, 6) 4, 5] args = [3, 6] range(*args) 4, 5] # normal call with separate arguments
# call with arguments unpacked from a list

4.7.5 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. Heres a function that returns the sum of its two arguments: lambda a, b: a+b. Lambda forms can be used wherever function objects are required. They are syntactically restricted to a single expression. Semantically, they are just syntactic sugar for a normal function denition. Like nested function denitions, lambda forms can reference variables from the containing scope:
>>>.. >>> >>> 42 >>> 43 def make_incrementor(n): return lambda x: x + n f = make_incrementor(42) f(0) f(1)
4.7.6 Documentation Strings
There are emerging conventions about the content and formatting of documentation strings. The rst line should always be a short, concise summary of the objects purpose. For brevity, it should not explicitly state the objects name or type, since these are available by other means (except if the name happens to be a verb describing a functions operation). This line should begin with a capital letter and end with a period. If there are more lines in the documentation string, the second line should be blank, visually separating the summary from the rest of the description. The following lines should be one or more paragraphs describing the objects calling conventions, its side effects, etc. The Python parser does not strip indentation from multi-line string literals in Python, so tools that process documentation have to strip indentation if desired. This is done using the following convention. The rst non-blank line after the rst line of the string determines the amount of indentation for the entire documentation string. (We cant use the rst line since it is generally adjacent to the strings opening quotes so its indentation is not apparent in the string literal.) Whitespace equivalent to this indentation is then stripped from the start of all lines of the string. Lines that are indented less should not occur, but if they occur all their leading whitespace should be stripped. Equivalence of whitespace should be tested after expansion of tabs (to 8 spaces, normally). Here is an example of a multi-line docstring:
>>> def my_function():. """Do nothing, but document it.. No, really, it doesnt do anything. """. pass. >>> print my_function.__doc__ Do nothing, but document it. No, really, it doesnt do anything.

for arg in sys.argv[1:]: try: f = open(arg, r) except IOError: print cannot open, arg else: print arg, has, len(f.readlines()), lines f.close()
The use of the else clause is better than adding additional code to the try clause because it avoids accidentally catching an exception that wasnt raised by the code being protected by the try. except statement. When an exception occurs, it may have an associated value, also known as the exceptions argument. The presence and type of the argument depend on the exception type. The except clause may specify a variable after the exception name (or list). The variable is bound to an exception instance with the arguments stored in instance.args. For convenience, the exception instance denes __getitem__ and __str__ so the arguments can be accessed or printed directly without having to reference.args.
>>> try:. raise Exception(spam, eggs). except Exception, inst:. print type(inst) # the exception instance. print inst.args # arguments stored in.args. print inst # __str__ allows args to printed directly. x, y = inst # __getitem__ allows args to be unpacked directly. print x =, x. print y =, y. <type instance> (spam, eggs) (spam, eggs) x = spam y = eggs
If an exception has an argument, it is printed as the last part (detail) of the message for unhandled exceptions. Exception handlers dont just handle exceptions if they occur immediately in the try clause, but also if they occur inside functions that are called (even indirectly) in the try clause. For example:

8.3. Handling Exceptions

>>> def this_fails():. x = 1/0. >>> try:. this_fails(). except ZeroDivisionError, detail:. print Handling run-time error:, detail. Handling run-time error: integer division or modulo

Raising Exceptions

The raise statement allows the programmer to force a specied exception to occur. For example:
>>> raise NameError, HiThere Traceback (most recent call last): File "<stdin>", line 1, in ? NameError: HiThere
The rst argument to raise names the exception to be raised. The optional second argument species the exceptions argument. If you need to determine whether an exception was raised but dont intend to handle it, a simpler form of the raise statement allows you to re-raise the exception:
>>> try:. raise NameError, HiThere. except NameError:. print An exception flew by!. raise. An exception flew by! Traceback (most recent call last): File "<stdin>", line 2, in ? NameError: HiThere
8.5 User-dened Exceptions

Classes

Pythons 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 denition and user, but rather rely on the politeness of the user not to break into the denition. The most important features of classes are retained with full power, however: the class inheritance mechanism allows multiple base classes, a derived class can override any methods of its base class or classes, a method can call the method of a base class with the same name. Objects can contain an arbitrary amount of private data. In C++ terminology, all class members (including the data members) are public, and all member functions are virtual. There are no special constructors or destructors. As in Modula-3, there are no shorthands for referencing the objects members from its methods: the method function is declared with an explicit rst argument representing the object, which is provided implicitly by the call. As in Smalltalk, classes themselves are objects, albeit in the wider sense of the word: in Python, all data types are objects. This provides semantics for importing and renaming. Unlike C++ and Modula-3, built-in types can be used as base classes for extension by the user. Also, like in C++ but unlike in Modula-3, most built-in operators with special syntax (arithmetic operators, subscripting etc.) can be redened for class instances.

A Word About Terminology

Lacking universally accepted terminology to talk about classes, I will make occasional use of Smalltalk and C++ terms. (I would use Modula-3 terms, since its object-oriented semantics are closer to those of Python than C++, but I expect that few readers have heard of it.) I also have to warn you that theres a terminological pitfall for object-oriented readers: the word object in Python does not necessarily mean a class instance. Like C++ and Modula-3, and unlike Smalltalk, not all types in Python are classes: the basic built-in types like integers and lists are not, and even somewhat more exotic types like les arent. However, all Python types share a little bit of common semantics that is best described by using the word object. Objects have individuality, and multiple names (in multiple scopes) can be bound to the same object. This is known as aliasing in other languages. This is usually not appreciated on a rst glance at Python, and can be safely ignored when dealing with immutable basic types (numbers, strings, tuples). However, aliasing has an (intended!) effect on the semantics of Python code involving mutable objects such as lists, dictionaries, and most types representing entities outside the program (les, windows, etc.). This is usually used to the benet of the program, since aliases behave like pointers in some respects. For example, passing an object is cheap since only a pointer is passed by the implementation; and if a function modies an object passed as an argument, the caller will see the change this eliminates the need for two different argument passing mechanisms as in Pascal.

x = MyClass()

creates a new instance of the class and assigns this object to the local variable x.
9.3. A First Look at Classes
The instantiation operation (calling a class object) creates an empty object. Many classes like to create objects in a known initial state. Therefore a class may dene a special method named __init__(), like this:
def __init__(self): self.data = []
When a class denes an __init__() method, class instantiation automatically invokes __init__() for the newly-created class instance. So in this example, a new, initialized instance can be obtained by:
Of course, the __init__() method may have arguments for greater exibility. In that case, arguments given to the class instantiation operator are passed on to __init__(). For example,
>>> class Complex:. def __init__(self, realpart, imagpart):. self.r = realpart. self.i = imagpart. >>> x = Complex(3.0, -4.5) >>> x.r, x.i (3.0, -4.5)

9.3.3 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 rst Ill call data attributes. These correspond to instance variables in Smalltalk, and to data members in C++. Data attributes need not be declared; like local variables, they spring into existence when they are rst assigned to. For example, if x is the instance of MyClass created above, the following piece of code will print the value 16, without leaving a trace:
x.counter = 1 while x.counter < 10: x.counter = x.counter * 2 print x.counter del x.counter
The second kind of attribute references understood by instance objects are methods. A method is a function that belongs to an object. (In Python, the term method is not unique to class instances: other object types can have methods as well. For example, list objects have methods called append, insert, remove, sort, and so on. However, below, well use the term method exclusively to mean methods of class instance objects, unless explicitly stated otherwise.) Valid method names of an instance object depend on its class. By denition, all attributes of a class that are (user-dened) function objects dene corresponding methods of its instances. So in our example, x.f is a valid method reference, since MyClass.f is a function, but x.i is not, since MyClass.i is not. But x.f is not the same thing as MyClass.f it is a method object, not a function object.

Line Editing

If supported, input line editing is active whenever the interpreter prints a primary or secondary prompt. The current line can be edited using the conventional Emacs control characters. The most important of these are: C-A (Control-A) moves the cursor to the beginning of the line, C-E to the end, C-B moves it one position to the left, C-F to the right. Backspace erases the character to the left of the cursor, C-D the character to its right. C-K kills (erases) the rest of the line to the right of the cursor, C-Y yanks back the last killed string. C-underscore undoes the last change you made; it can be repeated for cumulative effect.

A.2 History Substitution

History substitution works as follows. All non-empty input lines issued are saved in a history buffer, and when a new prompt is given you are positioned on a new line at the bottom of this buffer. C-P moves one line up (back) in the history buffer, C-N moves one down. Any line in the history buffer can be edited; an asterisk appears in front of the prompt to mark a line as modied. Pressing the Return key passes the current line to the interpreter. C-R starts an incremental reverse search; C-S starts a forward search.

A.3 Key Bindings

The key bindings and some other parameters of the Readline library can be customized by placing commands in an initialization le called /.inputrc. Key bindings have the form

key-name: function-name

"string": function-name
and options can be set with

set option-name value

For example:
# I prefer vi-style editing: set editing-mode vi # Edit using a single line: set horizontal-scroll-mode On # Rebind some keys: Meta-h: backward-kill-word "\C-u": universal-argument "\C-x\C-r": re-read-init-file
Note that the default binding for Tab in Python is to insert a Tab character instead of Readlines default lename completion function. If you insist, you can override this by putting

Tab: complete

in your /.inputrc. (Of course, this makes it harder to type indented continuation lines if youre accustomed to using Tab for that purpose.) Automatic completion of variable and module names is optionally available. To enable it in the interpreters interactive mode, add the following to your startup le:1
import rlcompleter, readline readline.parse_and_bind(tab: complete)
This binds the Tab key to the completion function, so hitting the Tab key twice suggests completions; it looks at Python statement names, the current local variables, and the available module names. For dotted expressions such as string.a, it will evaluate the expression up to the nal. and then suggest completions from the attributes of the resulting object. Note that this may execute application-dened code if an object with a __getattr__() method is part of the expression. A more capable startup le might look like this example. Note that this deletes the names it creates once they are no longer needed; this is done since the startup le is executed in the same namespace as the interactive commands, and removing the names avoids creating side effects in the interactive environments. You may nd it convenient to keep some of the imported modules, such as os, which turn out to be needed in most sessions with the interpreter.

ACCEPT CWI LICENSE AGREEMENT FOR PYTHON 0.9.0 THROUGH 1.2 Copyright c 1991 - 1995, Stichting Mathematisch Centrum Amsterdam, The Netherlands. All rights reserved. Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the name of Stichting Mathematisch Centrum or CWI not be used in advertising or publicity pertaining to distribution of the software without specic, written prior permission. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Appendix C. History and License

Glossary

>>> The typical Python prompt of the interactive shell. Often seen for code examples that can be tried right away in the interpreter. The typical Python prompt of the interactive shell when entering code for an indented code block. BDFL Benevolent Dictator For Life, a.k.a. Guido van Rossum, Pythons creator. byte code The internal representation of a Python program in the interpreter. The byte code is also cached in the.pyc and.pyo les so that executing the same le is faster the second time (compilation from source to byte code can be saved). This intermediate language is said to run on a virtual machine that calls the subroutines corresponding to each bytecode. classic class Any class which does not inherit from object. See new-style class. coercion Converting data from one type to another. For example, int(3.15) coerces the oating point number to the integer, 3. Most mathematical operations have rules for coercing their arguments to a common type. For instance, adding 3+4.5, causes the integer 3 to be coerced to be a oat 3.0 before adding to 4.5 resulting in the oat 7.5. descriptor Any new-style object that denes the methods __get__(), __set__(), or __delete__(). When a class attribute is a descriptor, its special binding behavior is triggered upon attribute lookup. Normally, writing a.b looks up the object b in the class dictionary for a, but if b is a descriptor, the dened method gets called. Understanding descriptors is a key to a deep understanding of Python because they are the basis for many features including functions, methods, properties, class methods, static methods, and reference to super classes. dictionary An associative array, where arbitrary keys are mapped to values. The use of dict much resembles that for list, but the keys can be any object with a __hash__() function, not just integers starting from zero. Called a hash in Perl. EAFP Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style that is common in many other languages such as C. __future__ A pseudo module which programmers can use to enable new language features which are not compatible with the current interpreter. For example, the expression 11/4 currently evaluates to 2. If the module in which it is executed had enabled true division by executing:

Symbols

., 89 >, 89 __builtin__ (built-in module), 40 __future__, 89 __slots__, 91
IDLE, 90 immutable, 90 index() (list method), 27 insert() (list method), 27 integer division, 90 interactive, 90 interpreted, 90 iterable, 90 iterator, 90
append() (list method), 27

BDFL, 89 byte code, 89

LBYL, 91 list comprehension, 90
classic class, 89 coercion, 89 compileall (standard module), 39 count() (list method), 27
mapping, 90 metaclass, 91 method object, 60 module search path, 38 mutable, 91
descriptor, 89 dictionary, 89 docstrings, 21, 26 documentation strings, 21, 26
namespace, 91 nested scope, 91 new-style class, 91
EAFP, 89 environment variables PATH, 4, 38 PYTHONPATH, 3840 PYTHONSTARTUP, 5, 78 extend() (list method), 27
object le, 47 method, 60 open() (built-in function), 47
le object, 47 for statement, 19
PATH, 4, 38 path module search, 38 pickle (standard module), 49 pop() (list method), 27 Python3000, 91 PYTHONPATH, 3840 PYTHONSTARTUP, 5, 78
generator, 89 GIL, 90 global interpreter lock, 90
readline (built-in module), 78 remove() (list method), 27 reverse() (list method), 27 rlcompleter (standard module), 78
search path, module, 38 sequence, 91 sort() (list method), 27 statement for, 19 string (standard module), 45 strings, documentation, 21, 26 sys (standard module), 39
unicode() (built-in function), 14

Zen of Python, 91

doc1

A Quick, Painless Tutorial on the Python Language
Norman Matloff University of California, Davis c 2003-2010, N. Matloff April 8, 2010

Contents

1 Overview 1.1 1.2 What Are Scripting Languages?. Why Python?. 8 9
2 How to Use This Tutorial 2.1 2.2 2.3 Background Needed. Approach. What Parts to Read, When.
3 A 5-Minute Introductory Example 3.1 3.2 3.3 3.4 3.5 Example Program Code. Python Lists. Python Block Denition. Python Also Offers an Interactive Mode.
Python As a Calculator. 10 11
4 A 10-Minute Introductory Example 4.1 4.2 4.3
Example Program Code. 11 Command-Line Arguments. 12 Introduction to File Manipulation. 13 13
5 Declaration (or Not), Scope, Functions, Etc.

5.1 5.2

Lack of Declaration. 13 Locals Vs. Globals. 14
6 A Couple of Built-In Functions 7 Types of Variables/Values 7.1 7.2
String Versus Numerical Values. 14 Sequences. 15 7.2.1 7.2.2 7.2.3 7.2.4 Lists (Quasi-Arrays). 15 Tuples. 17 Strings. 17 Sorting. 18

7.3 7.4

Dictionaries (Hashes). 19 Function Denition. 21 23
8 Keyboard Input 9 Use of name 10 Object-Oriented Programming
10.1 Example Program Code. 24 10.2 The Keyword self. 24 10.3 Instance Variables. 24 10.4 Class Variables. 25 10.5 Constructors and Destructors. 25 10.6 Instance Methods. 25 10.7 Class Methods. 25 10.8 Derived Classes. 26 10.9 A Word on Class Implementation. Importance of Understanding Object References 12 Object Deletion 13 Object Comparison 28

14 Modules and Packages

14.1 Modules. 29 14.1.1 Example Program Code. 30 14.1.2 How import Works. 30 14.1.3 Compiled Code. 31 14.1.4 Miscellaneous. 31 14.1.5 A Note on Global Variables Within Modules. 31 14.2 Data Hiding. 32 14.3 Packages. Exception Handling (Not Just for Exceptions!) 16 Docstrings 17 Miscellaneous 35
17.1 Running Python Scripts Without Explicitly Invoking the Interpreter. 35 17.2 Named Arguments in Functions. 35 17.3 Printing Without a Newline or Blanks. 36 17.4 Formatted String Manipulation. Example of Data Structures in Python 37
18.1 Making Use of Python Idioms. Functional Programming Features 39
19.1 Lambda Functions. 39 19.2 Mapping. 40 19.3 Filtering. 41 19.4 List Comprehension. 42 19.5 Reduction. 42 A Debugging 42
A.1 Pythons Built-In Debugger, PDB. 42 A.1.1 The Basics. 43 A.1.2 Using PDB Macros. 45

A.1.3 Using dict

A.1.4 The type() Function. 46 A.2 Using PDB with Emacs. 46 A.3 Debugging with DDD. 48 A.3.1 Youll Need a Special PYDB. 48 A.3.2 DDD Launch and Program Loading. 49 A.3.3 Breakpoints. 49 A.3.4 Running Your Code. 49 A.3.5 Inspecting Variables. 49 A.3.6 Miscellaneous. 50 A.4 Debugging with Winpdb. 50 A.5 Debugging with Eclipse. 50 A.6 Some Python Internal Debugging Aids. 50 A.6.1 The dict Attribute. 51 A.6.2 The id() Function. 51 B Online Documentation 51
B.1 The dir() Function. 51 B.2 The help() Function. 52 B.3 PyDoc. 53 C Putting All Globals into a Class D Looking at the Python Virtual Machine 53 54

1 Overview

1.1 What Are Scripting Languages?
Languages like C and C++ allow a programmer to write code at a very detailed level which has good execution speed (especially in the case of C). But in most applications, execution speed is not important, and in many cases one would prefer to write at a higher level. For example, for text-manipulation applications, the basic unit in C/C++ is a character, while for languages like Perl and Python the basic units are lines of text and words within lines. One can work with lines and words in C/C++, but one must go to greater effort to accomplish the same thing. The term scripting language has never been formally dened, but here are the typical characteristics: Used often for system administration, Web programming, text processing, etc. Very casual with regard to typing of variables, e.g. little or no distinction between integer, oatingpoint or string variables. Arrays can mix elements of different types, such as integers and strings. Functions can return nonscalars, e.g. arrays. Nonscalars can be used as loop indexes. Etc. Lots of high-level operations intrinsic to the language, e.g. string concatenation and stack push/pop. Interpreted, rather than being compiled to the instruction set of the host machine.

Why Python?

The rst really popular scripting language was Perl. It is still in wide usage today, but the languages with momentum are Python and the Python-like Ruby. Many people, including me, greatly prefer Python to Perl, as it is much cleaner and more elegant. Python is very popular among the developers at Google. Advocates of Python, often called pythonistas, say that Python is so clear and so enjoyable to write in that one should use Python for all of ones programming work, not just for scripting work. They believe it is superior to C or C++.1 Personally, I believe that C++ is bloated and its pieces dont t together well; Java is nicer, but its strongly-typed nature is in my view a nuisance and an obstacle to clear programming. I was pleased to see that Eric Raymond, the prominent promoter of the open source movement, has also expressed the same views as mine regarding C++, Java and Python.
2 How to Use This Tutorial

2.1 Background Needed

Anyone with even a bit of programming experience should nd the material through Section 8 to be quite accessible. The material beginning with Section 10 will feel quite comfortable to anyone with background in an objectoriented programming (OOP) language such as C++ or Java. If you lack this background, you will still be
Again, an exception would be programs which really need fast execution speed.
able to read these sections, but will probably need to go through them more slowly than those who do know OOP; just focus on the examples, not the terminology. There will be a couple of places in which we describe things briey in a Unix context, so some Unix knowledge would be helpful, but it certainly is not required. Python is used on Windows and Macintosh platforms too, not just Unix.

Approach

Our approach here is different from that of most Python books, or even most Python Web tutorials. The usual approach is to painfully go over all details from the beginning. For example, the usual approach would be to state all possible forms that a Python integer can take on, and for that matter how many different command-line options one can launch Python with. I avoid this here. Again, the aim is to enable the reader to quickly acquire a Python foundation. He/she should then be able to delve directly into some special topic if and when the need arises.

What Parts to Read, When

I would suggest that you rst read through Section 8, and then give Python a bit of a try yourself. First experiment a bit in Pythons interactive mode (Section 3.4). Then try writing a few short programs yourself. These can be entirely new programs, or merely modications of the example programs presented below.2 This will give you a much more concrete feel of the language. If your main use of Python will be to write short scripts and you wont be using the Python library, this will probably be enough for you. However, most readers will need to go further, acquiring a basic knowledge of Pythons OOP features and Python modules/packages. So you should next read through Section 17. That would be a very solid foundation for you from which to make good use of Python. Eventually, you may start to notice that many Python programmers make use of Pythons functional programming features, and you may wish to understand what the others are doing or maybe use these features yourself. If so, Section 19 will get you started. Dont forget the appendices! The key ones are Sections A and B. I also have a number of tutorials on Python special programming, e.g. network programming, iterators/generators, etc. See http://heather.cs.ucdavis.edu/matloff/python.html.

There is no need for an analog of argc, though. Python, being an object-oriented language, treats lists as objects, The length of a list is thus incorporated into that object. So, if we need to know the number of elements in argv, we can get it via len(argv). 10 We would also use it like C/C++s oor(), in applications that need such an operation. 11 In C/C++, we could use atof() if it were available, or sscanf().
Introduction to File Manipulation
The function open() is similar to the one in C/C++. Our line

f = open(sys.argv[1])

created an object of le class, and assigned it to f. The readlines() function of the le class returns a list (keep in mind, list is an ofcial Python term) consisting of the lines in the le. Each line is a string, and that string is one element of the list. Since the le here consisted of ve lines, the value returned by calling readlines() is the ve-element list
[,This is an,example of a,text file,]
(Though not visible here, there is an end-of-line character in each string.)

5.1 Lack of Declaration

Variables are not declared in Python. A variable is created when the rst assignment to it is executed. For example, in the program tme.py above, the variable ines does not exist until the statement

flines = f.readlines()

is executed. By the way, a variable which has not been assigned a value yet has the value None (and this can be assigned to a variable, tested for in an if statement, etc.).

Locals Vs. Globals

Python does not really have global variables in the sense of C/C++, in which the scope of a variable is an entire program. We will discuss this further in Section 14.1.5, but for now assume our source code consists of just a single.py le; in that case, Python does have global variables pretty much like in C/C++. Python tries to infer the scope of a variable from its position in the code. If a function includes any code which assigns to a variable, then that variable is assumed to be local. So, in the code for checkline(), Python would assume that l and wordcount are local to checkline() if we dont inform it otherwise. We do the latter with the global keyword. Use of global variables simplies the presentation here, and I personally believe that the unctuous criticism of global variables is unwarranted. (See http://heather.cs.ucdavis.edu/matloff/ globals.html.) In fact, in one of the major types of programming, threads, use of globals is basically mandatory. You may wish, however, to at least group together all your globals into a class, as I do. See Appendix C. 13

6 A Couple of Built-In Functions
The function len() returns the number of elements in a list, in this case, the number of lines in the le (since readlines() returned a list in which each element consisted of one line of the le). The method split() is a member of the string class.12 It splits a string into a list of words, for example.13 So, for instance, in checkline() when l is This is an then the list w will be equal to [This,is,an]. (In the case of the rst line, which is blank, w will be equal to the empty list, [].)
7 Types of Variables/Values
As is typical in scripting languages, type in the sense of C/C++ int or oat is not declared in Python. However, the Python interpreter does internally keep track of the type of all objects. Thus Python variables dont have types, but their values do. In other words, a variable X might be bound to an integer at one point in your program and then be rebound to a class instance at another point. In other words, Python uses dynamic typing. Pythons types include notions of scalars, sequences (lists or tuples) and dictionaries (associative arrays, discussed in Sec. 7.3), classes, function, etc.
String Versus Numerical Values
Unlike Perl, Python does distinguish between numbers and their string representations. The functions eval() and str() can be used to convert back and forth. For example:
>>> 2 + 1.5 Traceback (most recent call last): File "<stdin>", line 1, in ? TypeError: unsupported operand type(s) for +: int and str >>> 2 + eval(1.5) 3.5 >>> str(2 + eval(1.5)) 3.5
There are also int() to convert from strings to integers, and oat(), to convert from strings to oating-point values:
>>> n = int(32) >>> n 32 >>> x = float(5.28) >>> x 5.2800000000000002

See also Section 17.4.

Member functions of classes are referred to as methods. The default is to use blank characters as the splitting criterion, but other characters or strings can be used.

Sequences

Lists are actually special cases of sequences, which are all array-like but with some differences. Note though, the commonalities; all of the following (some to be explained below) apply to any sequence type: the use of brackets to denote individual elements (e.g. x[i]) the built-in len() function to give the number of elements in the sequence14 slicing operations, i.e. the extraction of subsequences use of + and * operators for concatenation and replication 7.2.1 Lists (Quasi-Arrays) As stated earlier, lists are denoted by brackets and commas. For instance, the statement

ntfiles = 0 # count of number of textfile objects
above.18 Note that a class variable v of a class u is referred to as u.v within methods of the class and in code outside the class. For code inside the class but not within a method, it is referred to as simply v. Take a moment now to go through our example program above, and see examples of this with our ntles variable.
Constructors and Destructors
The constructor for a class must be named init(). The argument self is mandatory, and you can add others, as Ive done in this case with a lename. The destructor is del(). Note that it is only invoked when garbage collection is done, i.e. when all variables pointing to the object are gone.

Instance Methods

The method wordcount() is an instance method, i.e. it applies specically to the given object of this class. Again, in C++/Java terminology, this is a non-static method. Unlike C++ and Java, where this is an implicit argument to instance methods, Python wisely makes the relation explicit; the argument self is required.

Class Methods

Before Version 2.2, Python had no formal provision for class methods, i.e. methods which do not apply to specic objects of the class. Now Python has two (slightly differing) ways to do this, using the functions
By the way, though we placed that code at the beginning of the class, it could be at the end of the class, or between two methods, as long as it is not inside a method. In the latter situation ntles would be considered a local variable in the method, not what we want at all.
staticmethod() and classmethod(). I will present use of the former, in the following enhancement to the code in Section 10.1 within the class textle:
class textfile:. def totfiles(): print "the total number of text files is", textfile.ntfiles totfiles = staticmethod(totfiles). # here we are in "main". textfile.totfiles().
Note that class methods do not have the self argument. (Nor should they, right?) Note also that this method could be called even if there are not yet any instances of the class textle. In the example here, 0 would be printed out, since no les had yet been counted.19

Derived Classes

Inheritance is very much a part of the Python philosophy. A statement like

class b(a):

starts the denition of a subclass b of a class a. Multiple inheritance, etc. can also be done. Note that when the constructor for a derived class is called, the constructor for the base class is not automatically called. If you wish the latter constructor to be invoked, you must invoke it yourself, e.g.
class b(a): def __init__(self,xinit): # constructor for class b self.x = xinit # define and initialize an instance variable x a.__init__(self) # call base class constructor
The ofcial Python tutorial notes, [In the C++ sense] all methods in Python are effectively virtual. If you wish to extend, rather than override a method in the base class, you can refer to the latter by prepending the base name, as in our example a. init (self) above.
A Word on Class Implementation
A Python class instance is implemented internally as a dictionary. For example, in our program tfe.py above, the object b is implemented as a dictionary.
Note carefully that this is different from the Python value None. Even if we have not yet created instances of the class textle, the code ntles = 0 would still have been executed when we rst started execution of the program. As mentioned earlier, the Python interpreter executes the le from the rst line onward. When it reaches the line class textle: it then executes any free-standing code in the denition of the class.
Among other things, this means that you can add member variables to an instance of a class on the y, long after the instance is created. We are simply adding another key and value to the dictionary. In our main program, for example, we could have a statement like

b.name = zzz

11 Importance of Understanding Object References
A variable which has been assigned a mutable value is actually a pointer to the given object. For example, consider this code:
>>> >>> >>> >>> 5 >>> >>> >>> [1, >>> >>> [1, x = [1,2,3] # x is mutable y = x # x and y now both point to [1,2,3] x[2] = 5 # the mutable object pointed to by x now "mutes" y[2] # this means y[2] changes to 5 too! x = [1,2] y = x y 2] x = [3,4] y 2]
In the rst few lines, x and y are references to a list, a mutable object. The statement

Heres another example:

try: i = 5 y = x[i] except: print no such index:, i
But the Python idiom also uses this for code which is not acting in an exception context. Say for example we want to nd the index of the number 8 in the list z and if there is no such number, to rst add it to the list. We could do it this way:
try: place = x.index(8) except: x.append(8) place = len(x)
As seen above, you use try to check for an exception; you use raise to raise one.

16 Docstrings

There is a double-quoted string, nds the number of words in the le, at the beginning of wordcount(). This is called a docstring. It serves as a kind of comment, but at runtime, so that it can be used by debuggers and the like. Also, it enables users who have only the compiled form of the method, say as a commercial product, access to a comment. Here is an example of how to access it, using tf.py from above:
>>> import tf >>> tf.textfile.wordcount.__doc__ finds the number of words in the file
A docstring typically spans several lines. To create this kind of string, use triple quote marks. The method grep() is another instance method, this one with an argument besides self. By the way, method arguments in Python can only be pass-by-value, in the sense of C: Functions have side effects with respect to the parameter if the latter is a pointer. (Python does not have formal pointers, but it does have references; see Section 11.)
Note also that grep() makes use of one of Pythons many string operations, nd(). It searches for the argument string within the object string, returning the index of the rst occurrence of the argument string within the object string, or returning -1 if none is found.24

17 Miscellaneous

17.1 Running Python Scripts Without Explicitly Invoking the Interpreter
Say you have a Python script x.py. So far, we have discussed running it via the command25
But if you state the location of the Python interpreter in the rst line of x.py, e.g.

#! /usr/bin/python

and use the Unix chmod command to make x.py executable, then you can run x.py by merely typing

% x.py

This is necessary, for instance, if you are invoking the program from a Web page. Better yet, you can have Unix search your environment for the location of Python, by putting this as your rst line in x.py:

And here is a test:

# trybt1.py: test of bintree.py # usage: python trybt.py numbers_to_insert
Some C/C++ programmers might recognize the similarity to sprintf() from the C library. But did you get the pun?
import sys import bintree def main(): tr = bintree.tree() for n in sys.argv[1:]: tr.insrt(int(n)) tr.root.prnt() if __name__ == __main__: main()
The good thing about Python is that we can use the same code again for nonnumerical objects, as long as they are comparable. (Recall Section 13.) So, we can do the same thing with strings, using the tree and treenode classes AS IS, NO CHANGE, e.g.
# trybt2.py: # usage: test of bintree.py
python trybt.py strings_to_insert
import sys import bintree def main(): tr = bintree.tree() for s in sys.argv[1:]: tr.insrt(s) tr.root.prnt() if __name__ == __main__: main()
% python trybt2.py abc tuv abc tuv

Or even

# trybt3.py: import bintree def main(): tr = bintree.tree() tr.insrt([12,xyz]) tr.insrt([15,xyz]) tr.insrt([12,tuv]) tr.insrt([2,y]) tr.insrt([20,aaa]) tr.root.prnt() if __name__ == __main__: main() test of bintree.py
% python trybt3.py [2, y] [12, tuv] [12, xyz] [15, xyz] [20, aaa]
Making Use of Python Idioms
In the example in Section 10.1, it is worth calling special attention to the line

for f in [a,b]:

where a and b are objects of type textle. This illustrates the fact that the elements within a list do not have to be scalars.28 Much more importantly, it illustrates that really effective use of Python means staying away from classic C-style loops and expressions with array elements. This is what makes for much cleaner, clearer and elegant code. It is where Python really shines. You should almost never use C/C++ style for loopsi.e. where an index (say j), is tested against an upper bound (say j < 10), and incremented at the end of each iteration (say j++). Indeed, you can often avoid explicit loops, and should do so whenever possible. For example, the code
self.lines = self.fh.readlines() self.nlines = len(self.lines)
in that same program is much cleaner than what we would have in, say, C. In the latter, we would need to set up a loop, which would read in the le one line at a time, incrementing a variable nlines in each iteration of the loop.29 Another great way to avoid loops is to use Pythons functional programming features, described in Section 19. Making use of Python idioms is often referred to by the pythonistas as the pythonic way to do things.

30 Side note: Note again that if we had not assigned to z, the list [3,6,4] would have been printed out anyway. In interactive mode, any Python non-assignment statement prints the value of the result.
>>> >>> >>> [1,
x = [1,2,3] y = map(lambda z: z*z, x) y 4, 9]
The condition that a lambda functions body consist only of an expression is rather limiting, for instance not allowing if-then-else constructs. If you really wish to have the latter, you could use a workaround. For example, to implement something like

if u > 2: u = 5

we could work as follows:
>>> >>> >>> [1, x = [1,2,3] g = lambda u: (u > 2) * 5 + (u <= 2) * u map(g,x) 2, 5]
Clearly, this is not feasible except for simple situations. For more complex cases, we would use a nonlambda function. For example, here is a revised version of the program in Section 4.1:
import sys def checkline(l): global wordcount w = l.split() wordcount += len(w) wordcount = 0 f = open(sys.argv[1]) flines = f.readlines() linecount = len(flines) map(checkline,flines) # replaces the old for loop print linecount, wordcount
Note that l is now an argument to checkline(). Of course, this could be reduced even further, with the heart of the main program above being changed to
map(checkline,open(sys.argv[1]).readlines)
But this is getting pretty hard to read and debug.

Filtering

The lter() function works like map(), except that it culls out the sequence elements which satisfy a certain condition. The function which lter() is applied to must be boolean-valued, i.e. return the desired true or false value. For example:
>>> >>> >>> [5, x = [5,12,-2,13] y = filter(lambda z: z > 0, x) y 12, 13]
Again, this allows us to avoid writing a for loop and an if statement. 41

List Comprehension

This allows you to compactify a for loop which produces a list. For example:

>>> x = [(1,-1), (12,5), (8,16)] >>> y = [(v,u) for (u,v) in x] >>> y [(-1, 1), (5, 12), (16, 8)]
This is more compact than rst initializing y to [], then having a for loop in which we call y.append(). It gets even better when done in nested form. Say for instance we have a list of lists which we want to concatenate together, ignoring the rst element in each. Heres how we could do it using list comprehensions:
>>> y [[0, 2, 22], [1, 5, 12], [2, 3, 33]] >>> [a for b in y for a in b[1:]] [2, 22, 5, 12, 3, 33]

Reduction

The reduce() function is used for applying the sum or other arithmetic-like operation to a list. For example,
>>> x = reduce(lambda x,y: x+y, range(5)) >>> x 10
Here range(5) is of course [0,1,2,3,4]. What reduce() does is it rst adds the rst two elements of [0,1,2,3,4], i.e. with 0 playing the role of x and 1, playing the role of y. That gives a sum of 1. Then that sum, 1, plays the role of x and the next element of [0,1,2,3,4], 2, plays the role of y, yielding a sum of 3, etc. Eventually reduce() nishes its work and returns a value of 10. Once again, this allowed us to avoid a for loop, plus a statement in which we initialize x to 0 before the for loop.

A Debugging

Do NOT debug by simply adding and subtracting print statements. Use a debugging tool! If you are not a regular user of a debugging tool, then you are causing yourself unnecessary grief and wasted time; see my debugging slide show, at http://heather.cs.ucdavis.edu/matloff/debug.html.
Pythons Built-In Debugger, PDB
The built-in debugger for Python, PDB, is rather primitive, but its very important to understand how it works, for two reasons: PDB is used indirectly by more sophisticated debugging tools. A good knowledge of PDB will enhance your ability to use those other tools. 42
I will show you here how to increase PDBs usefulness even as a standalone debugger. I will present PDB below in a sequence of increasingly-useful forms: The basic form. The basic form enhanced by strategic use of macros. The basic form in conjunction with the Emacs text editor. The basic form in conjunction with the DDD GUI for debuggers. A.1.1 The Basics You should be able to nd PDB in the lib subdirectory of your Python package. On a Unix system, for example, that is probably in something like /usr/lib/python2.2. /usr/local/lib/python2.4, etc. To debug a script x.py, type
% /usr/lib/python2.2/pdb.py x.py
(If x.py had had command-line arguments, they would be placed after x.py on the command line.) Of course, since you will use PDB a lot, it would be better to make an alias for it. For example, under Unix in the C-shell:
alias pdb /usr/lib/python2.2/pdb.py
so that you can write more simply

% pdb x.py

Once you are in PDB, set your rst breakpoint, say at line 12:
You can make it conditional, e.g.

b 12, z > 5

Hit c (continue), which you will get you into x.py and then stop at the breakpoint. Then continue as usual, with the main operations being like those of GDB: b (break) to set a breakpoint tbreak to set a one-time breakpoint 43
ignore to specify that a certain breakpoint will be ignored the next k times, where k is specied in the command l (list) to list some lines of source code n (next) to step to the next line, not stopping in function code if the current line is a function call s (subroutine) same as n, except that the function is entered in the case of a call c (continue) to continue until the next break point w (where) to get a stack report u (up) to move up a level in the stack, e.g. to query a local variable there d (down) to move down a level in the stack r (return) continue execution until the current function returns j (jump) to jump to another line without the intervening code being executed h (help) to get (minimal) online help (e.g. h b to get help on the b command, and simply h to get a list of all commands); type h pdb to get a tutorial on PDB31 q (quit) to exit PDB Upon entering PDB, you will get its (Pdb) prompt. If you have a multi-le program, breakpoints can be specied in the form module name:line number. For instance, suppose your main module is x.py, and it imports y.py. You set a breakpoint at line 8 of the latter as follows:

(Pdb) b y:8

Note, though, that you cant do this until y has actually been imported by x.32 When you are running PDB, you are running Python in its interactive mode. Therefore, you can issue any Python command at the PDB prompt. You can set variables, call functions, etc. This can be highly useful. For example, although PDB includes the p command for printing out the values of variables and expressions, it usually isnt necessary. To see why, recall that whenever you run Python in interactive mode, simply typing the name of a variable or expression will result in printing it outexactly what p would have done, without typing the p. So, if x.py contains a variable ww and you run PDB, instead of typing

(Pdb) p ww

you can simply type
The tutorial is run through a pager. Hit the space bar to go to the next page, and the q key to quit. Note also that if the module is implemented in C, you of course will not be able to break there.

Later, when you make a change to your source code, again issue the command
Your breakpoints from the last run will be retained. Select Program | Run as usual to set your command-line arguments, and then run. (You may get a DDD: No Source) popup error window, but just click OK and ignore it.) A.3.3 Breakpoints To set a breakpoint, right-click somewhere in blank space on the line in your source le and choose Set Breakpoint (or Set Temporary Breakpoint or Continue to Until Here, as the case may be). A.3.4 Running Your Code To run, click on Program | Run, ll in your programs command line arguments if any in the Run with Arguments box, and click Run in that pop-up window. You will be asked to hit Continue, which you could do by clicking Program | Run |, but is more conveniently done by clicking Continue in the little command summary window. (But dont use Run there.) You can then click on Next, Step, Cont etc. The marker for the current execution line is shaped like an I, though rather faint when the mouse pointer is not in the source code section of the DDD window. By the way, do not refer to sys.argv in freestanding code within a class. When your program is rst loaded, any freestanding code will be executed, and since the command-line arguments wont have been loaded yet, so you will get an index out of range error. Avoid this by putting code involving sys.argv either inside a function in the class, or outside the class entirely. A.3.5 Inspecting Variables You can inspect the value of a variable by moving the mouse pointer to any instance of the variable in the source code window. 49
As mentioned in Section A.1.3, if o is an object of some class, then printing o. dict will print all the member variables of this object. In DDD, you can do this even more easily, as follows. Simply put that expression in a comment, e.g.

# o.__dict__

and then whenever you wish to inspect the member variables of o, simply move the mouse pointer to that expression in the comment! Make good use of DDDs feature which allows a variable to be displayed continuously. Simply right-click on any instance of the variable, and then choose Display. A.3.6 Miscellaneous DDD, developed originally for C/C++, is not always a perfect match to Python. But since what DDD actually does is relay your clicked commands to PDB, as you can see in DDDs Console, whatever DDD cant do for you, you can type PDB commands directly into the Console.

Debugging with Winpdb

The Winpdb debugger (www.digitalpeers.com/pythondebugger/),37 is very good. Among other things, it can be used to debug threaded code, curses-based code and so on, which many debuggers cant. Winpdb is a GUI front end to the text-based RPDB2, which is in the same package. I have a tutorial on both at http://heather.cs.ucdavis.edu/matloff/winpdb.html.

The help() Function

For example, lets nd out about the pop() method for lists:
>>> help(list.pop) Help on method_descriptor: pop(.) L.pop([index]) -> item -- remove and return item at index (default last) (END)
And the center() method for strings:
>>> help(.center) Help on function center: center(s, width) center(s, width) -> string Return a center version of s, in a field of the specified width. padded with spaces as needed. The string is never truncated.
Hit q to exit the help pager. You can also get information by using pydoc at the Unix command line, e.g.
% pydoc string.center [.same as above]
The above methods of obtaining help were for use in Pythons interactive mode. Outside of that mode, in an OS shell, you can get the same information from PyDoc. For example,

pydoc sys

will give you all the information about the sys module. For modules outside the ordinary Python distribution, make sure they are in your Python search path, and be sure show the dot sequence, e.g.

pydoc u.v

C Putting All Globals into a Class
As mentioned in Section 5, instead of using the keyword global, we may nd it clearer or more organized to group all our global variables into a class. Here, in the le tmeg.py, is how we would do this to modify the example in that section, tme.py:
# reads in the text file whose name is specified on the command line, # and reports the number of lines and words
import sys def checkline(): glb.linecount += 1 w = glb.l.split() glb.wordcount += len(w) class glb: linecount = 0 wordcount = 0 l = [] f = open(sys.argv[1]) for glb.l in f.readlines(): checkline() print glb.linecount, glb.wordcount
Note that when the program is rst loaded, the class glb will be executed, even before main() starts.
D Looking at the Python Virtual Machine
One can inspect the Python virtual machine code for a program. For the program srvr.py in http:// heather.cs.ucdavis.edu/matloff/Python/PyThreads.pdf, I once did the following: Running Python in interactive mode, I rst imported the module dis (disassembler). I then imported the program, by typing import srvr (I rst needed to add the usual if being imported.) I then ran
>>> dis.dis(srvr)
name == main code, so that the program wouldnt execute upon
How do you read the code? You can get a list of Python virtual machine instructions in Python: the Complete Reference, by Martin C. Brown, pub. by Osborne, 2001. But if you have background in assembly language, you can probably guess what the code is doing anyway.

 

Tags

NV-MX7 System DSC-T99D 42EP24S AR-5127 Simdriveline 1 Dimmer WM WS9160 VVX1005 CZ-21M203N 5620 SAM ZOB471X KP-53HS10 WD-12235FB 2300W Securependrive S09AW Sr0 Vivicam 3735 KDL-46S2000 HD7850 80 IC-M88 YB500 PV-V4022 NE-972 Navman F10 Prestige 650 Powershot A70 MS7000 Mkii GSA-4167B WD-14445FDS X5270 Gateway SKS-HT750 TI-5019 STR-DE635 HT-Z310T GNX3000 RPX400 Tablet PC HD8795-M SL3242FX-PRO TH55B HD MDX-C5300 KXT7730 Sabt200 IP2000 Centauri Cornwall III XVS650A-2004 BR-6104KP WV-7225 PRS-350 DSP-R793 735-ME Remote Nokia 110S NV-FJ620EU HDR-SR1 VGN-FZ11S Black Evo3 GC8210 TL753C Duo Trio GC2510 81920 Litepro 730 Lide 600F Des Rois Freestyle-2005 CT-500 Review Armada 7800 WV-BP70 TL-WN951N HQC281 CX-supervisor V3 I8910 D-NF421 GN 2100 Reflexes VGN-NS21e S Nokia 6161 V8 2GB SRU3030-10 TME-M760 CW600T Minitower PC Kodak M380 MS-283MC MP-C848 PVR 5910 Steamer KW-AVX730 SH-EX1200 Acer M900 Ide Raid EHM6235K MW302X IC-F121S 4 25 CBX-K1XG

 

manuel d'instructions, Guide de l'utilisateur | Manual de instrucciones, Instrucciones de uso | Bedienungsanleitung, Bedienungsanleitung | Manual de Instruções, guia do usuário | инструкция | návod na použitie, Užívateľská príručka, návod k použití | bruksanvisningen | instrukcja, podręcznik użytkownika | kullanım kılavuzu, Kullanım | kézikönyv, használati útmutató | manuale di istruzioni, istruzioni d'uso | handleiding, gebruikershandleiding

 

Sitemap

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101