Python (programming language)

Python Official Python Logo

Paradigm          multi-paradigm: object-oriented, imperative, functional, reflective

Appeared in       1991

Designed by      Guido van Rossum

Developer          Python Software Foundation

License             Python Software Foundation License

Usual file extensions      .py, .pyw, .pyc, .pyo, .pyd

Website            www.python.org

Python is a general-purpose high-level programming language whose design philosophy emphasizes code readability. Python aims to combine “remarkable power with very clear syntax”, and its standard library is large and comprehensive. Its use of indentation for block delimiters is unusual among popular programming languages.

Python supports multiple programming paradigms, primarily but not limited to object oriented, imperative and, to a lesser extent, functional programming styles. It features a fully dynamic type system and automatic memory management, similar to that of Scheme, Ruby, Perl, and Tcl. Like other dynamic languages, Python is often used as a scripting language, but is also used in a wide range of non-scripting contexts.

The reference implementation of Python (CPython) is free and open source software and has a community-based development model, as do all or nearly all of its alternative implementations. CPython is managed by the non-profit Python Software Foundation.

History

Python was conceived in the late 1980s and its implementation was started in December 1989 by Guido van Rossum at CWI in the Netherlands as a successor to the ABC programming language (itself inspired by SETL) capable of exception handling and interfacing with the Amoeba operating system.

Python 2.0 was released on 16 October 2000, with many major new features including a full garbage collector and support for Unicode. However, the most important change was to the development process itself, with a shift to a more transparent and community-backed process. Python 3.0, a major, backwards-incompatible release, was released on 3 December 2008 after a long period of testing.

Programming philosophy

Python is a multi-paradigm programming language. Rather than forcing programmers to adopt a particular style of programming, it permits several styles: object-oriented programming and structured programming are fully supported, and there are a number of language features which support functional programming and aspect-oriented programming (including by metaprogramming and by magic methods). Many other paradigms are supported using extensions, such as pyDBC and Contracts for Python which allow Design by Contract.

Python uses dynamic typing and a combination of reference counting and a cycle-detecting garbage collector for memory management. An important feature of Python is dynamic name resolution (late binding), which binds method and variable names during program execution.

Rather than requiring all desired functionality to be built into the language’s core, Python was designed to be highly extensible. New built-in modules can be easily written in C, C++ or Cython. Python can also be used as an extension language for existing modules and applications that need a programmable interface.

The design of Python offers only limited support for functional programming in the Lisp tradition. However, Python’s design philosophy exhibits significant similarities to those of minimalist Lisp-family languages, such as Scheme[citation needed]. The library has two modules (itertools and functools) that implement proven functional tools borrowed from Haskell and Standard ML.

Python’s developers premature optimization, and reject patches to non-critical parts of CPython which would offer a marginal increase in speed at the cost of clarity. Python is sometimes described as “slow

Usage

Main article: Python software

Python is often used as a scripting language for web applications, e.g. via mod_python for the Apache web server. With Web Server Gateway Interface a standard API has been developed to facilitate these applications. Web application frameworks or application servers like Django, Pylons, TurboGears, web2py and Zope support developers in the design and maintenance of complex applications. Libraries like NumPy, Scipy and Matplotlib allow Python to be used effectively in scientific computing.

Python has been successfully embedded in a number of software products as a scripting language, including in finite element method software such as Abaqus, 3D animation packages such as Maya, MotionBuilder, Softimage, Cinema 4D, BodyPaint 3D, modo, and Blender, and 2D imaging programs like GIMP . ESRI is now promoting Python as the best choice for writing scripts in ArcGIS. It has even been used in several videogames.

For many operating systems, Python is a standard component; it ships with most Linux distributions, with NetBSD, and OpenBSD, and with Mac OS X and can be used from the terminal. Ubuntu uses the Ubiquity installer, while Red Hat Linux and Fedora use the Anaconda installer, and both installers are written in Python. Gentoo Linux uses Python in its package management system, Portage, and the standard tool to access it, emerge. Pardus uses it for administration and during system boot.

Python has also seen extensive use in the information security industry, including exploit development.

Among the users of Python are YouTube and the original BitTorrent client. Large organizations that make use of Python include Google, Yahoo!,CERN, NASA, and ITA. Most of the Sugar software for the One Laptop Per Child XO, now developed at Sugar Labs, is written in Python.

Syntax and semantics

Syntax-highlighted Python 2.x code.

Python was intended to be a highly readable language. It is designed to have an uncluttered visual layout, frequently using English keywords where other languages use punctuation. Python requires less boilerplate than traditional manifestly typed structured languages such as C or Pascal, and has a smaller number of syntactic exceptions and special cases than either of these.

Indentation

Python uses whitespace indentation, rather than curly braces or keywords, to delimit blocks (a feature also known as the off-side rule). An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block.

Statements and control flow

Python’s statements include:

* The if statement, which conditionally executes a block of code, along with else and elif (a contraction of else-if).

* The for statement, which iterates over an iterable object, capturing each element to a local variable for use by the attached block.

* The while statement, which executes a block of code as long as its condition is true.

* The try statement, which allows exceptions raised in its attached code block to be caught and handled by except clauses; it also ensures that clean-up code in a finally block will always be run regardless of how the block exits.

* The class statement, which executes a block of code and attaches its local namespace to a class, for use in object-oriented programming.

* The def statement, which defines a function or method.

* The with statement, which encloses a code block within a context manager (for example, acquiring a lock before the block of code is run, and releasing the lock afterwards).

* The pass statement, which serves as a NOP and can be used in place of a code block.

Each statement has its own semantics: for example, the def statement does not execute its block immediately, unlike most other statements.

Methods

Methods on objects are functions attached to the object’s class; the syntax instance.method(argument) is, for normal methods and functions, syntactic sugar for Class.method(instance, argument). Python methods have an explicit self parameter to access instance data, in contrast to the implicit self in some other object-oriented programming languages (for example, Java, C++ or Ruby).

Typing

Python uses duck typing and has typed objects but untyped variable names. Type constraints are not checked at compile time; rather, operations on an object may fail, signifying that the given object is not of a suitable type. Despite being dynamically typed, Python is strongly typed, forbidding operations that are not well-defined (for example, adding a number to a string) rather than silently attempting to make sense of them.

Python allows programmers to define their own types using classes, which are most often used for object-oriented programming. New instances of classes are constructed by calling the class (for example, SpamClass() or EggsClass()), and the classes themselves are instances of the metaclass type (itself an instance of itself), allowing metaprogramming and reflection.

Here is a summary of Python’s built-in types:

Type     Description        Syntax example

str        An immutable sequence of Unicode characters     ‘Wikipedia’ “Wikipedia”" “”Spanning multiple lines”"”

bytes    An immutable sequence of bytes             b’Some ASCII’ b”Some ASCII”

list        Mutable, can contain mixed types           [4.0, 'string', True]

tuple     Immutable, can contain mixed types        (4.0, ‘string’, True)

set, frozenset    Unordered, contains no duplicates           {4.0, ‘string’, True} frozenset([4.0, 'string', True])

dict       A mutable group of key and value pairs    {‘key1′: 1.0, 3: False}

int         An immutable fixed precision number of unlimited magnitude         42

float      An immutable floating point number (system-defined precision)      3.1415927

complex            An immutable complex number with real number and imaginary parts         3+2.7j

bool      An immutable truth value            True False

long      An immutable fixed precision number of unlimited magnitude         10L

While many programming languages round the result of integer division towards zero, Python always rounds it down towards minus infinity; so that 7//3 is 2, but (−7)//3 is −3.

Implementations

CPython

The mainstream Python implementation, known as CPython, is written in C meeting the C89 standard. CPython compiles Python programs into intermediate bytecode, which are then executed by the virtual machine. It is distributed with a large standard library written in a mixture of C and Python. CPython ships in versions for many platforms, including Microsoft Windows and most modern Unix-like systems. CPython was intended from almost its very conception to be cross-platform; its use and development on esoteric platforms such as Amoeba, alongside more conventional ones like Unix and Mac OS, has greatly helped in this regard.

Alternative implementations

Jython compiles the Python program into Java byte code, which can then be executed by every Java Virtual Machine implementation. This also enables the use of Java class library functions from the Python program. IronPython follows a similar approach in order to run Python programs on the .NET Common Language Runtime. PyPy is an experimental self-hosting implementation of Python, written in Python, that can output several types of bytecode, object code and intermediate languages. PyPy is of this type, compiling RPython to several languages; other examples include Pyjamas compiling to Javascript; Shed Skin compiling to C++; and Cython & Pyrex compiling to C.

In 2005 Nokia released a Python interpreter for the Series 60 mobile phones called PyS60. It includes many of the modules from the CPython implementations and some additional modules for integration with the Symbian operating system.

Interpretational semantics

Most Python implementations (including CPython, the primary implementation) can function as a command line interpreter, for which the user enters statements sequentially and receives the results immediately. In short, Python acts as a shell. While the semantics of the other modes of execution (bytecode compilation, or compilation to native code) preserve the sequential semantics, they offer a speed boost at the cost of interactivity, so they are usually only used outside of a command-line interaction (e.g., when importing a module).

Other shells add capabilities beyond those in the basic interpreter, including IDLE and IPython. While generally following the visual style of the Python shell, they implement features like auto-completion, retention of session state, and syntax highlighting.

Standard library.

Python has a large standard library, commonly cited as one of Python’s greatest strengths,providing pre-written tools suited to many tasks. This is deliberate and has been described as a “batteries included” Python philosophy. The modules of the standard library can be augmented with custom modules written in either C or Python. Recently, Boost C++ Libraries includes a library, Boost.Python, to enable interoperability between C++ and Python. Because of the wide variety of tools provided by the standard library, combined with the ability to use a lower-level language such as C and C++, which is already capable of interfacing between other libraries, Python can be a powerful glue language between languages and tools.

The standard library is particularly well tailored to writing Internet-facing applications, with a large number of standard formats and protocols (such as MIME and HTTP) already supported. Modules for creating graphical user interfaces, connecting to relational databases, arithmetic with arbitrary precision decimals, manipulating regular expressions, and doing unit testing are also included.

Influences on other languages

Python’s design and philosophy have influenced several programming languages, including:

* Pyrex and its derivative Cython are code translators that are targeted at writing fast C extensions for the CPython interpreter. The language is mostly Python with syntax extensions for C and C++ features. Both languages produce compilable C code as output.

* Boo uses indentation, a similar syntax, and a similar object model. However, Boo uses static typing and is closely integrated with the .NET framework.

* Cobra uses indentation and a similar syntax. Cobra’s “Acknowledgements” document lists Python first among languages that influenced it. However, Cobra directly supports design-by-contract, unit tests and optional static typing.

* Groovy was motivated by the desire to bring the Python design philosophy to Java.

Python’s development practices have also been emulated by other languages.