Difference between revisions of "Python"

From Christoph's Personal Wiki
Jump to: navigation, search
(Variadic functions)
 
(48 intermediate revisions by the same user not shown)
Line 1: Line 1:
'''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. Python 2.4.2 was released on 28 September 2005.
+
'''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.
  
== Properties ==
+
see: The [[Python/tutorial|tutorial]] and [[Python/scripts|scripts]] for examples and [[Python/NumPy|NumPy]] for tips on using this module.
  
* Python is strongly typed (i.e. types are enforced);
+
==Properties==
* dynamically, implicitly typed (i.e. you don't have to declare variables);
+
*Python is strongly typed (i.e. types are enforced);
* case sensitive (i.e. <tt>var</tt> and <tt>VAR</tt> are two different variables); and
+
*dynamically, implicitly typed (i.e. you don't have to declare variables);
* object-oriented (i.e. everything is an object).
+
*case sensitive (i.e. <tt>var</tt> and <tt>VAR</tt> are two different variables); and
 +
*object-oriented (i.e. everything is an object).
  
== Reserved words (native to Python) ==
+
==Reserved words / keywords (native to Python)==
and elif global or
+
''Note: Keywords define the language’s rules and structure, and they cannot be used as variable names.''
assert else if pass
+
break except import print
+
class exec in raise
+
continue finally is return
+
def for lambda try
+
del from not while
+
  
== External links ==
+
and      as    assert  break  class  continue
* [http://www.python.org/ Python.org] &mdash; Official site
+
def      del    elif    else    except  exec
* [http://wiki.python.org/moin/ PythonInfo Wiki]
+
finally  for    from    global  if      import
* [http://en.wikipedia.org/wiki/Python_programming_language Wikipedia article on '''Python''']
+
in      is    lambda  not    or      pass
* [http://www.poromenos.org/tutorials/python Learn Python in 10 minutes] &mdash; from Poromenos's Stuff
+
print    raise  return  try    while  with
* [http://www.diveintopython.org/ Dive Into Python &mdash; Python from novice to pro] &mdash; download entire eBook.
+
yield
* [http://www-128.ibm.com/developerworks/library/l-cheatsheet3.html Python 101 cheat sheet]
+
 
 +
==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. <code><nowiki>'''</nowiki></code> or <code>"""</code>).
 +
 
 +
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 <code>Template</code> class described in [http://www.python.org/dev/peps/pep-0292/ 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. <code>*var</code>) the given variable is defined to be a tuple of all the extra arguments. By marking variables with two asterisks (e.g. <code>**var</code>) 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. [http://www.crummy.com/software/BeautifulSoup/documentation.html 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'"
 +
 
 +
==The Zen of Python==
 +
 
 +
This is a poem written by Tim Peters named "The Zen of Python", which can be read by importing the <code>this</code> module in the interpreter:
 +
<pre>
 +
import this
 +
------------------------------------------------
 +
The Zen of Python, by Tim Peters
 +
Beautiful is better than ugly.
 +
Explicit is better than implicit.
 +
Simple is better than complex.
 +
Complex is better than complicated.
 +
Flat is better than nested.
 +
Sparse is better than dense.
 +
Readability counts.
 +
Special cases aren't special enough to break the rules.
 +
Although practicality beats purity.
 +
Errors should never pass silently.
 +
Unless explicitly silenced.
 +
In the face of ambiguity, refuse the temptation to guess.
 +
There should be one-- and preferably only one --obvious way to do it.
 +
Although that way may not be obvious at first unless you're Dutch.
 +
Now is better than never.
 +
Although never is often better than *right* now.
 +
If the implementation is hard to explain, it's a bad idea.
 +
If the implementation is easy to explain, it may be a good idea.
 +
Namespaces are one honking great idea -- let's do more of those!
 +
</pre>
 +
 
 +
==Version history==
 +
*'''3.x'''
 +
**Python 3.2.3 &mdash; 2012-04-10
 +
**Python 3.0.1 &mdash; 2009-02-13
 +
*'''2.x'''
 +
**Python 2.7.3 &mdash; 2012-04-09
 +
**Python 2.6.1 &mdash; 2008-12-04
 +
**Python 2.5.4 &mdash; 2008-12-23
 +
**Python 2.5.2 &mdash; 2008-02-22
 +
**Python 2.5.1 &mdash; 2007-04-18
 +
**Python 2.4.4 &mdash; 2006-10-18
 +
**Python 2.4.2 &mdash; 2005-09-28
 +
 
 +
==See also==
 +
*[http://pypi.python.org/pypi/pep8 pep8] and [https://github.com/GreenSteam/pep257/ pep257]
 +
*[http://pypi.python.org/pypi pypi - Python Package Index]
 +
 
 +
===Science / mathematics===
 +
*[http://www.scipy.org/ SciPy]
 +
*[http://numpy.scipy.org/ NumPy]
 +
*[http://www.sympy.org sympy] &mdash; a Python library for symbolic mathematics (aka a computer algebra system (CAS))
 +
*[http://pandas.pydata.org/ pandas] &mdash; a Python Data Analysis Library
 +
 
 +
===Graphics===
 +
*[http://www.pythonware.com/products/pil/ Python Imaging Library (PIL)]
 +
*[http://matplotlib.sourceforge.net/ Matplotlib] &mdash; a python 2D plotting library which produces publication quality figures
 +
*[http://www.pythonxy.com/foreword.php python(x,y)] &mdash; a free scientific and engineering development software for numerical computations, data analysis and data visualization
 +
*[http://www.sagemath.org/ SAGE: Open Source Mathematics Software]
 +
 
 +
===Multimedia===
 +
*[http://pymedia.org/ PyMedia] &mdash; module for wav, mp3, ogg, avi, divx, dvd, cdda, etc. file manipulations.
 +
*[http://id3-py.sourceforge.net/ ID3] &mdash; ID3 tagging in Python
 +
 
 +
===Google===
 +
*[http://pygooglechart.slowchop.com/ Python Google Chart] ([http://slowchop.com/2008/04/26/python-google-chart-020/ maps])
 +
*[http://code.google.com/p/sympy/ SymPy] &mdash; a Python library for symbolic mathematics.
 +
 
 +
===Bioinformatics===
 +
*[http://www.biopython.org BioPython]
 +
*[http://www.embl-heidelberg.de/~chenna/PySAT/ PySAT: Python Seqeuence Analysis Tools]
 +
*[http://www.unc.edu/~nghoffma/software.html Noah Hoffman code]
 +
*[http://www.sbg.bio.ic.ac.uk/~mueller/ Blast Parser] &mdash; by Arne Muller
 +
*[http://bioinf.scri.ac.uk/lp/programs.php Leighton Pritchard code]
 +
*[http://www.pasteur.fr/recherche/unites/sis/Pise/ PISE]
 +
*[http://nodebox.net/code/index.php/Graph NodeBox: Graph]
 +
 
 +
===Miscellaneous===
 +
*[http://www.parallelpython.com/ Parallel Python] ("pp" module)
 +
*[http://www.turbogears.org/ TurboGears]
 +
*[http://rhodesmill.org/pyephem/ PyEphem] &mdash; an astronomy library for Python
 +
*[http://linux.duke.edu/projects/urlgrabber/ urlgrabber]
 +
*[http://pyparsing.wikispaces.com/ Pyparsing] (with [http://eikke.com/pyparsing-introduction-bnf-to-code/ Pyparsing introduction: BNF to code] and [[wikipedia:Backus–Naur form]])
 +
*[http://www.crummy.com/software/BeautifulSoup/ Beautiful Soup] &mdash; a Python HTML/XML parser designed for quick turnaround projects like screen-scraping.
 +
*[http://www.reportlab.org/pyrxp.html pyRXP] &mdash; an XML parser
 +
*[http://www.noah.org/wiki/Pexpect Pexpect] &mdash; [[expect]] for Python
 +
*[http://www.mcnabbs.org/andrew/smug/ Smug] &mdash; a wiki built with [[git]] as the backend (also using Python and [http://djangoproject.com/ Djanjo])
 +
*[http://www.newartisans.com/blog_files/git.versioned.data.store.php#unique-entry-id-69 gitshelve] &mdash; using [[git]] as a versioned data store in Python
 +
*[http://www.ics.uci.edu/~eppstein/PADS/ PADS] &mdash; a library of Python Algorithms and Data Structures implemented by David Eppstein of the University of California, Irvine.
 +
 
 +
==External links==
 +
*[http://www.python.org/ Python.org] &mdash; Official site
 +
*[http://wiki.python.org/moin/ PythonInfo Wiki]
 +
*[http://wiki.python.org/moin/PerlPhrasebook Perl/Python Phrasebook]
 +
*[http://www.rosettacode.org/wiki/Main_Page Rosetta Code]
 +
*[http://www.perlmonks.org/?node_id=632023 Yet Another Rosetta Code Problem (Perl, Ruby, Python, Haskell, ...)]
 +
*[http://rgruet.free.fr/PQR25/PQR2.5.html Python 2.5 Quick Reference]
 +
*[http://swaroopch.info/text/Byte_of_Python:Main_Page Byte of Python]
 +
*[http://pydoc.org/ pydoc.org: Python Documentation Online]
 +
*[http://www.scipy.org/PerformancePython PerformancePython]
 +
*[http://mpi4py.scipy.org/ MPI for Python]
 +
*[http://www.pyxer.net/ Pyxer] &mdash; a Python AJAX Server/ Framework
 +
*[http://www.ferg.org/papers/debugging_in_python.html Debugging in Python]
 +
*[[wikipedia:python]]
 +
*[http://python3statement.org/ Moving to require Python 3]
 +
 
 +
===Online books/tutorials===
 +
*"[http://learnpythonthehardway.org/book/ Learn Python the Hard Way]"
 +
*[https://docs.python.org/2/tutorial/ Official Python Online Tutorial]
 +
*[https://wiki.python.org/moin/BeginnersGuide/NonProgrammers Python for Non-Programmers]
 +
*[http://homepage.mac.com/s_lott/books/python/htmlchunks/index.html Building Skills in Python: A Programmer's Introduction to Python] &mdash; by Steven F. Lott (2007)
 +
*[http://python.genedrift.org/ Beginning Python for Bioinformatics]
 +
*[http://www.pasteur.fr/recherche/unites/sis/formation/python/index.html Python course in Bioinformatics]
 +
*[http://www.poromenos.org/tutorials/python Learn Python in 10 minutes] &mdash; from Poromenos's Stuff
 +
*[http://www.diveintopython.org/ Dive Into Python &mdash; Python from novice to pro] &mdash; download entire eBook.
 +
*[http://www-128.ibm.com/developerworks/library/l-cheatsheet3.html Python 101 cheat sheet]
 +
*[http://www.amk.ca/python/howto/ Python HOWTOs]
 +
 
 +
===Techniques===
 +
*[http://www.mediawiki.org/wiki/Manual:Pywikipediabot Using the python wikipediabot]
 +
*[http://www.blog.pythonlibrary.org/?p=34 wxPython: Embedding an image in your title bar]
 +
====Bioinformatics====
 +
*[http://www2.warwick.ac.uk/fac/sci/moac/currentstudents/peter_cock/python/ramachandran/top500/ Calculating phi/psi angles for the "Top 500" PDB files]
 +
*[http://www2.warwick.ac.uk/fac/sci/moac/currentstudents/peter_cock/python/heatmap/ Using Python (and R) to draw a Heatmap from Microarray Data]
  
 
[[Category:Scripting languages]]
 
[[Category:Scripting languages]]

Latest revision as of 20:06, 1 June 2021

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: The tutorial and scripts for examples and NumPy for tips on using this module.

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 / keywords (native to Python)

Note: Keywords define the language’s rules and structure, and they cannot be used as variable names.

and      as     assert  break   class   continue
def      del    elif    else    except  exec
finally  for    from    global  if      import
in       is     lambda  not     or      pass
print    raise  return  try     while   with
yield

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'"

The Zen of Python

This is a poem written by Tim Peters named "The Zen of Python", which can be read by importing the this module in the interpreter:

import this
------------------------------------------------
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!

Version history

  • 3.x
    • Python 3.2.3 — 2012-04-10
    • Python 3.0.1 — 2009-02-13
  • 2.x
    • Python 2.7.3 — 2012-04-09
    • 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

See also

Science / mathematics

  • SciPy
  • NumPy
  • sympy — a Python library for symbolic mathematics (aka a computer algebra system (CAS))
  • pandas — a Python Data Analysis Library

Graphics

Multimedia

  • PyMedia — module for wav, mp3, ogg, avi, divx, dvd, cdda, etc. file manipulations.
  • ID3 — ID3 tagging in Python

Google

Bioinformatics

Miscellaneous

External links

Online books/tutorials

Techniques

Bioinformatics