Python is intended to be a highly readable language. It is designed to have an uncluttered visual layout, often using English keywords where other languages use punctuation. Further, Python has fewer syntactic exceptions and special cases than C or Pascal.
Python uses whitespace indentation to delimit blocks – rather than curly braces or keywords. An increase in indentation comes after certain statements; a decrease in indentation signifies the end of the current block. This feature is also sometimes termed the off-side rule.
Statements and control flow
Python’s statements include (among others):
- 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 = 2result 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.
ifstatement, which conditionally executes a block of code, along with
elif(a contraction of else-if).
forstatement, which iterates over an iterable object, capturing each element to a local variable for use by the attached block.
whilestatement, which executes a block of code as long as its condition is true.
trystatement, which allows exceptions raised in its attached code block to be caught and handled by
exceptclauses; it also ensures that clean-up code in a
finallyblock will always be run regardless of how the block exits.
classstatement, which executes a block of code and attaches its local namespace to a class, for use in object-oriented programming.
defstatement, which defines a function or method.
withstatement (from Python 2.5), 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.
passstatement, which serves as a NOP. It is syntactically needed to create an empty code block.
assertstatement, used during debugging to check for conditions that ought to apply.
yieldstatement, which returns a value from a generator function. From Python 2.5,
yieldis also an operator. This form is used to implement coroutines.
importstatement, which is used to import modules whose functions or variables can be used in the current program.
print()function in Python 3.
Python does not support tail call optimization or first-class continuations, and, according to Guido van Rossum, it never will. However, better support for coroutine-like functionality is provided in 2.5, by extending Python’s generators.Before 2.5, generators were lazy iterators; information was passed unidirectionally out of the generator. As of Python 2.5, it is possible to pass information back into a generator function, and as of Python 3.3, the information can be passed through multiple stack levels.