Python (programming language): Difference between revisions

From Citizendium
Jump to navigation Jump to search
imported>Subpagination Bot
m (Add {{subpages}} and remove any categories (details))
imported>Andrew Yates
m (grammar cleanup, links to: interpreted programming languages, operand, C)
Line 1: Line 1:
{{subpages}}
{{subpages}}


'''Python''' is a dynamic object-oriented, general purpose interpreted programming language which runs on many different computer platforms and mobile devices. Python is [[open source software]] and is published under an OSI-approved license. Python aims to be a language that is efficient, coherent, readable, and fun to use. Because Python is an interpreted language, Python programs run immediately without the need for lengthy compile and link steps.
'''Python''' is a dynamic object-oriented, general purpose [[Programming_language#Compiled_vs._interpreted|interpreted programming language]] which runs on many different computer platforms and mobile devices. Python is [[open source software]] and is published under an OSI-approved license. Python aims to be a language that is efficient, coherent, readable, and fun to use. Because Python is an interpreted language, Python programs run immediately without the need for lengthy compile and link steps.


==History==
==History==
Line 28: Line 28:


===Hello World===
===Hello World===
The code for the "hello world" program can hardly be simpler:
The code for the "hello world" program can hardly be more simple:


<code> print 'Hello World'</code>
<code> print 'Hello World'</code>


This can be put in a file "hello.py", say, and executed with
This can be put in a file "hello.py", for example, and executed with


<code> python hello.py </code>
<code> python hello.py </code>


from the usual command line of the operating system. Alternatively, the code can be typed directly in an interactive Python environment (Python command line interpreter or [[IDLE (Python programming language)|IDLE]], which make part of a standard Python distribution).
from your operating system's command line. Alternatively, the code can be typed directly into an interactive Python environment (the Python command line interpreter or [[IDLE (Python programming language)|IDLE]], both of which are included in the standard Python distribution).


===Calculator===
===Calculator===
Python interpreter invoked from the command line gives an easy access to a scientific calculator. At prompt (denoted here by <code> >>> </code>) one types
The Python interpreter can be invoked from the command line and used as a scientific calculator. At the prompt (denoted here by <code> >>> </code>), type


<code> >>> 2+3*(1+1)</code>
<code> >>> 2+3*(1+1)</code>


to get 8 in the result. Division of integers returns integer result, so
the interpreter will print 8. Division of integers returns an integer result, so


<code> >>> 7/2</code>
<code> >>> 7/2</code>


is 3 ([[floor]] from the exact result). If a [[real number|real]] result is needed, then at least one ingredient should be 'real', as below
is 3 ([[floor]] from the exact result). If a [[real number|real]] result is needed, then at least one [[operand]] should be a real number, as shown below


<code> >>> 7.0/2</code>
<code> >>> 7.0/2</code>


More interesting functions may be found in the <tt>math</tt> ''module''. This is how to use it.
More interesting functions can be found in the <tt>math</tt> ''module''. It must be imported before it can be used.


<code> >>> from math import *</code>  
<code> >>> from math import *</code>  
Line 57: Line 57:
<code> >>> print sin(pi/2)</code>
<code> >>> print sin(pi/2)</code>


High quality graphs may be obtained with <tt>matplotlib</tt> library<ref>This is installed separately, see an [http://matplotlib.sourceforge.net/tutorial.html introduction] and a couple of [http://matplotlib.sourceforge.net/screenshots.html examples].</ref>
High quality graphs may be obtained with the <tt>matplotlib</tt> library<ref>This is installed separately, see its [http://matplotlib.sourceforge.net/tutorial.html introduction] and a couple of [http://matplotlib.sourceforge.net/screenshots.html examples].</ref>


===Files===
===Files===
Line 72: Line 72:
</pre>
</pre>


Notice the indentation that indicates what code is executed within the loop. This is the standard Python notation, a part of the syntax. When the end of file is reached, the loop terminates and the results are printed by the last line of the code. Note also that the variable <code>line</code> contains the terminating ''newline'' character, so that 1 is subtracted from its length for the character counting. Another interesting observation can be made: the method (i.e. function) of splitting a string is 'provided' by this very string. Indeed, Python is an objective language, the string variables are objects and related methods are its attributes.
The indentation indicates what code is executed within the loop. This is part of Python's syntax. When the end of file is reached, the loop terminates and the results are printed by the last line of the code. Note also that the variable <code>line</code> contains the terminating ''newline'' character, so that 1 is subtracted from its length for the character counting. Another interesting observation can be made: the method (i.e. function) of splitting a string is 'provided' by this very string. Indeed, Python is an objective language; the string variables are objects and related methods are its attributes.


===Internet access===
===Internet access===
Line 87: Line 87:


==Syntax==
==Syntax==
Remarkable global features of the Python syntax include high readability of the code, which is not independent of the use of indentation to separate blocks of code and a general "one statement per line" principle.<ref>Backslash "\" at the end of line allows to break e.g. a long assignment over multiple lines. There is also a formal possibility to put more than one statement in a line by separating them with a semicolon. Still, the general principle shapes the code.</ref>
Remarkable global features of the Python syntax include high readability of the code, which is aided by the use of indentation to separate blocks of code and a general "one statement per line" principle.<ref>Backslash "\" at the end of line allows to break e.g. a long assignment over multiple lines. There is also a formal possibility to put more than one statement in a line by separating them with a semicolon. Still, the general principle shapes the code.</ref>


==Implementations==
==Implementations==
Python's official distribution is known as CPython. It's written in C and functions as a virtual machine interpreting bytecode-compiled Python programs. Jython is an implementation for the [[Java Virtual Machine]], which can run either standalone (like CPython) or as an embedded scripting engine. IronPython is an implementation for the [[Common Language Runtime]] (.NET and Mono). PyPy is an implementation written in Python that targets several backends, including C, LLVM, JavaScript, JVM and CLR.
Python's official distribution is known as CPython. It's written in [[C_programming_language|C]] and functions as a virtual machine for interpreting bytecode-compiled Python programs. Jython is an implementation for the [[Java Virtual Machine]], which can run either standalone (like CPython) or as an embedded scripting engine. IronPython is an implementation for the [[Common Language Runtime]] (.NET and Mono). PyPy is an implementation written in Python that targets several backends, including C, LLVM, JavaScript, JVM and CLR.


==Python IDEs==
==Python IDEs==

Revision as of 18:03, 13 November 2007

This article is developing and not approved.
Main Article
Discussion
Related Articles  [?]
Bibliography  [?]
External Links  [?]
Citable Version  [?]
 
This editable Main Article is under development and subject to a disclaimer.

Python is a dynamic object-oriented, general purpose interpreted programming language which runs on many different computer platforms and mobile devices. Python is open source software and is published under an OSI-approved license. Python aims to be a language that is efficient, coherent, readable, and fun to use. Because Python is an interpreted language, Python programs run immediately without the need for lengthy compile and link steps.

History

Python was first published by the Dutch computer scientist (and applied mathematician) Guido van Rossum in pre-release (early-adopter) form in 1991, and to this day he remains the project leader and final arbiter of Python Enhancement Proposals (PEPs).

Python 2.5.1 is the current production release, and is very stable.

Python's major (standard) releases were:

  • Python 2.5.1 (April 2007)
  • Python 2.4.4 (October 2006)
  • Python 2.3.6 (November 2006)
  • Python 2.2.3 (May 2003)
  • Python 2.1.3 (April 2002)
  • Python 2.0.1 (June 2001)
  • Python 1.6.1 (September 2000)
  • Python 1.5.2 (April 1999)
  • Python 1.4 (October 1996)
  • Python 1.3 (October 1995)
  • Python 1.2 (April 1995)
  • Python 1.1 (1995)
  • Python 1.0 (1994)

Currently under development is a full refactoring for Python (version 3.0), which is known as Python 3000 (Py3k), and which is now in its early alpha (pre-production) stage.

Examples

Hello World

The code for the "hello world" program can hardly be more simple:

print 'Hello World'

This can be put in a file "hello.py", for example, and executed with

python hello.py

from your operating system's command line. Alternatively, the code can be typed directly into an interactive Python environment (the Python command line interpreter or IDLE, both of which are included in the standard Python distribution).

Calculator

The Python interpreter can be invoked from the command line and used as a scientific calculator. At the prompt (denoted here by >>> ), type

>>> 2+3*(1+1)

the interpreter will print 8. Division of integers returns an integer result, so

>>> 7/2

is 3 (floor from the exact result). If a real result is needed, then at least one operand should be a real number, as shown below

>>> 7.0/2

More interesting functions can be found in the math module. It must be imported before it can be used.

>>> from math import *
>>> print sin(pi/2)

High quality graphs may be obtained with the matplotlib library[1]

Files

A useful Python construction is related to working with files. Line-by-line Perl-like file processing can be realized with a standard for loop as in the following simple word count script. Below, as always in Python, comments start with the hash '#' sign and continue to the end of line.

(char_count,word_count,line_count)=0,0,0      # multiple assignment is OK
file = open ('myfile.txt')                    # standard opening of the file
for line in file:                             # this is "idiomatic" use of of the "for" loop
    wordlist = line.split()                       # splitting line into the list of words
    word_count += len(wordlist)                   # counting words
    line_count += 1                               # counting lines
    char_count += len(line) - 1                   # counting characters
print line_count, word_count, char_count      # we're done

The indentation indicates what code is executed within the loop. This is part of Python's syntax. When the end of file is reached, the loop terminates and the results are printed by the last line of the code. Note also that the variable line contains the terminating newline character, so that 1 is subtracted from its length for the character counting. Another interesting observation can be made: the method (i.e. function) of splitting a string is 'provided' by this very string. Indeed, Python is an objective language; the string variables are objects and related methods are its attributes.

Internet access

The following script counts the images on the Citizendium Main Page.

import urllib2                      
cnt=0
for line in urllib2.urlopen('http://en.citizendium.org/wiki/Main_Page'):
  cnt += line.count('<img src')
print cnt

First, we import a module from the standard library. The latter is said to be rich and often regarded as Python's strong point. In fact, Python official pages declare a "batteries included" philosophy. Now, the source HTML files of web pages can be treated much like the local files, e.g. processed line-by-line with a for loop. In the above example the variable line is a string containing a piece of HTML code of Citizendium's Main Page. Embedded images are inserted in this code with a text that begins with '<img src'. So it's enough to count instances of this last string. The appropriate method count() is at hand in the variable line.

Syntax

Remarkable global features of the Python syntax include high readability of the code, which is aided by the use of indentation to separate blocks of code and a general "one statement per line" principle.[2]

Implementations

Python's official distribution is known as CPython. It's written in C and functions as a virtual machine for interpreting bytecode-compiled Python programs. Jython is an implementation for the Java Virtual Machine, which can run either standalone (like CPython) or as an embedded scripting engine. IronPython is an implementation for the Common Language Runtime (.NET and Mono). PyPy is an implementation written in Python that targets several backends, including C, LLVM, JavaScript, JVM and CLR.

Python IDEs

Python can be supported as the programming language in an integrated development environment (IDE).

Popular Python IDEs include the following:

  • IDLE, the default Integrated Development Environment for Python
  • Eclipse pydev, the pydev plug-in to the Eclipse IDE for Python
  • ActiveState Komodo, the multiple scripting languages IDE
  • Wingware Wing, the multiplatform Python-language-specific IDE

Books

  • Beazley, David M. Python Essential Reference, 3rd Ed. Sams, 2006 ISBN 0672328623
  • Chun, Wesley J. Core Python Programming, 2nd Ed. Prentice Hall, 2006 ISBN 0132269937
  • Goerzen, John. Foundations Of Python Network Programming Apress, 2006 ISBN 1590593715
  • Hetland, Magnus Lie. Beginning Python: From Novice To Professional Apress, 2005 ISBN 159059519X
  • Lutz, Mark. Programming Python, 3rd Ed. O'Reilly, 2006 ISBN 0596009259
  • Lutz, Mark and Ascher, David. Learning Python, 2nd Ed. O'Reilly, 2003 ISBN 0596002815
  • Martelli, Alex. Python In A Nutshell, 2nd Ed. O'Reilly, 2006 ISBN 0596001886
  • Martelli, Alex. Ravenscroft, Anna. Ascher, David. Python Cookbook, 2nd Ed. O'Reilly, 2005 ISBN 0596007973
  • Zelle, John M. Python Programming: An Introduction To Computer Science Franklin Beedle, 2003 ISBN 1887902996

External links

Notes and references

  1. This is installed separately, see its introduction and a couple of examples.
  2. Backslash "\" at the end of line allows to break e.g. a long assignment over multiple lines. There is also a formal possibility to put more than one statement in a line by separating them with a semicolon. Still, the general principle shapes the code.