- Official Documentation (Language Reference, Library Reference).
- Wikipedia, Python syntax and semantics.
- O'Reilly Python Pocket Reference, 5th ed. (Safari)
You can also get interactive documentation using the help()
function:
help()
: Start interactive help system.help(object)
: Display docstring(s) for given object.help('topics')
: Display help topics. Individual topics listed must be given as strings in the same (upper) case.help('keywords')
,help('symbols')
help('modules')
: List all modules; can take a very long time. Usehelp('modules foo')
to search for modules containingfoo
in the name or description, or display information about modulefoo
.help(s)
: Wheres
is'modules'
,'keywords'
,'symbols'
.
- Use the ipython REPL to experiment.
- Use virtual environments to experiment with packages.
Python variables are untyped, holding references to typed values which
are always objects. Each object has a namespace,
a dictionary of name-value bindings called attributes, usually
accessed as obj.attr
. Attribute access can be modified 'under the
hood' with descriptors.
There is limited implicit conversation, mainly between numeric types and any type to boolean.
Functions are first-class objects like any other.
f(x) syntax invokes the __call__
attribute on the object (which
must be a function itself), passing the parameters x
to it.
Scripts are executed statement by statement, updating the interpreter state as they go. Thus, bindings must come before use.
All statments (which includes expressions) run in the context of a
module (__main__
by default); that module's namespace is referred
to as the "global" namespace for that statement.
-
Loading a module creates a new namespace that will be the global one for the code in that module.
-
Defining a class creates a new namespace that will be the local namespace for the code defining the class. The code in the class definition is executed immediately in ithat local namespace (which shadows the module namespace): this is implemented by storing new bindings in the class object's attribute dictionary.
-
Function definitions are executable statements that compile and store the function's code in a (callable) function object, binding it to a variable that is the function name. (Expressions in the function declaration, i.e. arguments, are executed at the time of definition, not at the time of the call.) Calling a function creates a new local namespace used by the code run during that function call.
-
Blocks (see below) do not introduce new namespaces.
(Also see Hashes and Equality.)
Some objects in Python, such as int
and str
are immutable, and
thus hashable. dict
and similar can store only hashable objects
because lookup is done based on hash()
.
The contract for a hashable object is:
- Hashable objects are "immutable" in some sense;
hash()
will return the same value for the entire lifetime of the object or throwTypeError
;__eq__()
can be used to distinguish two different objects whose hash collides;hash(x) != hash(y)
impliesx != y
.
"Immutable" collections, such as tuple
are only "shallow immutable";
they are truly immutable (and hash()
works on them) only when they
contain only immutable values. Thus:
hash((1,2)) ⇒ 3713081631934410656
t = (1,[]); t ⇒ (1,[])
t[1].append(2); t ⇒ (1,[2])
hash(t) ⇒ TypeError: unhashable type: 'list'
GvR explains, "Immutability is a property of types, while hashability is a property of values"; @nedbat's corollary: "Immutablity is shallow, hashability is deep." But "this stops being true with a sufficiently good notion of type (or even just the one provided by the typing module)" (@DRMaclver).
For expanded discussion see Python Tuples are Immutable, Except When They're Mutable and There's Immutable And Then There's Immutable.
- Also see Python reference documentation Lexical Analysis.
Statements are normally terminated by semicolon, newline or EOF.
However, newlines will be removed (but replaced by implicit
whitespace) when preceeded by a backslash \
(excepting comments) or
not all opening ([{
have a matching close. Indentation on lines
continued from ([{
is ignored. It's fine form to enclose a full
expression in parens to take advantage of this: x = ( 2 + 2 )
.
Indenting introduces a new block that closes at the next dedent.
Statements that introduce blocks (def
, if
, else
, etc.) are
followed by a colon and may not be on the same line as another
block-introducing statement. Following the colon may be a list of
semicolon-separated statements on the same line or an indented block
on subsequent lines. E.g.:
if x == 0: pass # Statement must be present; pass does nothing
elif x > 0: foo; bar; baz()
else:
baz(0, 1, 2)
quux('............................................')
In place of the pass
statement you can also use an expression,
usually None
or ...
(Ellipsis literal).
If return
is not used in a function it always returns None
.
lambda: expr
however returns expr
and may not use return
.
- See Types.
Numbers: the usual: 0
, -1
, 3.4
, 3.5e-8
.
String (str
) Literals:
'foo' # C-style backslash sequences interpolated
'''foo''' # newlines are literal
r'foo' # no `\x` interpolation; useful for regexes
'a' "b" # adjacent literals are concatenated
len('a' # 1 # Good for commenting parts of strings.
'b') # 2 # len('ab') => 2
Tuples, Lists, Dictionaries, Sets:
1, 2 # tuple
(1, 2) # tuple
() # empty tuple
[0, 'a', (), []] # list
list({4,2,3}) # list: [2,3,4] (from set; see below)
{ 1:'one', 'two':2 } # dict
{} # empty dict
{ 0, 'a', (), [] } # set
set() # empty set
frozenset(['a',2,'a']) # frozen set: frozenset({2, 'a'})
dir()
returns a list of names in local scope; dir(obj)
returns the
list of obj
's attributes.
str(o)
returns an informal, nicely printable string representation
of an object by calling o.__str__()
. If that's not present, it
returns repr(o)
, which returns the formal string representation,
which either can be passed to eval()
to recreate the object or is of
the form <...description...>
. (It should always unambiguously
describe that particular object.) repr()
returns o.__repr__()
if
present or does its own thing if not.