Difference between revisions of "Python"
(→Version history) |
(→See also) |
||
Line 123: | Line 123: | ||
*[http://www.pythonxy.com/foreword.php python(x,y)] — a free scientific and engineering development software for numerical computations, data analysis and data visualization | *[http://www.pythonxy.com/foreword.php python(x,y)] — a free scientific and engineering development software for numerical computations, data analysis and data visualization | ||
*[http://www.sagemath.org/ SAGE: Open Source Mathematics Software] | *[http://www.sagemath.org/ SAGE: Open Source Mathematics Software] | ||
+ | ===Multimedia=== | ||
+ | *[http://pymedia.org/ PyMedia] — module for wav, mp3, ogg, avi, divx, dvd, cdda, etc. file manipulations. | ||
===Google=== | ===Google=== | ||
*[http://pygooglechart.slowchop.com/ Python Google Chart] ([http://slowchop.com/2008/04/26/python-google-chart-020/ maps]) | *[http://pygooglechart.slowchop.com/ Python Google Chart] ([http://slowchop.com/2008/04/26/python-google-chart-020/ maps]) |
Revision as of 23:14, 10 March 2009
Python is an interpreted programming language created by Guido van Rossum in 1990. Python is fully dynamically typed and uses automatic memory management; it is thus similar to Perl, Ruby, Scheme, Smalltalk, and Tcl. Python is developed as an open source project, managed by the non-profit Python Software Foundation.
see: scripts for examples and NumPy for tips on using this module.
Contents
Version history
- 3.x
- Python 3.0.1 — 2009-02-13
- 2.x
- Python 2.6.1 — 2008-12-04
- Python 2.5.4 — 2008-12-23
- Python 2.5.2 — 2008-02-22
- Python 2.5.1 — 2007-04-18
- Python 2.4.4 — 2006-10-18
- Python 2.4.2 — 2005-09-28
Properties
- Python is strongly typed (i.e. types are enforced);
- dynamically, implicitly typed (i.e. you don't have to declare variables);
- case sensitive (i.e. var and VAR are two different variables); and
- object-oriented (i.e. everything is an object).
Reserved words (native to Python)
and elif global or assert else if pass break except import print class exec in raise continue finally is return def for lambda try del from not while
Array slicing
If you have a list
nums = [1, 3, 5, 7, 8, 13, 20]
then the first 3 elements, middle 3 elements, and last 3 elements would be:
nums[:3] #equals [1, 3, 5] nums[2:5] #equals [5, 7, 8] nums[-3:] #equals [8, 13, 20]
Note that Python allows negative list indices. The index -1 represents the last element, -2 the penultimate element, etc. Python also has more advanced slicing operators using the double colon (::) index operator. For example, the code:
nums[3::] #equals [7, 8, 13, 20] (starting at index 3 going to the end) nums[::3] #equals [1, 7, 20] (starting at index 0 and getting every third element afterward) nums[1::2] #equals [3, 7, 13] (starting at index 1 and getting every second element afterward)
Here document
Python supports heredocs delimited by single or double quotes repeated three times (i.e. '''
or """
).
A simple example with variable interpolation is:
sender = 'Buffy the Vampire Slayer' recipient = 'Spike' print("""\ Dear %(recipient)s, I wish you to leave Sunnydale and never return. Not Quite Love, %(sender)s """ % locals())
The Template
class described in PEP 292 (Simpler String Substitutions) provides similar functionality for variable interpolation and may be used in combination with the Python triple-quotes syntax.
Variadic functions
Python supports very flexible variadic functions. By marking variables with one asterisk (e.g. *var
) the given variable is defined to be a tuple of all the extra arguments. By marking variables with two asterisks (e.g. **var
) the given variable is a dictionary of all extra keyword arguments; the keys are strings, which are the names that were. Conventionally these are called "args" and "kwargs" respectively, but they may be something else, and packages often make good use of this ability to improve readability (e.g. BeautifulSoup). If they exist, these arguments must be the last one in the list.
def f(*args, **kwargs): print args print kwargs >>> f(1, 2, "cow", "kitty") (1, 2, "cow", "kitty") {} >>> f(arg1=1, sample=2, name="cow", hero="kitty") () {"arg1": 1, "sample": 2, "name": "cow", "hero": "kitty"} >>> f(1, 2, name="cow", hero="kitty") (1, 2) {"name": "cow", "hero": "kitty"} >>> f(arg1=1, sample=2, name="cow", "kitty") SyntaxError "Non-keyword arg after keyword arg"
Conversely you may also pass in a tuple or dictionary using the same asterisk-notation and have it automatically expand to fill.
def g(a, b, c): print a, b, c >>> mytuple = 1,2,3 >>> mydict = {"a": "first", "b": "second", "c": "third"} >>> g(*mytuple) 1 2 3 >>> g(**mydict) first second third >>> g(**{"a": "first"}) TypeError "g() takes exactly 3 non-keyword arguments (got 1)" >>> g(**{"a": "first", "b": "second", "c": "third", "d": "fourth"}) TypeError "g() got an unexpected keyword argument 'd'"
See also
- pypi - Python Package Index
- SciPy
- NumPy
- Parallel Python ("pp" module)
- TurboGears
- PyEphem — an astronomy library for Python
- urlgrabber
- Pyparsing (with Pyparsing introduction: BNF to code and wikipedia:Backus–Naur form)
- Beautiful Soup — a Python HTML/XML parser designed for quick turnaround projects like screen-scraping.
- pyRXP — an XML parser
- Pexpect — expect for Python
- Smug — a wiki built with git as the backend (also using Python and Djanjo)
- gitshelve — using git as a versioned data store in Python
- PADS — a library of Python Algorithms and Data Structures implemented by David Eppstein of the University of California, Irvine.
Graphics
- Python Imaging Library (PIL)
- Matplotlib — a python 2D plotting library which produces publication quality figures
- python(x,y) — a free scientific and engineering development software for numerical computations, data analysis and data visualization
- SAGE: Open Source Mathematics Software
Multimedia
- PyMedia — module for wav, mp3, ogg, avi, divx, dvd, cdda, etc. file manipulations.
- Python Google Chart (maps)
- SymPy — a Python library for symbolic mathematics.
Bioinformatics
- BioPython
- PySAT: Python Seqeuence Analysis Tools
- Noah Hoffman code
- Blast Parser — by Arne Muller
- Leighton Pritchard code
- PISE
- NodeBox: Graph
External links
- Python.org — Official site
- PythonInfo Wiki
- Perl/Python Phrasebook
- Rosetta Code
- Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)
- Python 2.5 Quick Reference
- Byte of Python
- pydoc.org: Python Documentation Online
- PerformancePython
- MPI for Python
- Pyxer — a Python AJAX Server/ Framework
- Debugging in Python
- wikipedia:python
Online books/tutorials
- Building Skills in Python: A Programmer's Introduction to Python — by Steven F. Lott (2007)
- Beginning Python for Bioinformatics
- Python course in Bioinformatics
- Learn Python in 10 minutes — from Poromenos's Stuff
- Dive Into Python — Python from novice to pro — download entire eBook.
- Python 101 cheat sheet
- Python HOWTOs