Difference between revisions of "Python"

From Christoph's Personal Wiki
Jump to: navigation, search
(See also)
Line 16: Line 16:
  
 
==Reserved words (native to Python)==
 
==Reserved words (native to Python)==
  and elif global or
+
  and       elif     global   or
  assert else if pass
+
  assert     else     if       pass
  break except import print
+
  break     except   import   print
  class exec in raise
+
  class     exec     in       raise
  continue finally is return
+
  continue   finally   is       return
  def for lambda try
+
  def       for       lambda   try
  del from not while
+
  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)
  
 
==See also==
 
==See also==

Revision as of 22:55, 6 November 2008

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: NumPy for tips on using this module.

Version history

  • 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)

See also

Graphics

Google

Bioinformatics

External links

Online books/tutorials

Techniques

Bioinformatics