Python (Programming Language) - Wikipedia
Python (Programming Language) - Wikipedia
Guido van Rossum began working on Python in the late 1980s as Paradigm Multi-paradigm:
a successor to the ABC programming language and first released object-oriented,[1]
it in 1991 as Python 0.9.0.[35] Python 2.0 was released in 2000. procedural
Python 3.0, released in 2008, was a major revision not completely (imperative),
backward-compatible with earlier versions. Python 2.7.18, functional,
released in 2020, was the last release of Python 2.[36] structured,
reflective
Python consistently ranks as one of the most popular
Designed by Guido van
programming languages.[37][38][39][40]
Rossum
Python was conceived in the late 1980s[41] by Guido van Rossum First appeared 20 February
at Centrum Wiskunde & Informatica (CWI) in the Netherlands as 1991[2]
a successor to the ABC programming language, which was
Stable release 3.12.0[3] / 2
inspired by SETL,[42] capable of exception handling and
October 2023
interfacing with the Amoeba operating system.[11] Its
implementation began in December 1989.[43] Van Rossum Typing Duck, dynamic,
shouldered sole responsibility for the project, as the lead discipline strong typing;[4]
developer, until 12 July 2018, when he announced his gradual (since 3.5,
"permanent vacation" from his responsibilities as Python's but ignored in
"benevolent dictator for life", a title the Python community CPython)[5]
bestowed upon him to reflect his long-term commitment as the
OS Windows, macOS,
project's chief decision-maker.[44] In January 2019, active Python
Linux/UNIX,
core developers elected a five-member Steering Council to lead
Android[6][7] and a
the project.[45][46]
few other
Python 2.0 was released on 16 October 2000, with many major platforms[8]
new features such as list comprehensions, cycle-detecting garbage License Python Software
collection, reference counting, and Unicode support.[47] Foundation
Python 3.0, released on 3 December 2008, with many of its major
License
https://en.wikipedia.org/wiki/Python_(programming_language) 1/36
11/3/23, 10:08 PM Python (programming language) - Wikipedia
features backported to Python 2.6.x[48] and 2.7.x. Releases of Filename Main: .py (it and
Python 3 include the 2to3 utility, which automates the extensions .pyw used on
translation of Python 2 code to Python 3.[49] Windows); .pyz
(for single-file ZIP
Python 2.7's end-of-life was initially set for 2015, then postponed
apps);[9]
to 2020 out of concern that a large body of existing code could not
Other: .pyi, .pyc,
easily be forward-ported to Python 3.[50][51] No further security
.pyd,
patches or other improvements will be released for it.[52][53]
Currently only 3.8 and later are supported (2023 security issues Website python.org (http
were fixed in e.g. 3.7.17, the final 3.7.x release[54]). s://www.python.or
g/)
In 2021 (and again twice in 2022), security updates were
expedited, since all Python versions were insecure (including Major implementations
2.7[55]) because of security issues leading to possible remote code CPython, PyPy, Stackless Python,
execution[56] and web-cache poisoning.[57] In 2022, MicroPython, CircuitPython,
Python 3.10.4 and 3.9.12 were expedited[58] and 3.8.13, because IronPython, Jython
of many security issues.[59] When Python 3.9.13 was released in Dialects
May 2022, it was announced that the 3.9 series (joining the older
series 3.8 and 3.7) would only receive security fixes in the Cython, RPython, Starlark[10]
future.[60] On 7 September 2022, four new releases were made Influenced by
due to a potential denial-of-service attack: 3.10.7, 3.9.14, 3.8.14, ABC,[11] Ada,[12] ALGOL 68,[13]
and 3.7.14.[61][62] APL,[14] C,[15] C++,[16] CLU,[17]
As of October 2023, Python 3.12 is the stable release, and 3.12 Dylan,[18]
and 3.11 are the only versions with active (as opposed to just Haskell,[19][14] Icon,[20] Lisp,[21]
security) support. Notable changes in 3.11 from 3.10 include Modula-3,[13][16] Perl,[22] Standard
increased program execution speed and improved error ML[14]
reporting.[63] Influenced
Python 3.12 adds syntax (and in fact every Python since at least Apache Groovy, Boo, Cobra,
3.5 adds some syntax) to the language, the new (soft) keyword CoffeeScript,[23] D, F#, GDScript,
type (recent releases have added a lot of typing support e.g. new Genie,[24] Go, JavaScript,[25][26]
type union operator in 3.10), and 3.11 for exception handling, and Julia,[27] Mojo,[28] Nim, Ring,[29]
3.10 the match and case (soft) keywords, for structural pattern Ruby,[30] Swift[31]
matching statements. Python 3.12 also drops outdated modules
Python Programming at
and functionality, and future versions will too, see below in
Development section. Wikibooks
Python 3.11 claims to be between 10 and 60% faster than Python 3.10, and Python 3.12 adds another
5% on top of that. It also has improved error messages, and many other changes.
Since 27 June 2023, Python 3.8 is the oldest supported version of Python (albeit in the 'security
support' phase), due to Python 3.7 reaching end-of-life.[64]
https://en.wikipedia.org/wiki/Python_(programming_language) 2/36
11/3/23, 10:08 PM Python (programming language) - Wikipedia
Its design offers some support for functional programming in the Lisp
tradition. It has filter,mapandreduce functions; list comprehensions,
dictionaries, sets, and generator expressions.[71] The standard library has
two modules (itertools and functools) that implement functional tools
borrowed from Haskell and Standard ML.[72] The designer of Python,
Guido van Rossum, at
Its core philosophy is summarized in the document The Zen of Python (PEP
OSCON 2006
20), which includes aphorisms such as:[73]
Rather than building all of its functionality into its core, Python was designed to be highly extensible
via modules. This compact modularity has made it particularly popular as a means of adding
programmable interfaces to existing applications. Van Rossum's vision of a small core language with a
large standard library and easily extensible interpreter stemmed from his frustrations with ABC,
which espoused the opposite approach.[41]
Python strives for a simpler, less-cluttered syntax and grammar while giving developers a choice in
their coding methodology. In contrast to Perl's "there is more than one way to do it" motto, Python
embraces a "there should be one—and preferably only one—obvious way to do it" philosophy.[73] Alex
Martelli, a Fellow at the Python Software Foundation and Python book author, wrote: "To describe
something as 'clever' is not considered a compliment in the Python culture."[74]
Python's developers strive to avoid premature optimization and reject patches to non-critical parts of
the CPython reference implementation that would offer marginal increases in speed at the cost of
clarity.[75] Execution speed can be improved by moving speed-critical functions to extension modules
written in languages such as C, or by using a just-in-time compiler like PyPy. It is also possible to
cross-compile to other languages, but it either doesn't provide the full speed-up that might be
expected, since Python is a very dynamic language, or a restricted subset of Python is compiled, and
possibly semantics are slightly changed.[76] Python's developers aim for it to be fun to use. This is
reflected in its name—a tribute to the British comedy group Monty Python[77]—and in occasionally
playful approaches to tutorials and reference materials, such as the use of the terms "spam" and
"eggs" (a reference to a Monty Python sketch) in examples, instead of the often-used "foo" and
"bar".[78][79]
A common neologism in the Python community is pythonic, which has a wide range of meanings
related to program style. "Pythonic" code may use Python idioms well, be natural or show fluency in
the language, or conform with Python's minimalist philosophy and emphasis on readability. Code that
https://en.wikipedia.org/wiki/Python_(programming_language) 3/36
11/3/23, 10:08 PM Python (programming language) - Wikipedia
is difficult to understand or reads like a rough transcription from another programming language is
called unpythonic.[80][81]
Indentation
Python uses whitespace indentation, rather than curly brackets or keywords, to delimit blocks. An
increase in indentation comes after certain statements; a decrease in indentation signifies the end of
the current block.[83] Thus, the program's visual structure accurately represents its semantic
structure.[84] This feature is sometimes termed the off-side rule. Some other languages use
indentation this way; but in most, indentation has no semantic meaning. The recommended indent
size is four spaces.[85]
The assert statement, used in debugging to check for conditions that should apply
The yield statement, which returns a value from a generator function (and also an operator);
used to implement coroutines
The return statement, used to return a value from a function
The import and from statements, used to import modules whose functions or variables can be
used in the current program
The assignment statement (=) binds a name as a reference to a separate, dynamically allocated object.
Variables may subsequently be rebound at any time to any object. In Python, a variable name is a
generic reference holder without a fixed data type; however, it always refers to some object with a
type. This is called dynamic typing—in contrast to statically-typed languages, where each variable may
contain only a value of a certain type.
Python does not support tail call optimization or first-class continuations, and, according to Van
Rossum, it never will.[88][89] However, better support for coroutine-like functionality is provided by
extending Python's generators.[90] Before 2.5, generators were lazy iterators; data was passed
unidirectionally out of the generator. From Python 2.5 on, it is possible to pass data back into a
generator function; and from version 3.3, it can be passed through multiple stack levels.[91]
Expressions
The +, -, and * operators for mathematical addition, subtraction, and multiplication are similar to
other languages, but the behavior of division differs. There are two types of divisions in Python:
floor division (or integer division) // and floating-point/division.[92] Python uses the ** operator
for exponentiation.
Python uses the + operator for string concatenation. Python uses the * operator for duplicating a
string a specified number of times.
The @ infix operator. It is intended to be used by libraries such as NumPy for matrix
multiplication.[93][94]
The syntax :=, called the "walrus operator", was introduced in Python 3.8. It assigns values to
variables as part of a larger expression.[95]
In Python, == compares by value. Python's is operator may be used to compare object identities
(comparison by reference), and comparisons may be chained—for example, a <= b <= c.
Python uses and, or, and not as Boolean operators.
Python has a type of expression called a list comprehension, as well as a more general
expression called a generator expression.[71]
Anonymous functions are implemented using lambda expressions; however, there may be only
one expression in each body.
Conditional expressions are written as x if c else y[96] (different in order of operands from the
c ? x : y operator common to many other languages).
Python makes a distinction between lists and tuples. Lists are written as [1, 2, 3], are mutable,
and cannot be used as the keys of dictionaries (dictionary keys must be immutable in Python).
Tuples, written as (1, 2, 3), are immutable and thus can be used as keys of dictionaries,
provided all of the tuple's elements are immutable. The + operator can be used to concatenate
two tuples, which does not directly modify their contents, but produces a new tuple containing the
elements of both. Thus, given the variable t initially equal to (1, 2, 3), executing t = t + (4,
https://en.wikipedia.org/wiki/Python_(programming_language) 5/36
11/3/23, 10:08 PM Python (programming language) - Wikipedia
5) first evaluates t + (4, 5), which yields (1, 2, 3, 4, 5), which is then assigned back to t
—thereby effectively "modifying the contents" of t while conforming to the immutable nature of
tuple objects. Parentheses are optional for tuples in unambiguous contexts.[97]
Python features sequence unpacking where multiple expressions, each evaluating to anything
that can be assigned (to a variable, writable property, etc.) are associated in an identical manner
to that forming tuple literals—and, as a whole, are put on the left-hand side of the equal sign in an
assignment statement. The statement expects an iterable object on the right-hand side of the
equal sign that produces the same number of values as the provided writable expressions; when
iterated through them, it assigns each of the produced values to the corresponding expression on
the left.[98]
Python has a "string format" operator % that functions analogously to printf format strings in C—
e.g. "spam=%s eggs=%d" % ("blah", 2) evaluates to "spam=blah eggs=2". In Python 2.6+
and 3+, this was supplemented by the format() method of the str class, e.g. "spam={0} eggs=
{1}".format("blah", 2). Python 3.6 added "f-strings": spam = "blah"; eggs = 2; f'spam=
{spam} eggs={eggs}'.[99]
Strings in Python can be concatenated by "adding" them (with the same operator as for adding
integers and floats), e.g. "spam" + "eggs" returns "spameggs". If strings contain numbers, they
are added as strings rather than integers, e.g. "2" + "2" returns "22".
Python has various string literals:
Delimited by single or double quote marks; unlike in Unix shells, Perl, and Perl-influenced
languages, single and double quote marks work the same. Both use the backslash (\) as an
escape character. String interpolation became available in Python 3.6 as "formatted string
literals".[99]
Triple-quoted (beginning and ending with three single or double quote marks), which may
span multiple lines and function like here documents in shells, Perl, and Ruby.
Raw string varieties, denoted by prefixing the string literal with r. Escape sequences are not
interpreted; hence raw strings are useful where literal backslashes are common, such as
regular expressions and Windows-style paths. (Compare "@-quoting" in C#.)
Python has array index and array slicing expressions in lists, denoted as a[key], a[start:stop]
or a[start:stop:step]. Indexes are zero-based, and negative indexes are relative to the end.
Slices take elements from the start index up to, but not including, the stop index. The third slice
parameter called step or stride, allows elements to be skipped and reversed. Slice indexes may
be omitted—for example, a[:] returns a copy of the entire list. Each element of a slice is a
shallow copy.
https://en.wikipedia.org/wiki/Python_(programming_language) 6/36
11/3/23, 10:08 PM Python (programming language) - Wikipedia
This has the advantage of avoiding a classic C error of mistaking an assignment operator = for an
equality operator == in conditions: if (c = 1) { ... } is syntactically valid (but probably
unintended) C code, but if c = 1: ... causes a syntax error in Python.
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 (or this) in some other object-oriented programming
languages (e.g., C++, Java, Objective-C, Ruby).[100] Python also provides methods, often called
dunder methods (due to their names beginning and ending with double-underscores), to allow user-
defined classes to modify how they are handled by native operations including length, comparison, in
arithmetic operations and type conversion.[101]
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 it 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.
Before version 3.0, Python had two kinds of classes (both using the
same syntax): old-style and new-style,[102] current Python The standard type hierarchy in
versions only support the semantics new style. Python 3
https://en.wikipedia.org/wiki/Python_(programming_language) 7/36