<s>
The	O
syntax	O
of	O
the	O
Python	B-Language
programming	I-Language
language	I-Language
is	O
the	O
set	O
of	O
rules	O
that	O
defines	O
how	O
a	O
Python	B-Language
program	I-Language
will	O
be	O
written	O
and	O
interpreted	B-Application
(	O
by	O
both	O
the	O
runtime	B-Device
system	I-Device
and	O
by	O
human	O
readers	O
)	O
.	O
</s>
<s>
The	O
Python	B-Language
language	I-Language
has	O
many	O
similarities	O
to	O
Perl	B-Language
,	O
C	B-Language
,	O
and	O
Java	B-Language
.	O
</s>
<s>
Python	B-Language
was	O
designed	O
to	O
be	O
a	O
highly	O
readable	O
language	O
.	O
</s>
<s>
It	O
has	O
a	O
relatively	O
uncluttered	O
visual	O
layout	B-Language
and	O
uses	O
English	O
keywords	O
frequently	O
where	O
other	O
languages	O
use	O
punctuation	O
.	O
</s>
<s>
Python	B-Language
aims	O
to	O
be	O
simple	O
and	O
consistent	O
in	O
the	O
design	O
of	O
its	O
syntax	O
,	O
encapsulated	O
in	O
the	O
mantra	O
,	O
from	O
the	O
Zen	O
of	O
Python	B-Language
.	O
</s>
<s>
This	O
mantra	O
is	O
deliberately	O
opposed	O
to	O
the	O
Perl	B-Language
and	O
Ruby	B-Language
mantra	O
,	O
"	O
there	B-Architecture
's	I-Architecture
more	I-Architecture
than	I-Architecture
one	I-Architecture
way	I-Architecture
to	I-Architecture
do	I-Architecture
it	I-Architecture
"	O
.	O
</s>
<s>
Python	B-Language
has	O
35	O
keywords	O
or	O
reserved	O
words	O
;	O
they	O
cannot	O
be	O
used	O
as	O
identifiers	O
.	O
</s>
<s>
In	O
addition	O
,	O
Python	B-Language
also	O
has	O
3	O
soft	O
keywords	O
.	O
</s>
<s>
Python	B-Language
uses	O
whitespace	O
to	O
delimit	O
control	O
flow	O
blocks	O
(	O
following	O
the	O
off-side	B-Language
rule	I-Language
)	O
.	O
</s>
<s>
Python	B-Language
borrows	O
this	O
feature	O
from	O
its	O
predecessor	O
ABC	B-Language
:	O
instead	O
of	O
punctuation	O
or	O
keywords	O
,	O
it	O
uses	O
indentation	O
to	O
indicate	O
the	O
run	O
of	O
a	O
block	O
.	O
</s>
<s>
In	O
so-called	O
"	O
free-format	O
"	O
languages	O
—	O
that	O
use	O
the	O
block	O
structure	O
derived	O
from	O
ALGOL	B-Language
—	O
blocks	O
of	O
code	O
are	O
set	O
off	O
with	O
braces	O
( {	O
}	O
)	O
or	O
keywords	O
.	O
</s>
<s>
A	O
recursive	O
function	O
named	O
foo	O
,	O
which	O
is	O
passed	O
a	O
single	O
parameter	O
,	O
x	O
,	O
and	O
if	O
the	O
parameter	O
is	O
0	O
will	O
call	O
a	O
different	O
function	O
named	O
bar	O
and	O
otherwise	O
will	O
call	O
baz	O
,	O
passing	O
x	O
,	O
and	O
also	O
call	O
itself	O
recursively	O
,	O
passing	O
x-1	O
as	O
the	O
parameter	O
,	O
could	O
be	O
implemented	O
like	O
this	O
in	O
Python	B-Language
:	O
</s>
<s>
and	O
could	O
be	O
written	O
like	O
this	O
in	O
C	B-Language
with	O
K&R	O
indent	O
style	O
:	O
</s>
<s>
Incorrectly	O
indented	O
code	O
could	O
be	O
misread	O
by	O
a	O
human	O
reader	O
differently	O
than	O
it	O
would	O
be	O
interpreted	B-Application
by	O
a	O
compiler	B-Language
or	O
interpreter	B-Application
.	O
</s>
<s>
Mixing	O
spaces	O
and	O
tabs	O
on	O
consecutive	O
lines	O
is	O
not	O
allowed	O
starting	O
with	O
Python	B-Language
3	O
because	O
that	O
can	O
create	O
bugs	O
which	O
are	O
difficult	O
to	O
see	O
,	O
since	O
many	O
tools	O
do	O
not	O
visually	O
distinguish	O
spaces	O
and	O
tabs	O
.	O
</s>
<s>
Since	O
Python	B-Language
is	O
a	O
dynamically-typed	O
language	O
,	O
Python	B-Language
values	O
,	O
not	O
variables	O
,	O
carry	O
type	O
information	O
.	O
</s>
<s>
All	O
variables	O
in	O
Python	B-Language
hold	O
references	O
to	O
objects	B-Language
,	O
and	O
these	O
references	O
are	O
passed	O
to	O
functions	O
.	O
</s>
<s>
a	O
copy	O
of	O
the	O
reference	O
to	O
the	O
same	O
object	O
,	O
just	O
as	O
in	O
C/C	O
++	O
.	O
</s>
<s>
In	O
Python	B-Language
,	O
non-innermost-local	O
and	O
not-declared-global	O
accessible	O
names	O
are	O
all	O
aliases	O
.	O
</s>
<s>
Among	O
dynamically-typed	O
languages	O
,	O
Python	B-Language
is	O
moderately	O
type-checked	O
.	O
</s>
<s>
Implicit	O
conversion	O
is	O
defined	O
for	O
numeric	B-Algorithm
types	I-Algorithm
(	O
as	O
well	O
as	O
booleans	O
)	O
,	O
so	O
one	O
may	O
validly	O
multiply	O
a	O
complex	O
number	O
by	O
an	O
integer	O
(	O
for	O
instance	O
)	O
without	O
explicit	O
casting	O
.	O
</s>
<s>
Python	B-Language
has	O
a	O
broad	O
range	O
of	O
basic	O
data	O
types	O
.	O
</s>
<s>
Alongside	O
conventional	O
integer	O
and	O
floating-point	B-Algorithm
arithmetic	I-Algorithm
,	O
it	O
transparently	O
supports	O
arbitrary-precision	B-Algorithm
arithmetic	I-Algorithm
,	O
complex	O
numbers	O
,	O
and	O
decimal	B-Algorithm
numbers	I-Algorithm
.	O
</s>
<s>
Python	B-Language
supports	O
a	O
wide	O
variety	O
of	O
string	O
operations	O
.	O
</s>
<s>
Strings	O
in	O
Python	B-Language
are	O
immutable	B-Application
,	O
so	O
a	O
string	O
operation	O
such	O
as	O
a	O
substitution	O
of	O
characters	O
,	O
that	O
in	O
other	O
programming	O
languages	O
might	O
alter	O
the	O
string	O
in	B-Algorithm
place	I-Algorithm
,	O
returns	O
a	O
new	O
string	O
in	O
Python	B-Language
.	O
</s>
<s>
One	O
of	O
the	O
very	O
useful	O
aspects	O
of	O
Python	B-Language
is	O
the	O
concept	O
of	O
collection	O
(	O
or	O
container	B-Application
)	O
types	O
.	O
</s>
<s>
In	O
general	O
a	O
collection	O
is	O
an	O
object	O
that	O
contains	O
other	O
objects	B-Language
in	O
a	O
way	O
that	O
is	O
easily	O
referenced	O
or	O
indexed	O
.	O
</s>
<s>
The	O
ordered	O
sequential	O
types	O
are	O
lists	O
(	O
dynamic	O
arrays	O
)	O
,	O
tuples	B-Application
,	O
and	O
strings	O
.	O
</s>
<s>
Both	O
strings	O
and	O
tuples	B-Application
are	O
immutable	B-Application
,	O
making	O
them	O
perfect	O
candidates	O
for	O
dictionary	O
keys	O
(	O
see	O
below	O
)	O
.	O
</s>
<s>
Lists	O
,	O
on	O
the	O
other	O
hand	O
,	O
are	O
mutable	B-Application
;	O
elements	O
can	O
be	O
inserted	O
,	O
deleted	O
,	O
modified	O
,	O
appended	O
,	O
or	O
sorted	O
in-place	B-Algorithm
.	O
</s>
<s>
Mappings	O
,	O
on	O
the	O
other	O
hand	O
,	O
are	O
(	O
often	O
unordered	O
)	O
types	O
implemented	O
in	O
the	O
form	O
of	O
dictionaries	O
which	O
"	O
map	O
"	O
a	O
set	O
of	O
immutable	B-Application
keys	O
to	O
corresponding	O
elements	O
(	O
much	O
like	O
a	O
mathematical	O
function	O
)	O
.	O
</s>
<s>
The	O
keys	O
in	O
a	O
dictionary	O
must	O
be	O
of	O
an	O
immutable	B-Application
Python	B-Language
type	O
,	O
such	O
as	O
an	O
integer	O
or	O
a	O
string	O
,	O
because	O
under	O
the	O
hood	O
they	O
are	O
implemented	O
via	O
a	O
hash	B-Error_Name
function	I-Error_Name
.	O
</s>
<s>
Dictionaries	O
are	O
central	O
to	O
the	O
internals	O
of	O
Python	B-Language
as	O
they	O
reside	O
at	O
the	O
core	O
of	O
all	O
objects	B-Language
and	O
classes	O
:	O
the	O
mappings	O
between	O
variable	O
names	O
(	O
strings	O
)	O
and	O
the	O
values	O
which	O
the	O
names	O
reference	O
are	O
stored	O
as	O
dictionaries	O
(	O
see	O
Object	B-Language
system	I-Language
)	O
.	O
</s>
<s>
Since	O
these	O
dictionaries	O
are	O
directly	O
accessible	O
(	O
via	O
an	O
object	O
's	O
__dict__	O
attribute	O
)	O
,	O
metaprogramming	B-Application
is	O
a	O
straightforward	O
and	O
natural	O
process	O
in	O
Python	B-Language
.	O
</s>
<s>
There	O
are	O
two	O
types	O
of	O
sets	O
:	O
set	O
and	O
frozenset	O
,	O
the	O
only	O
difference	O
being	O
that	O
set	O
is	O
mutable	B-Application
and	O
frozenset	O
is	O
immutable	B-Application
.	O
</s>
<s>
Python	B-Language
also	O
provides	O
extensive	O
collection	O
manipulating	O
abilities	O
such	O
as	O
built	O
in	O
containment	O
checking	O
and	O
a	O
generic	O
iteration	O
protocol	O
.	O
</s>
<s>
In	O
Python	B-Language
,	O
everything	O
is	O
an	O
object	O
,	O
even	O
classes	O
.	O
</s>
<s>
Classes	O
,	O
as	O
objects	B-Language
,	O
have	O
a	O
class	O
,	O
which	O
is	O
known	O
as	O
their	O
metaclass	B-Language
.	O
</s>
<s>
Python	B-Language
also	O
supports	O
multiple	B-Application
inheritance	I-Application
and	O
mixins	B-Language
.	O
</s>
<s>
The	O
language	O
supports	O
extensive	O
introspection	B-Application
of	O
types	O
and	O
classes	O
.	O
</s>
<s>
Operators	O
can	O
be	O
overloaded	O
in	O
Python	B-Language
by	O
defining	O
special	O
member	O
functions	O
—	O
for	O
instance	O
,	O
defining	O
a	O
method	O
named	O
__add__	O
on	O
a	O
class	O
permits	O
one	O
to	O
use	O
the	O
+	O
operator	O
on	O
objects	B-Language
of	O
that	O
class	O
.	O
</s>
<s>
Python	B-Language
has	O
various	O
kinds	O
of	O
string	B-Language
literals	I-Language
.	O
</s>
<s>
Unlike	O
in	O
Unix	O
shell	O
languages	O
,	O
Perl	B-Language
or	O
Perl-influenced	O
languages	O
such	O
as	O
Ruby	B-Language
or	O
Groovy	B-Application
,	O
single	O
quotes	O
and	O
double	O
quotes	O
function	O
identically	O
,	O
i.e.	O
</s>
<s>
However	O
,	O
interpolation	O
can	O
be	O
done	O
in	O
various	O
ways	O
:	O
with	O
"	O
f-strings	O
"	O
(	O
since	O
Python	B-Language
3.6	O
)	O
,	O
using	O
the	O
format	O
method	O
or	O
the	O
old	O
%	O
string-format	O
operator	O
.	O
</s>
<s>
For	O
instance	O
,	O
all	O
of	O
these	O
Python	B-Language
statements:are	O
equivalent	O
to	O
the	O
Perl	B-Language
statement:They	O
build	O
a	O
string	O
using	O
the	O
variables	O
num	O
and	O
printer	O
.	O
</s>
<s>
There	O
are	O
also	O
multi-line	O
strings	O
,	O
which	O
begin	O
and	O
end	O
with	O
a	O
series	O
of	O
three	O
single	O
or	O
double	O
quotes	O
and	O
function	O
like	O
here	O
documents	O
in	O
Perl	B-Language
and	O
Ruby	B-Language
.	O
</s>
<s>
Finally	O
,	O
all	O
of	O
the	O
previously	O
mentioned	O
string	O
types	O
come	O
in	O
"	O
raw	O
"	O
varieties	O
(	O
denoted	O
by	O
placing	O
a	O
literal	O
r	O
before	O
the	O
opening	O
quote	O
)	O
,	O
which	O
do	O
no	O
backslash-interpolation	O
and	O
hence	O
are	O
very	O
useful	O
for	O
regular	B-Language
expressions	I-Language
;	O
compare	O
"	O
@	O
-quoting	O
"	O
in	O
C#	B-Application
.	O
</s>
<s>
Raw	O
strings	O
were	O
originally	O
included	O
specifically	O
for	O
regular	B-Language
expressions	I-Language
.	O
</s>
<s>
String	B-Language
literals	I-Language
(	O
using	O
possibly	O
different	O
quote	O
conventions	O
)	O
appearing	O
contiguously	O
and	O
only	O
separated	O
by	O
whitespace	O
(	O
including	O
new	O
lines	O
)	O
,	O
are	O
allowed	O
and	O
are	O
aggregated	O
into	O
a	O
single	O
longer	O
string	O
.	O
</s>
<s>
Since	O
Python	B-Language
3.0	O
,	O
the	O
default	O
character	O
set	O
is	O
UTF-8	O
both	O
for	O
source	O
code	O
and	O
the	O
interpreter	B-Application
.	O
</s>
<s>
Numeric	B-Algorithm
literals	O
in	O
Python	B-Language
are	O
of	O
the	O
normal	O
sort	O
,	O
e.g.	O
</s>
<s>
Python	B-Language
has	O
arbitrary-length	O
integers	O
and	O
automatically	O
increases	O
their	O
storage	O
size	O
as	O
necessary	O
.	O
</s>
<s>
Prior	O
to	O
Python	B-Language
3	O
,	O
there	O
were	O
two	O
kinds	O
of	O
integral	O
numbers	O
:	O
traditional	O
fixed	O
size	O
integers	O
and	O
"	O
long	O
"	O
integers	O
of	O
arbitrary	O
size	O
.	O
</s>
<s>
Python	B-Language
supports	O
normal	O
floating	B-Algorithm
point	I-Algorithm
numbers	I-Algorithm
,	O
which	O
are	O
created	O
when	O
a	O
dot	O
is	O
used	O
in	O
a	O
literal	O
(	O
e.g.	O
</s>
<s>
1.1	O
)	O
,	O
when	O
an	O
integer	O
and	O
a	O
floating	B-Algorithm
point	I-Algorithm
number	I-Algorithm
are	O
used	O
in	O
an	O
expression	O
,	O
or	O
as	O
a	O
result	O
of	O
some	O
mathematical	O
operations	O
(	O
"	O
true	O
division	O
"	O
via	O
the	O
/	O
operator	O
,	O
or	O
exponentiation	O
with	O
a	O
negative	O
exponent	O
)	O
.	O
</s>
<s>
Python	B-Language
also	O
supports	O
complex	O
numbers	O
natively	O
.	O
</s>
<s>
Python	B-Language
has	O
syntactic	O
support	O
for	O
the	O
creation	O
of	O
container	B-Application
types	O
.	O
</s>
<s>
Tuples	B-Application
(	O
class	O
tuple	B-Application
)	O
are	O
immutable	B-Application
sequences	O
of	O
items	O
of	O
arbitrary	O
types	O
.	O
</s>
<s>
Although	O
tuples	B-Application
are	O
created	O
by	O
separating	O
items	O
with	O
commas	O
,	O
the	O
whole	O
construct	O
is	O
usually	O
wrapped	O
in	O
parentheses	O
to	O
increase	O
readability	O
.	O
</s>
<s>
An	O
empty	O
tuple	B-Application
is	O
denoted	O
by	O
(	O
)	O
,	O
while	O
a	O
tuple	B-Application
with	O
a	O
single	O
value	O
can	O
be	O
created	O
with	O
(	O
1	O
,	O
)	O
.	O
</s>
<s>
Sets	O
(	O
class	O
set	O
)	O
are	O
mutable	B-Application
containers	B-Application
of	O
hashable	O
items	O
of	O
arbitrary	O
types	O
,	O
with	O
no	O
duplicates	O
.	O
</s>
<s>
Python	B-Language
sets	O
are	O
very	O
much	O
like	O
mathematical	O
sets	O
,	O
and	O
support	O
operations	O
like	O
set	O
intersection	O
and	O
union	O
.	O
</s>
<s>
Python	B-Language
also	O
features	O
a	O
frozenset	O
class	O
for	O
immutable	B-Application
sets	O
,	O
see	O
Collection	O
types	O
.	O
</s>
<s>
Dictionaries	O
(	O
class	O
dict	O
)	O
are	O
mutable	B-Application
mappings	O
tying	O
keys	O
and	O
corresponding	O
values	O
.	O
</s>
<s>
Python	B-Language
includes	O
the	O
+	O
,	O
-	O
,	O
*	O
,	O
/	O
(	O
"	O
true	O
division	O
"	O
)	O
,	O
//	O
(	O
floor	O
division	O
)	O
,	O
%	O
(	O
modulus	O
)	O
,	O
and	O
**	O
(	O
exponentiation	O
)	O
operators	O
,	O
with	O
their	O
usual	O
mathematical	O
precedence	O
.	O
</s>
<s>
In	O
Python	B-Language
3	O
,	O
x	O
/	O
y	O
performs	O
"	O
true	O
division	O
"	O
,	O
meaning	O
that	O
it	O
always	O
returns	O
a	O
float	B-Algorithm
,	O
even	O
if	O
both	O
x	O
and	O
y	O
are	O
integers	O
that	O
divide	O
evenly	O
.	O
</s>
<s>
In	O
Python	B-Language
2	O
(	O
and	O
most	O
other	O
programming	O
languages	O
)	O
,	O
unless	O
explicitly	O
requested	O
,	O
x	O
/	O
y	O
performed	O
integer	O
division	O
,	O
returning	O
a	O
float	B-Algorithm
only	O
if	O
either	O
input	O
was	O
a	O
float	B-Algorithm
.	O
</s>
<s>
However	O
,	O
because	O
Python	B-Language
is	O
a	O
dynamically-typed	O
language	O
,	O
it	O
was	O
not	O
always	O
possible	O
to	O
tell	O
which	O
operation	O
was	O
being	O
performed	O
,	O
which	O
often	O
led	O
to	O
subtle	O
bugs	O
,	O
thus	O
prompting	O
the	O
introduction	O
of	O
the	O
//	O
operator	O
and	O
the	O
change	O
in	O
semantics	O
of	O
the	O
/	O
operator	O
in	O
Python	B-Language
3	O
.	O
</s>
<s>
In	O
Python	B-Language
3	O
,	O
disparate	O
types	O
(	O
such	O
as	O
a	O
str	O
and	O
an	O
int	O
)	O
do	O
not	O
have	O
a	O
consistent	O
relative	O
ordering	O
.	O
</s>
<s>
While	O
it	O
was	O
possible	O
to	O
compare	O
whether	O
some	O
string	O
was	O
greater-than	O
or	O
less-than	O
some	O
integer	O
in	O
Python	B-Language
2	O
,	O
this	O
was	O
considered	O
a	O
historical	O
design	O
quirk	O
and	O
was	O
ultimately	O
removed	O
in	O
Python	B-Language
3	O
.	O
</s>
<s>
Chained	O
comparison	O
expressions	O
such	O
as	O
a	O
<	O
b	O
<	O
c	B-Language
have	O
roughly	O
the	O
meaning	O
that	O
they	O
have	O
in	O
mathematics	O
,	O
rather	O
than	O
the	O
unusual	O
meaning	O
found	O
in	O
C	B-Language
and	O
similar	O
languages	O
.	O
</s>
<s>
The	O
operation	O
has	O
short-circuit	O
semantics	O
,	O
meaning	O
that	O
evaluation	O
is	O
guaranteed	O
to	O
stop	O
as	O
soon	O
as	O
a	O
verdict	O
is	O
clear	O
:	O
if	O
a	O
<	O
b	O
is	O
false	O
,	O
c	B-Language
is	O
never	O
evaluated	O
as	O
the	O
expression	O
cannot	O
possibly	O
be	O
true	O
anymore	O
.	O
</s>
<s>
For	O
expressions	O
without	O
side	O
effects	O
,	O
a	O
<	O
b	O
<	O
c	B-Language
is	O
equivalent	O
to	O
a	O
<	O
b	O
and	O
b	O
<	O
c	B-Language
.	O
However	O
,	O
there	O
is	O
a	O
substantial	O
difference	O
when	O
the	O
expressions	O
have	O
side	O
effects	O
.	O
</s>
<s>
In	O
all	O
versions	O
of	O
Python	B-Language
,	O
boolean	O
operators	O
treat	O
zero	O
values	O
or	O
empty	O
values	O
such	O
as	O
""	O
,	O
0	O
,	O
None	O
,	O
0.0	O
,	O
[],	O
and	O
 {  } 	O
as	O
false	O
,	O
while	O
in	O
general	O
treating	O
non-empty	O
,	O
non-zero	O
values	O
as	O
true	O
.	O
</s>
<s>
The	O
boolean	O
values	O
True	O
and	O
False	O
were	O
added	O
to	O
the	O
language	O
in	O
Python	B-Language
2.2.1	O
as	O
constants	O
(	O
subclassed	O
from	O
1	O
and	O
0	O
)	O
and	O
were	O
changed	O
to	O
be	O
full	O
blown	O
keywords	O
in	O
Python	B-Language
3	O
.	O
</s>
<s>
As	O
mentioned	O
above	O
,	O
another	O
strength	O
of	O
Python	B-Language
is	O
the	O
availability	O
of	O
a	O
functional	B-Language
programming	I-Language
style	O
.	O
</s>
<s>
One	O
such	O
construction	O
is	O
the	O
list	B-Language
comprehension	I-Language
,	O
which	O
can	O
be	O
expressed	O
with	O
the	O
following	O
format	O
:	O
</s>
<s>
Using	O
list	B-Language
comprehension	I-Language
to	O
calculate	O
the	O
first	O
five	O
powers	O
of	O
two	O
:	O
</s>
<s>
The	O
Quicksort	B-Algorithm
algorithm	O
can	O
be	O
expressed	O
elegantly	O
(	O
albeit	O
inefficiently	O
)	O
using	O
list	B-Language
comprehensions	I-Language
:	O
</s>
<s>
Python	B-Language
2.7	O
+	O
also	O
supports	O
set	O
comprehensions	O
and	O
dictionary	O
comprehensions	O
.	O
</s>
<s>
In	O
Python	B-Language
,	O
functions	O
are	O
first-class	B-Application
objects	B-Language
that	O
can	O
be	O
created	O
and	O
passed	O
around	O
dynamically	O
.	O
</s>
<s>
Python	B-Language
's	O
limited	O
support	O
for	O
anonymous	B-General_Concept
functions	I-General_Concept
is	O
the	O
lambda	B-General_Concept
construct	O
.	O
</s>
<s>
An	O
example	O
is	O
the	O
anonymous	B-General_Concept
function	I-General_Concept
which	O
squares	O
its	O
input	O
,	O
called	O
with	O
the	O
argument	O
of	O
5	O
:	O
</s>
<s>
Lambdas	B-General_Concept
are	O
limited	O
to	O
containing	O
an	O
expression	O
rather	O
than	O
statements	O
,	O
although	O
control	O
flow	O
can	O
still	O
be	O
implemented	O
less	O
elegantly	O
within	O
lambda	B-General_Concept
by	O
using	O
short-circuiting	O
,	O
and	O
more	O
idiomatically	O
with	O
conditional	O
expressions	O
.	O
</s>
<s>
Python	B-Language
has	O
had	O
support	O
for	O
lexical	B-Language
closures	I-Language
since	O
version	O
2.2	O
.	O
</s>
<s>
Here	O
's	O
an	O
example	O
function	O
that	O
returns	O
a	O
function	O
that	O
approximates	B-Algorithm
the	I-Algorithm
derivative	I-Algorithm
of	O
the	O
given	O
function	O
:	O
</s>
<s>
Python	B-Language
's	O
syntax	O
,	O
though	O
,	O
sometimes	O
leads	O
programmers	O
of	O
other	O
languages	O
to	O
think	O
that	O
closures	B-Language
are	O
not	O
supported	O
.	O
</s>
<s>
Variable	B-Language
scope	I-Language
in	O
Python	B-Language
is	O
implicitly	O
determined	O
by	O
the	O
scope	O
in	O
which	O
one	O
assigns	O
a	O
value	O
to	O
the	O
variable	O
,	O
unless	O
scope	O
is	O
explicitly	O
declared	O
with	O
global	O
or	O
nonlocal	O
.	O
</s>
<s>
Note	O
that	O
the	O
closure	B-Language
's	O
binding	O
of	O
a	O
name	O
to	O
some	O
value	O
is	O
not	O
mutable	B-Application
from	O
within	O
the	O
function	O
.	O
</s>
<s>
and	O
you	O
can	O
see	O
that	O
b	O
,	O
as	O
visible	O
from	O
the	O
closure	B-Language
's	O
scope	O
,	O
retains	O
the	O
value	O
it	O
had	O
;	O
the	O
changed	O
binding	O
of	O
b	O
inside	O
the	O
inner	O
function	O
did	O
not	O
propagate	O
out	O
.	O
</s>
<s>
In	O
Python	B-Language
2	O
(	O
which	O
lacks	O
nonlocal	O
)	O
,	O
the	O
usual	O
workaround	O
is	O
to	O
use	O
a	O
mutable	B-Application
value	O
and	O
change	O
that	O
value	O
,	O
not	O
the	O
binding	O
.	O
</s>
<s>
Introduced	O
in	O
Python	B-Language
2.2	O
as	O
an	O
optional	O
feature	O
and	O
finalized	O
in	O
version	O
2.3	O
,	O
generators	O
are	O
Python	B-Language
's	O
mechanism	O
for	O
lazy	O
evaluation	O
of	O
a	O
function	O
that	O
would	O
otherwise	O
return	O
a	O
space-prohibitive	O
or	O
computationally	O
intensive	O
list	O
.	O
</s>
<s>
The	O
definition	O
of	O
a	O
generator	O
appears	O
identical	O
to	O
that	O
of	O
a	O
function	O
,	O
except	O
the	O
keyword	O
yield	O
is	O
used	O
in	B-Algorithm
place	I-Algorithm
of	O
return	O
.	O
</s>
<s>
A	O
generator	O
call	O
can	O
then	O
be	O
used	O
in	B-Algorithm
place	I-Algorithm
of	O
a	O
list	O
,	O
or	O
other	O
structure	O
whose	O
elements	O
will	O
be	O
iterated	O
over	O
.	O
</s>
<s>
Introduced	O
in	O
Python	B-Language
2.4	O
,	O
generator	B-Language
expressions	I-Language
are	O
the	O
lazy	O
evaluation	O
equivalent	O
of	O
list	B-Language
comprehensions	I-Language
.	O
</s>
<s>
In	O
contrast	O
,	O
a	O
list	B-Language
comprehension	I-Language
is	O
functionally	O
equivalent	O
,	O
but	O
is	O
greedy	O
in	O
performing	O
all	O
the	O
work	O
:	O
</s>
<s>
The	O
list	B-Language
comprehension	I-Language
will	O
immediately	O
create	O
a	O
large	O
list	O
(	O
with	O
78498	O
items	O
,	O
in	O
the	O
example	O
,	O
but	O
transiently	O
creating	O
a	O
list	O
of	O
primes	O
under	O
two	O
million	O
)	O
,	O
even	O
if	O
most	O
elements	O
are	O
never	O
accessed	O
.	O
</s>
<s>
While	O
lists	O
and	O
generators	O
had	O
comprehensions/expressions	O
,	O
in	O
Python	B-Language
versions	O
older	O
than	O
2.7	O
the	O
other	O
Python	B-Language
built-in	O
collection	O
types	O
(	O
dicts	O
and	O
sets	O
)	O
had	O
to	O
be	O
kludged	O
in	O
using	O
lists	O
or	O
generators	O
:	O
</s>
<s>
Python	B-Language
2.7	O
and	O
3.0	O
unified	O
all	O
collection	O
types	O
by	O
introducing	O
dictionary	O
and	O
set	O
comprehensions	O
,	O
similar	O
to	O
list	B-Language
comprehensions	I-Language
:	O
</s>
<s>
Python	B-Language
supports	O
most	O
object	B-Language
oriented	I-Language
programming	I-Language
(	O
OOP	O
)	O
techniques	O
.	O
</s>
<s>
It	O
allows	O
polymorphism	B-Application
,	O
not	O
only	O
within	O
a	O
class	B-Application
hierarchy	I-Application
but	O
also	O
by	O
duck	B-Application
typing	I-Application
.	O
</s>
<s>
And	O
everything	O
in	O
Python	B-Language
is	O
an	O
object	O
,	O
including	O
classes	O
,	O
functions	O
,	O
numbers	O
and	O
modules	O
.	O
</s>
<s>
Python	B-Language
also	O
has	O
support	O
for	O
metaclasses	B-Language
,	O
an	O
advanced	O
tool	O
for	O
enhancing	O
classes	O
 '	O
functionality	O
.	O
</s>
<s>
Naturally	O
,	O
inheritance	B-Language
,	O
including	O
multiple	B-Application
inheritance	I-Application
,	O
is	O
supported	O
.	O
</s>
<s>
Python	B-Language
has	O
very	O
limited	O
support	O
for	O
private	O
variables	O
using	O
name	O
mangling	O
which	O
is	O
rarely	O
used	O
in	O
practice	O
as	O
information	B-Application
hiding	I-Application
is	O
seen	O
by	O
some	O
as	O
unpythonic	O
,	O
in	O
that	O
it	O
suggests	O
that	O
the	O
class	O
in	O
question	O
contains	O
unaesthetic	O
or	O
ill-planned	O
internals	O
.	O
</s>
<s>
doctrines	O
such	O
as	O
the	O
use	O
of	O
accessor	O
methods	O
to	O
read	O
data	O
members	O
are	O
not	O
enforced	O
in	O
Python	B-Language
.	O
</s>
<s>
Just	O
as	O
Python	B-Language
offers	O
functional-programming	O
constructs	O
but	O
does	O
not	O
attempt	O
to	O
demand	O
referential	O
transparency	O
,	O
it	O
offers	O
an	O
object	B-Language
system	I-Language
but	O
does	O
not	O
demand	O
behavior	O
.	O
</s>
<s>
In	O
version	O
2.2	O
of	O
Python	B-Language
,	O
"	O
new-style	O
"	O
classes	O
were	O
introduced	O
.	O
</s>
<s>
With	O
new-style	O
classes	O
,	O
objects	B-Language
and	O
types	O
were	O
unified	O
,	O
allowing	O
the	O
subclassing	O
of	O
types	O
.	O
</s>
<s>
This	O
allows	O
for	O
many	O
radical	O
things	O
to	O
be	O
done	O
syntactically	O
within	O
Python	B-Language
.	O
</s>
<s>
A	O
new	O
for	O
multiple	B-Application
inheritance	I-Application
was	O
also	O
adopted	O
with	O
Python	B-Language
2.3	O
.	O
</s>
<s>
It	O
is	O
also	O
possible	O
to	O
run	O
custom	O
code	O
while	O
accessing	O
or	O
setting	O
attributes	O
,	O
though	O
the	O
details	O
of	O
those	O
techniques	O
have	O
evolved	O
between	O
Python	B-Language
versions	O
.	O
</s>
<s>
Python	B-Language
allows	O
the	O
creation	O
of	O
class	B-Application
methods	I-Application
and	O
static	O
methods	O
via	O
the	O
use	O
of	O
the	O
@classmethod	O
and	O
@staticmethod	O
decorators	O
.	O
</s>
<s>
The	O
first	O
argument	O
to	O
a	O
class	B-Application
method	I-Application
is	O
the	O
class	O
object	O
instead	O
of	O
the	O
self-reference	O
to	O
the	O
instance	O
.	O
</s>
<s>
Python	B-Language
supports	O
(	O
and	O
extensively	O
uses	O
)	O
exception	B-General_Concept
handling	I-General_Concept
as	O
a	O
means	O
of	O
testing	O
for	O
error	O
conditions	O
and	O
other	O
"	O
exceptional	O
"	O
events	O
in	O
a	O
program	O
.	O
</s>
<s>
Python	B-Language
style	O
calls	O
for	O
the	O
use	O
of	O
exceptions	O
whenever	O
an	O
error	O
condition	O
might	O
arise	O
.	O
</s>
<s>
Rather	O
than	O
testing	O
for	O
access	O
to	O
a	O
file	O
or	O
resource	O
before	O
actually	O
using	O
it	O
,	O
it	O
is	O
conventional	O
in	O
Python	B-Language
to	O
just	O
go	O
ahead	O
and	O
try	O
to	O
use	O
it	O
,	O
catching	O
the	O
exception	O
if	O
access	O
is	O
rejected	O
.	O
</s>
<s>
For	O
instance	O
,	O
the	O
Mailman	B-Application
mailing	O
list	O
software	O
,	O
written	O
in	O
Python	B-Language
,	O
uses	O
exceptions	O
to	O
jump	O
out	O
of	O
deeply	O
nested	O
message-handling	O
logic	O
when	O
a	O
decision	O
has	O
been	O
made	O
to	O
reject	O
a	O
message	O
or	O
hold	O
it	O
for	O
moderator	O
approval	O
.	O
</s>
<s>
Exceptions	O
are	O
often	O
used	O
as	O
an	O
alternative	O
to	O
the	O
if-block	O
,	O
especially	O
in	O
threaded	B-Operating_System
situations	O
.	O
</s>
<s>
The	O
Python	B-Language
can	O
be	O
used	O
in	O
specific	O
cases	O
to	O
determine	O
performance	O
characteristics	O
.	O
</s>
<s>
If	O
exceptional	O
cases	O
are	O
rare	O
,	O
then	O
the	O
EAFP	O
version	O
will	O
have	O
superior	O
average	B-General_Concept
performance	I-General_Concept
than	O
the	O
alternative	O
.	O
</s>
<s>
In	O
addition	O
,	O
it	O
avoids	O
the	O
whole	O
class	O
of	O
time-of-check-to-time-of-use	B-Error_Name
(	O
TOCTTOU	B-Error_Name
)	O
vulnerabilities	O
,	O
other	O
race	B-Operating_System
conditions	I-Operating_System
,	O
and	O
is	O
compatible	O
with	O
duck	B-Application
typing	I-Application
.	O
</s>
<s>
A	O
drawback	O
of	O
EAFP	O
is	O
that	O
it	O
can	O
be	O
used	O
only	O
with	O
statements	O
;	O
an	O
exception	O
cannot	O
be	O
caught	O
in	O
a	O
generator	B-Language
expression	I-Language
,	O
list	B-Language
comprehension	I-Language
,	O
or	O
lambda	B-General_Concept
function	I-General_Concept
.	O
</s>
<s>
Python	B-Language
has	O
two	O
ways	O
to	O
annotate	O
Python	B-Language
code	I-Language
.	O
</s>
<s>
Single-line	O
comments	O
begin	O
with	O
the	O
hash	B-Error_Name
character	O
( #	O
)	O
and	O
continue	O
until	O
the	O
end	O
of	O
the	O
line	O
.	O
</s>
<s>
Docstrings	B-Language
(	O
documentation	B-Language
strings	I-Language
)	O
,	O
that	O
is	O
,	O
strings	O
that	O
are	O
located	O
alone	O
without	O
assignment	O
as	O
the	O
first	O
indented	O
line	O
within	O
a	O
module	O
,	O
class	O
,	O
method	O
or	O
function	O
,	O
automatically	O
set	O
their	O
contents	O
as	O
an	O
attribute	O
named	O
__doc__	O
,	O
which	O
is	O
intended	O
to	O
store	O
a	O
human-readable	O
description	O
of	O
the	O
object	O
's	O
purpose	O
,	O
behavior	O
,	O
and	O
usage	O
.	O
</s>
<s>
Such	O
strings	O
can	O
be	O
delimited	O
with	O
"	O
or	O
'	O
for	O
single	O
line	O
strings	O
,	O
or	O
may	O
span	O
multiple	O
lines	O
if	O
delimited	O
with	O
either	O
"""	O
or	O
'''	O
which	O
is	O
Python	B-Language
's	O
notation	O
for	O
specifying	O
multi-line	O
strings	O
.	O
</s>
<s>
However	O
,	O
the	O
style	O
guide	O
for	O
the	O
language	O
specifies	O
that	O
triple	O
double	O
quotes	O
("""	O
)	O
are	O
preferred	O
for	O
both	O
single	O
and	O
multi-line	O
docstrings	B-Language
.	O
</s>
<s>
Single-line	O
docstring	B-Language
:	O
</s>
<s>
Multi-line	O
docstring	B-Language
:	O
</s>
<s>
Docstrings	B-Language
can	O
be	O
as	O
large	O
as	O
the	O
programmer	O
wants	O
and	O
contain	O
line	O
breaks	O
.	O
</s>
<s>
In	O
contrast	O
with	O
comments	O
,	O
docstrings	B-Language
are	O
themselves	O
Python	B-Language
objects	B-Language
and	O
are	O
part	O
of	O
the	O
interpreted	B-Application
code	O
that	O
Python	B-Language
runs	O
.	O
</s>
<s>
That	O
means	O
that	O
a	O
running	O
program	O
can	O
retrieve	O
its	O
own	O
docstrings	B-Language
and	O
manipulate	O
that	O
information	O
,	O
but	O
the	O
normal	O
usage	O
is	O
to	O
give	O
other	O
programmers	O
information	O
about	O
how	O
to	O
invoke	O
the	O
object	O
being	O
documented	O
in	O
the	O
docstring	B-Language
.	O
</s>
<s>
There	O
are	O
tools	O
available	O
that	O
can	O
extract	O
the	O
docstrings	B-Language
from	O
Python	B-Language
code	I-Language
and	O
generate	O
documentation	O
.	O
</s>
<s>
Docstring	B-Language
documentation	O
can	O
also	O
be	O
accessed	O
from	O
the	O
interpreter	B-Application
with	O
the	O
help( )	O
function	O
,	O
or	O
from	O
the	O
shell	O
with	O
the	O
pydoc	B-Protocol
command	O
pydoc	B-Protocol
.	O
</s>
<s>
The	O
doctest	B-Application
standard	O
module	O
uses	O
interactions	O
copied	O
from	O
Python	B-Language
shell	O
sessions	O
into	O
docstrings	B-Language
to	O
create	O
tests	O
,	O
whereas	O
the	O
module	O
uses	O
them	O
to	O
define	O
command-line	O
options	O
.	O
</s>
<s>
A	O
decorator	O
is	O
any	O
callable	O
Python	B-Language
object	O
that	O
is	O
used	O
to	O
modify	O
a	O
function	O
,	O
method	O
or	O
class	O
definition	O
.	O
</s>
<s>
Python	B-Language
decorators	O
were	O
inspired	O
in	O
part	O
by	O
Java	B-Language
annotations	I-Language
,	O
and	O
have	O
a	O
similar	O
syntax	O
;	O
the	O
decorator	O
syntax	O
is	O
pure	O
syntactic	O
sugar	O
,	O
using	O
@	O
as	O
the	O
keyword	O
:	O
</s>
<s>
Decorators	O
are	O
a	O
form	O
of	O
metaprogramming	B-Application
;	O
they	O
enhance	O
the	O
action	O
of	O
the	O
function	O
or	O
method	O
they	O
decorate	O
.	O
</s>
<s>
Canonical	O
uses	O
of	O
function	O
decorators	O
are	O
for	O
creating	O
class	B-Application
methods	I-Application
or	O
static	O
methods	O
,	O
adding	O
function	O
attributes	O
,	O
tracing	O
,	O
setting	O
pre	O
-	O
and	O
postconditions	O
,	O
and	O
synchronization	O
,	O
but	O
can	O
be	O
used	O
for	O
far	O
more	O
,	O
including	O
tail	B-Language
recursion	I-Language
elimination	I-Language
,	O
memoization	O
and	O
even	O
improving	O
the	O
writing	O
of	O
other	O
decorators	O
.	O
</s>
<s>
In	O
the	O
example	O
above	O
,	O
the	O
favourite_colour	O
decorator	O
factory	B-Application
takes	O
an	O
argument	O
.	O
</s>
<s>
Closure	B-Language
ensures	O
that	O
the	O
colour	O
argument	O
is	O
accessible	O
to	O
the	O
innermost	O
wrapper	O
function	O
even	O
when	O
it	O
is	O
returned	O
and	O
goes	O
out	O
of	O
scope	O
,	O
which	O
is	O
what	O
allows	O
decorators	O
to	O
work	O
.	O
</s>
<s>
Despite	O
the	O
name	O
,	O
Python	B-Language
decorators	O
are	O
not	O
an	O
implementation	O
of	O
the	O
decorator	O
pattern	O
.	O
</s>
<s>
The	O
decorator	O
pattern	O
is	O
a	O
design	O
pattern	O
used	O
in	O
statically-typed	O
object-oriented	B-Language
programming	I-Language
languages	I-Language
to	O
allow	O
functionality	O
to	O
be	O
added	O
to	O
objects	B-Language
at	O
run	O
time	O
;	O
Python	B-Language
decorators	O
add	O
functionality	O
to	O
functions	O
and	O
methods	O
at	O
definition	O
time	O
,	O
and	O
thus	O
are	O
a	O
higher-level	O
construct	O
than	O
decorator-pattern	O
classes	O
.	O
</s>
<s>
The	O
decorator	O
pattern	O
itself	O
is	O
trivially	O
implementable	O
in	O
Python	B-Language
,	O
because	O
the	O
language	O
is	O
duck	B-Application
typed	I-Application
,	O
and	O
so	O
is	O
not	O
usually	O
considered	O
as	O
such	O
.	O
</s>
<s>
Users	O
of	O
curly	O
bracket	O
languages	O
,	O
such	O
as	O
C	B-Language
or	O
Java	B-Language
,	O
sometimes	O
expect	O
or	O
wish	O
Python	B-Language
to	O
follow	O
a	O
block-delimiter	O
convention	O
.	O
</s>
<s>
The	O
Python	B-Language
interpreter	B-Application
contains	O
an	O
easter	O
egg	O
that	O
summarizes	O
its	O
developers	O
 '	O
feelings	O
on	O
this	O
issue	O
.	O
</s>
<s>
The	O
__future__	O
module	O
is	O
normally	O
used	O
to	O
provide	O
features	O
from	O
future	O
versions	O
of	O
Python	B-Language
.	O
</s>
<s>
Another	O
hidden	O
message	O
,	O
the	O
Zen	O
of	O
Python	B-Language
(	O
a	O
summary	O
of	O
Python	B-Language
design	O
philosophy	O
)	O
,	O
is	O
displayed	O
when	O
trying	O
to	O
import	O
this	O
.	O
</s>
<s>
In	O
Python	B-Language
2.7	O
,	O
instead	O
of	O
Hello	O
world	O
!	O
</s>
<s>
Importing	O
the	O
antigravity	O
module	O
opens	O
a	O
web	O
browser	O
to	O
xkcd	O
comic	O
that	O
portrays	O
a	O
humorous	O
fictional	O
use	O
for	O
such	O
a	O
module	O
,	O
intended	O
to	O
demonstrate	O
the	O
ease	O
with	O
which	O
Python	B-Language
modules	O
enable	O
additional	O
functionality	O
.	O
</s>
<s>
In	O
Python	B-Language
3	O
,	O
this	O
module	O
also	O
contains	O
an	O
implementation	O
of	O
the	O
"	O
geohash	O
"	O
algorithm	O
,	O
a	O
reference	O
to	O
xkcd	O
comic	O
.	O
</s>
