Python Is An Interpreted, High-Level, General-Purpose Programming Language. Created
Python Is An Interpreted, High-Level, General-Purpose Programming Language. Created
The assignment statement (token '=', the equals sign). This operates differently than
in traditional imperative programming languages, and this fundamental mechanism
(including the nature of Python's version of variables) illuminates many other features
of the language. Assignment in C, e.g., x = 2 , translates to "typed variable name x
receives a copy of numeric value 2". The (right-hand) value is copied into an allocated
storage location for which the (left-hand) variable name is the symbolic address. The
memory allocated to the variable is large enough (potentially quite large) for the
declared type. In the simplest case of Python assignment, using the same example, x
= 2 , translates to "(generic) name x receives a reference to a separate, dynamically
allocated object of numeric (int) type of value 2." This is termed binding the name to
the object. Since the name's storage location doesn't contain the indicated value, it is
improper to call it a variable. Names may be subsequently rebound at any time to
objects of greatly varying types, including strings, procedures, complex objects with
data and methods, etc. Successive assignments of a common value to multiple
names, e.g., x = 2 ; y = 2 ; z = 2 result in allocating storage to (at most) three names
and one numeric object, to which all three names are bound. Since a name is a
generic reference holder it is unreasonable to associate a fixed data type with it.
However at a given time a name will be bound to some object, which will have a type;
thus there is dynamic typing.
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 raise statement, used to raise a specified exception or re-raise a caught
exception.
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, from Python 2.5 released on September 2006,[61] 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, or opening a file and then
closing it), allowing Resource Acquisition Is Initialization (RAII)-like behavior and
replaces a common try/finally idiom.[62]
The pass statement, which serves as a NOP. It is syntactically needed to create an
empty code block.
The assert statement, used during debugging to check for conditions that ought to
apply.
The yield statement, which returns a value from a generator function. From Python
2.5, yield is also an operator. This form is used to implement coroutines.
The import statement, which is used to import modules whose functions or variables
can be used in the current program. There are three ways of using import: import
<module name> [as <alias>] or from <module name> import * or from <module
name> import <definition 1> [as <alias 1>], <definition 2> [as <alias 2>], ... .
The print statement was changed to the print() function in Python 3.[63]
Python does not support tail call optimization or first-class continuations, and, according
to Guido van Rossum, it never will.[64][65] However, better support for coroutine-like
functionality is provided in 2.5, by extending Python's generators.[66] Before 2.5,
generators were lazy iterators; information was passed unidirectionally out of the
generator. From Python 2.5, it is possible to pass information back into a generator
function, and from Python 3.3, the information can be passed through multiple stack
levels.[67]
Expressions[edit]
Some Python expressions are similar to languages such as C and Java, while some are
not:
Addition, subtraction, and multiplication are the same, but the behavior of division
differs. There are two types of divisions in Python. They are floor division and integer
division.[68] Python also added the ** operator for exponentiation.
From Python 3.5, the new @ infix operator was introduced. It is intended to be used
by libraries such as NumPy for matrix multiplication.[69][70]
In Python, == compares by value, versus Java, which compares numerics by
value[71] and objects by reference.[72] (Value comparisons in Java on objects can be
performed with the equals() method.) Python's is operator may be used to compare
object identities (comparison by reference). In Python, comparisons may be chained,
for example a <= b <= c .
Python uses the words and , or , not for its boolean operators rather than the
symbolic && , || , ! used in Java and C.
Python has a type of expression termed a list comprehension. Python 2.4 extended
list comprehensions into a more general expression termed
a generator expression.[49]
Anonymous functions are implemented using lambda expressions; however, these
are limited in that the body can only be one expression.
Conditional expressions in Python are written as x if c else y [73] (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 are written as (1, 2, 3) , are immutable and thus can
be used as the keys of dictionaries, provided all elements of the tuple are immutable.
The + operator can be used to concatenate two tuples, which does not directly modify
their contents, but rather produces a new tuple containing the elements of both
provided tuples. Thus, given the variable t initially equal to (1, 2, 3) , executing t = t
+ (4, 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.[74]