The Essence of Python

advertisement
The Essence of Python
Objects, Values, Types, and Bindings
Everything is an object.
Every object has
a value.
a type.
an identity.
a namespace.
CS105 Python
4
Spring 2009
Everything is an object.
CS105 Python
5
Spring 2009
value
1
value
2
Every object has a value.
value -0.33
CS105 Python
value 100.0
6
Spring 2009
Numbers
Name (type)
Integer (int)
Floating-point (float)
Values
...,-1, 0, 1, ...
-1.3, 100.777
Operations
arithmetic: +, -, *, **, /, //, %
comparison: ==, !=, >, >=, <, <=
CS105 Python
7
Spring 2009
value
1
value
type int
2
type int
Every object has a type.
value -0.33
type float
CS105 Python
value 100.0
type float
8
Spring 2009
Inspecting Types
>>> type(1)
<type 'int'>
>>>
>>> type(1.0)
<type 'float'>
CS105 Python
9
Spring 2009
Truth
Name (type)
Boolean (bool)
Values
True, False
Operations
CS105 Python
logical: and, or, not
10
Spring 2009
Bindings
global namespace
Python 2.6.1 ...
Type "help", ...
>>>
CS105 Python
12
Spring 2009
Bindings
global namespace
x
CS105 Python
Python 2.6.1 ...
Type "help", ...
>>>
>>> x = 1
1
type
int
13
Spring 2009
Bindings
global namespace
Python 2.6.1 ...
Type "help", ...
>>>
>>> x = 1
>>>
>>> x = True
x
True
type bool
CS105 Python
X
Spring 2009
Strings
Name (type)
String (str)
Values
'hi',
"hi",
'''Hello''',
"""That's all,
folks"""
Operations
CS105 Python
construction +, *
comparison: ==, !=, >, >=, <, <=
membership: in
built-in: len(str), and more...
15
Spring 2009
Sequences
Name (type)
List (list)
Values
[], [1], ['a', 'b']
Operations
CS105 Python
access: list[index]
construction +, *
comparison: ==, !=, >, >=, <, <=
membership: in
built-in: len(list), and more...
16
Spring 2009
Slicing A Sequence
seq[start : end : step]
0 1
10
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
-10
-2
-1
think: [start, end)
CS105 Python
17
Spring 2009
value
1
type
int
id 8405384
value
'a'
type
str
id 269440
Every object has an identity.
value [1,2,3]
type
list
id 4161168
CS105 Python
18
Spring 2009
Inspecting Identities
>>> x = [1,2,3]
>>> id(x)
4160448
>>>
>>> y = x
>>> id(y)
4160448
CS105 Python
19
Spring 2009
Identity and Equality
(== vs is)
>>> x
>>> y
>>> y
True
>>> y
True
>>>
>>> y
>>> y
True
>>> y
False
== compares values.
is compares identities.
CS105 Python
20
= [1,2,3]
= x
== x
is x
= [1,2,3]
== x
is x
Spring 2009
More Sequences
Name (type)
Tuple (tuple)
Values
(), (1,), ('Williams', 1941, .406)
Operations
CS105 Python
access: tuple[index]
construction +, *
comparison: ==, !=, >, >=, <, <=
membership: in
built-in: len(tuple), and more...
21
Spring 2009
Usage Patterns
Use list as a collection
Use tuple as a record
•Values have same type
•Values may have different types
•Values change over time
•Values constant over time
CS105 Python
22
Spring 2009
Mutability
The value of a mutable type may change.
The value of an immutable type cannot change.
Mutable types
Immutable types
int
float
str
tuple
...
list
...
CS105 Python
24
Spring 2009
Mapping
Name (type)
Dictionary (dict)
Values
{}, {1:'one'}, {1:'one', 2:'two'}
Operations
CS105 Python
Mu
access: dict[key]
imm st be
utab
an
l
e
removal: del dict[key]
valu
e
key membership: in
dict.keys(), dict.items(),
built-in:
dict.values(), and more...
25
Spring 2009
value
type
id
'a'
str
269440
name
value
namespace 'upper'
...
...
...
Every object has a namespace.
value
type
id
namespace
CS105 Python
{}
dict
515072
name
value
'keys'
...
...
...
26
Spring 2009
Inspecting Namespaces
>>> dir({})
['__class__', '__cmp__', '__contains__', '__delattr__',
'__delitem__', '__doc__', '__eq__', '__format__',
'__ge__', '__getattribute__', '__getitem__', '__gt__',
'__hash__', '__init__', '__iter__', '__le__', '__len__',
'__lt__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__',
'__setitem__', '__sizeof__', '__str__',
'__subclasshook__', 'clear', 'copy', 'fromkeys', 'get',
'has_key', 'items', 'iteritems', 'iterkeys',
'itervalues', 'keys', 'pop', 'popitem', 'setdefault',
'update', 'values']
CS105 Python
27
Spring 2009
Everything is an object.
Every object has
a value.
a type.
an identity.
a namespace.
CS105 Python
29
Spring 2009
Download