>>>
..
.
.pyc
and .pyo
files so that executing the same file is faster the second time
(recompilation from source to byte code can be avoided). This
``intermediate language'' is said to run on a ``virtual
machine'' that calls the subroutines corresponding to each bytecode.
int(3.15)
converts the floating point number to the integer
3
, but in 3+4.5
, each argument is of a different type (one
int, one float), and both must be converted to the same type before they can
be added or it will raise a TypeError
. Coercion between two
operands can be performed with the coerce
builtin function; thus,
3+4.5
is equivalent to calling operator.add(*coerce(3,
4.5))
and results in operator.add(3.0, 4.5)
. Without coercion,
all arguments of even compatible types would have to be normalized to the
same value by the programmer, e.g., float(3)+4.5
rather than just
3+4.5
.
-1
),
often written i
in mathematics or j
in engineering.
Python has builtin support for complex numbers, which are written with this
latter notation; the imaginary part is written with a j
suffix,
e.g., 3+1j
. To get access to complex equivalents of the
math module, use cmath. Use of complex numbers is a
fairly advanced mathematical feature. If you're not aware of a need for them,
it's almost certain you can safely ignore them.
11/4
currently evaluates to 2
.
If the module in which it is executed had enabled true division
by executing:
from __future__ import division
the expression 11/4
would evaluate to 2.75
. By
importing the __future__
module and evaluating its variables, you can see when a new feature
was first added to the language and when it will become the default:
>>> import __future__ >>> __future__.division _Feature((2, 2, 0, 'alpha', 2), (3, 0, 0, 'alpha', 0), 8192)
>>> sum(i*i for i in range(10)) # sum of squares 0, 1, 4, ... 81 285
11/4
currently evaluates to 2
in contrast
to the 2.75
returned by float division. Also called
floor division. When dividing two integers the outcome will
always be another integer (having the floor function applied to it).
However, if one of the operands is another numeric type (such as a
float), the result will be coerced (see coercion) to
a common type. For example, an integer divided by a float will result
in a float value, possibly with a decimal fraction. Integer division
can be forced by using the //
operator instead of the /
operator. See also __future__.
python
with no
arguments (possibly by selecting it from your computer's main menu).
It is a very powerful way to test out new ideas or inspect modules and
packages (remember help(x)
).
for
statement does
that automatically for you, creating a temporary unnamed variable to
hold the iterator for the duration of the loop. See also
iterator, sequence, and generator.
result = ["0x%02x"
% x for x in range(256) if x % 2 == 0]
generates a list of strings
containing hex numbers (0x..) that are even and in the range from 0 to 255.
The if clause is optional. If omitted, all elements in
range(256)
are processed.
import this
'' at the interactive prompt.
See About this document... for information on suggesting changes.