Python (programming language)

From Citizendium
Revision as of 11:47, 30 August 2007 by imported>Aleksander Stos (→‎Files: more to come)
Jump to navigation Jump to search

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 published by Guido van Rossum in 1991, and to this day, Guido remains the project leader.

Python reached version 1.0 in 1994.

Examples

Hello World

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

print 'Hello World'

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

python hello.py

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, which make parts of a standard Python distribution).

Calculator

Python interpreter invoked from the command line gives an easy access to a scientific calculator. At prompt (denoted here by >>> ) one types

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

to get 8 in the result. Division of integers returns integer result, so

>>> 7/2

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

>>> 7.0/2

More interesting functions may be found in the math module. This is how to use it.

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

Files

A useful Python idiom is related to working with files. Line-by-line Perl-like file processing can be realized as in the following simple word count script. Comments, as always in Python, 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 the idiom
  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

Notice the indentation that indicates what code is executed within the loop. When the end of file is reached and the loop exits, 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

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.[1]

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.

See also

  • IDLE, the Integrated Development Environment for Python

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. 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.