Advanced python programming (slides)

Solo disponible en BuenasTareas
  • Páginas : 45 (11023 palabras )
  • Descarga(s) : 0
  • Publicado : 25 de enero de 2012
Leer documento completo
Vista previa del texto
Advanced Python Programming
David M. Beazley
Department of Computer Science University of Chicago
beazley@cs.uchicago.edu

O’Reilly Open Source Conference July 17, 2000

O’Reilly OSCON 2000, Advanced Python Programming, Slide 1 July 17, 2000, beazley@cs.uchicago.edu

Overview
Advanced Programming Topics in Python
A brief introduction to Python Working with the filesystem. Operatingsystem interfaces Programming with Threads Network programming Database interfaces Restricted execution Extensions in C.

This is primarily a tour of the Python library
Everything covered is part of the standard Python distribution. Goal is to highlight many of Python’s capabilities.

O’Reilly OSCON 2000, Advanced Python Programming, Slide 2 July 17, 2000, beazley@cs.uchicago.edu Preliminaries
Audience
Experienced programmers who are familiar with advanced programming topics in other languages. Python programmers who want to know more. Programmers who aren’t afraid of gory details.

Disclaimer
This tutorial is aimed at an advanced audience I assume prior knowledge of topics in Operating Systems and Networks. Prior experience with Python won’t hurt as well.

My Background
Iwas drawn to Python as a C programmer. Primary interest is using Python as an interpreted interface to C programs. Wrote the "Python Essential Reference" in 1999 (New Riders Publishing). All of the material presented here can be found in that source.

O’Reilly OSCON 2000, Advanced Python Programming, Slide 3 July 17, 2000, beazley@cs.uchicago.edu

A Very Brief Tour of Python

O’Reilly OSCON2000, Advanced Python Programming, Slide 4 July 17, 2000, beazley@cs.uchicago.edu

Starting and Stopping Python
Unix
unix % python Python 1.5.2 (#1, Sep 19 1999, 16:29:25) [GCC 2.7.2.3] on linux2 Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam >>>

On Windows and Macintosh
Python is launched as an application. An interpreter window will appear and you will see the prompt.Program Termination
Programs run until EOF is reached. Type Control-D or Control-Z at the interactive prompt. Or type
raise SystemExit

O’Reilly OSCON 2000, Advanced Python Programming, Slide 5 July 17, 2000, beazley@cs.uchicago.edu

Your First Program
Hello World
>>> print "Hello World" Hello World >>>

Putting it in a file
# hello.py print "Hello World"

Running a file
unix %python hello.py

Or you can use the familiar #! trick
#!/usr/local/bin/python print "Hello World"

O’Reilly OSCON 2000, Advanced Python Programming, Slide 6 July 17, 2000, beazley@cs.uchicago.edu

Variables and Expressions
Expressions
Standard mathematical operators work like other languages:
3 + 5 3 + (5*4) 3 ** 2 ’Hello’ + ’World’

Variable assignment
a b c a = = = = 4 = a print if not(b print and b c): "b is still between a and c"

O’Reilly OSCON 2000, Advanced Python Programming, Slide 9 July 17, 2000, beazley@cs.uchicago.edu

Basic Types (Numbers and Strings)
Numbers
a b c d = = = = 3 4.5 517288833333L 4 + 3j # # # # Integer Floating point Long integer (arbitrary precision) Complex (imaginary) number

Strings
a = ’Hello’ # Single quotes b = "World" # Doublequotes c = "Bob said ’hey there.’" # A mix of both d = ’’’A triple quoted string can span multiple lines like this’’’ e = """Also works for double quotes"""

O’Reilly OSCON 2000, Advanced Python Programming, Slide 10 July 17, 2000, beazley@cs.uchicago.edu

Basic Types (Lists)
Lists of Arbitrary Objects
a b c d e = = = = = [2, [2, [] [2, a + 3, 4] 7, 3.5, "Hello"] [a,b]] b # # # # # A list ofintegers A mixed list An empty list A list containing a list Join two lists

List Manipulation
x = a[1] y = b[1:3] z = d[1][0][2] b[0] = 42 # # # # Get 2nd element (0 is first) Return a sublist Nested lists Change an element

O’Reilly OSCON 2000, Advanced Python Programming, Slide 11 July 17, 2000, beazley@cs.uchicago.edu

Basic Types (Tuples)
Tuples
f = (2,3,4,5) g = (,) h = (2, [3,4],...
tracking img