
The Python Library Reference, Release 3.4.1
b + a % b is very close to a, if a % b is non-zero it has the same sign as b, and 0 <= abs(a % b)
< abs(b).
enumerate(iterable, start=0)
Return an enumerate object. iterable must be a sequence, an iterator, or some other object which supports
iteration. The __next__() method of the iterator returned by enumerate() returns a tuple containing
a count (from start which defaults to 0) and the values obtained from iterating over iterable.
>>> seasons = [’Spring’, ’Summer’, ’Fall’, ’Winter’]
>>> list(enumerate(seasons))
[(0, ’Spring’), (1, ’Summer’), (2, ’Fall’), (3, ’Winter’)]
>>> list(enumerate(seasons, start=1))
[(1, ’Spring’), (2, ’Summer’), (3, ’Fall’), (4, ’Winter’)]
Equivalent to:
def enumerate(sequence, start=0):
n = start
for elem in sequence:
yield n, elem
n += 1
eval(expression, globals=None, locals=None)
The arguments are a string and optional globals and locals. If provided, globals must be a dictionary. If
provided, locals can be any mapping object.
The expression argument is parsed and evaluated as a Python expression (technically speaking, a condition
list) using the globals and locals dictionaries as global and local namespace. If the globals dictionary is
present and lacks ‘__builtins__’, the current globals are copied into globals before expression is parsed.
This means that expression normally has full access to the standard builtins module and restricted
environments are propagated. If the locals dictionary is omitted it defaults to the globals dictionary. If both
dictionaries are omitted, the expression is executed in the environment where eval() is called. The return
value is the result of the evaluated expression. Syntax errors are reported as exceptions. Example:
>>> x = 1
>>> eval(’x+1’)
2
This function can also be used to execute arbitrary code objects (such as those created by compile()). In
this case pass a code object instead of a string. If the code object has been compiled with ’exec’ as the
mode argument, eval()‘s return value will be None.
Hints: dynamic execution of statements is supported by the exec() function. The globals() and
locals() functions returns the current global and local dictionary, respectively, which may be useful to
pass around for use by eval() or exec().
See ast.literal_eval() for a function that can safely evaluate strings with expressions containing
only literals.
exec(object[, globals[, locals ]])
This function supports dynamic execution of Python code. object must be either a string or a code object. If
it is a string, the string is parsed as a suite of Python statements which is then executed (unless a syntax error
occurs).
1
If it is a code object, it is simply executed. In all cases, the code that’s executed is expected to
be valid as file input (see the section “File input” in the Reference Manual). Be aware that the return and
yield statements may not be used outside of function definitions even within the context of code passed
to the exec() function. The return value is None.
In all cases, if the optional parts are omitted, the code is executed in the current scope. If only globals is
provided, it must be a dictionary, which will be used for both the global and the local variables. If globals
1
Note that the parser only accepts the Unix-style end of line convention. If you are reading the code from a file, make sure to use newline
conversion mode to convert Windows or Mac-style newlines.
9