The language incorporates built-in data types and structures, control flow mechanisms, first-class functions, and modules for better code reusability and organization.
The language provides robust error handling through exceptions, and includes a debugger in the standard library for efficient problem-solving.
Python's syntax, designed for readability and ease of use, makes it a popular choice among beginners and professionals alike.
Python borrows this feature from its predecessor ABC: instead of punctuation or keywords, it uses indentation to indicate the run of a block.
Implicit conversion is defined for numeric types (as well as booleans), so one may validly multiply a complex number by an integer (for instance) without explicit casting.
Lists, on the other hand, are mutable; elements can be inserted, deleted, modified, appended, or sorted in-place.
The keys in a dictionary must be of an immutable Python type, such as an integer or a string, because under the hood they are implemented via a hash function.
Since these dictionaries are directly accessible (via an object's __dict__ attribute), metaprogramming is a straightforward and natural process in Python.
Python also provides extensive collection manipulating abilities such as built in containment checking and a generic iteration protocol.
There are also multi-line strings, which begin and end with a series of three single or double quotes and function like here documents in Perl and Ruby.
A simple example with variable interpolation (using the format method) is: Finally, all of the previously mentioned string types come in "raw" varieties (denoted by placing a literal r before the opening quote), which do no backslash-interpolation and hence are very useful for regular expressions; compare "@-quoting" in C#.
The conversion to "long" integers was performed automatically when required, and thus the programmer usually didn't have to be aware of the two integral types.
Lists (class list) are mutable sequences of items of arbitrary types, and can be created either with the special syntax or using normal object creation Tuples (class tuple) are immutable sequences of items of arbitrary types.
Given: and you can see that b, as visible from the closure's scope, retains the value it had; the changed binding of b inside the inner function did not propagate out.
Python has very limited support for private variables using name mangling which is rarely used in practice as information hiding is seen by some as unpythonic, in that it suggests that the class in question contains unaesthetic or ill-planned internals.
This nullifies the practical advantage of accessor functions, and it remains OOP because the property eggs becomes a legitimate part of the object's interface: it need not reflect an implementation detail.
It is also possible to run custom code while accessing or setting attributes, though the details of those techniques have evolved between Python versions.
Properties allow specially defined methods to be invoked on an object instance by using the same syntax as used for attribute access.
For instance, the Mailman mailing list software, written in Python, uses exceptions to jump out of deeply nested message-handling logic when a decision has been made to reject a message or hold it for moderator approval.
A commonly invoked motto is EAFP, or "It is Easier to Ask for Forgiveness than Permission,"[26] which is attributed to Grace Hopper.
In addition, it avoids the whole class of time-of-check-to-time-of-use (TOCTTOU) vulnerabilities, other race conditions,[28][30] and is compatible with duck typing.
A drawback of EAFP is that it can be used only with statements; an exception cannot be caught in a generator expression, list comprehension, or lambda function.
Comments spanning more than one line are achieved by inserting a multi-line string (with """ or ''' as the delimiter on each end) that is not used in assignment or otherwise evaluated, but sits in between other statements.
Commenting a piece of code: Commenting a piece of code with multiple lines: Docstrings (documentation strings), that is, strings that are located alone without assignment as the first indented line within a module, class, method or function, automatically set their contents as an attribute named __doc__, which is intended to store a human-readable description of the object's purpose, behavior, and usage.
However, the style guide for the language specifies that triple double quotes (""") are preferred for both single and multi-line docstrings.
For example, a library could be written to handle static typing:[32] A decorator is any callable Python object that is used to modify a function, method or class definition.
For example, in the sample below, viking_chorus might cause menu_item to be run 8 times (see Spam sketch) for each time it is called: Canonical uses of function decorators are for creating class methods or static methods, adding function attributes, tracing, setting pre- and postconditions, and synchronization,[33] but can be used for far more, including tail recursion elimination,[34] memoization and even improving the writing of other decorators.
Closure ensures that the colour argument is accessible to the innermost wrapper function even when it is returned and goes out of scope, which is what allows decorators to work.
The decorator pattern itself is trivially implementable in Python, because the language is duck typed, and so is not usually considered as such.
[clarification needed] Users of curly bracket languages, such as C or Java, sometimes expect or wish Python to follow a block-delimiter convention.