| ||||||||
| Python presentation by Phil Jensen | [ Up ] |
(Notes for a presentation by Phil Jensen at the COLUG meeting of 10/27/1999.)
Editor's note: Phil since moved on to work at Google on the West Coast; we are proud to count him as a COLUG alumnus.
For much more information, very well-organized:
<http://www.python.org>
"Python is an interpreted, interactive, object-oriented programming language. It incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes. Python combines remarkable power with very clear syntax. It has interfaces to many system calls and libraries, as well as to various window systems, and is extensible in C or C++. It is also usable as an extension language for applications that need a programmable interface."
Integers, 'long' (infinite-precision) integers, floating-point, complex, strings, tuples, lists, dictionaries. No declarations. Everything is an object. Assignment is object assignment. Strings, tuples, and lists are 'sequences', i.e., they can be indexed and sliced. A character is a string of length 1. Strings and tuples are 'immutable'. Automatic memory management (in CPython, currently by reference-counting).
Roughly like C.
Logical operators are spelled and,
or, and not.
Bitwise operators and shifts bind tighter than comparisons
(C got this wrong).
"0 < i < n" has the meaning usual in mathematics.
is and is not test object identity.
in and not in test sequence membership.
% doubles as sprintf.
Assignment (=) is not an expression operator,
and there are no shortcut assignments (like +=).
Newline is a statement separator, as in the shell. If you want to combine simple statements on one line, you can use a semicolon. Compound statements in Python look like this:
while expr:
<statement-list>
The colon is required. Indentation must be consistent.
If <statement-list> is simple,
the construct can be written
on one line,
e.g.,
if not symtab.has_key(s): symtab[s] = entry
While nested inside (...), [...],
or {...},
lines may be continued 'for free' and indentation is disregarded.
Expressions (i.e., procedure call). Assignment.
Tuple assignment. Element and slice assignment.
print.
del.
exec.
continue, break,
pass, return.
raise, assert.
global.
if <expr>: elif <expr>: else: <stmts>
while <expr>: <stmts>
for <var> in <sequence>: <stmts>
while and for statements may also
have an else part,
executed if the loop was exited normally
(without hitting a break).
The built-in range function allows simulation of counter-based
for loops.
try: <stmts> except <exception>: <stmts>
try: <stmts> finally: <stmts>
def some_function(a, b, c):
<stmts>
Function definition is effectively an assignment.
Variables assigned to within a function are local unless
mentioned in a global statement (the one exception
to the 'no declarations' rule :-).
class some_class:
def __init__(self, x, y):
<statements to set up an instance of 'some_class'>
<other method definitions>
Assignments (both = and def)
within a class definition
create attributes at the class level.
Calling the class generates
an instance and calls the __init__ function (if provided).
Attributes, both class-level and instance-level, are named with
qualification (object.attribute).
A class may inherit from one or more superclasses.
By defining special method names like __add__ and
__getattr__, a class may
be made to respond to built-in operators.
Gain access to other modules with the import
statement. A compiled version of the module (.pyo file) will
be used if it is up-to-date. The module runs the first time it is
loaded. Names within module are accessed as
module.name.
open.
map, filter,
reduce.
max, min.
chr, ord.
type, id.
And important built-in methods like append,
keys, and sort.
Hundreds exist, see the library documentation at
the Python web site.
string, re,
os, shutil,
random, urllib, gzip.
|
Copyright
© 2000 The Central Ohio Linux User Group.
All rights reserved. |
||
| Search |