Transcript Containers
Python Crash Course
Containers
3rd year Bachelors
V1.0
dd 03-09-2013
Hour 3
Introduction to language - containers
Container data types in Python are types whose instances are capable of storing
other objects. Some of the fundamental built-in container objects include:
•lists – the most popular container data type in python; can store any number of any
objects.
•tuples – similar to list, yet once created are immutable,
•sets – can store only unique elements,
•bytes – immutable sequence of integers in the range 0 <= x < 256,
•bytearray – like bytes, but mutable,
•dictionary – also known as associative arrays. They contain mapping of keys into
value,
•str – string, a sequence of unicode characters,
•range – a sequence of numbers — more precisely a list containing arithmetic
progressions
•array.array – present in the array module. Similar to list, yet during the construction
it is restricted to holding a specific data type,
Introduction to language - containers
Data type
Mutable Ordered
Literal example
Constructor
Sequence types
list
yes
yes
[1,2,3]
list()
tuple
no
yes
(1,2,3)
tuple()
str
no
yes
“text” / ‘text’
range
no
yes
–
range()
bytes
no
yes
b’abcde’ / b”abc”
bytes()
bytearray
yes
yes
–
bytearray()
array *
yes
yes
–
array.array()
set
yes
no
{1,2,3}
frozenset
no
no
–
yes
no
{“key1″: “val”, “key2″: “val”}
str()
Set types
set()
frozenset()
Mapping types
dict
dict()
Containers – Mutable, Immutable,
Hashable
Containers in Python can be either mutable or immutable. The fact that a container object is
immutable doesn’t always mean that the objects it holds are also immutable (e.g. an immutable tuple
holding mutable lists). However, container objects are fully immutable only if the object itself, and the
objects it contains are recursively immutable. Recursively immutable objects may be hashable. This
is important as only hashable objects can be used in a mapping container object (see below) as
keys.
All of Python’s immutable built-in objects are hashable, while no mutable containers (such as lists or
dictionaries) are.
Examples of mutable containers include:
•list,
•set,
•dictionary,
•bytearray
•array
Examples of immutable containers include:
•string,
•frozenset,
•tuple,
•bytes
The main implication of the mutable/immutable distinction and hashability is that not all container
object can store all other container objects, in particular:
•sets can store only hashable object (each object in set has to have a unique hash — sets do not
store duplicate objects, as opposed to e.g. lists or tuples)
•dictionaries can have only hashable objects as keys
Containers – Ordered or Unordered
Container object can store their content in either an ordered or unordered manner. Order, or lack of
thereof, is unrelated to the mutability of objects. This means that both mutable and immutable
objects can be either ordered or unordered.
Examples of ordered containers include:
•list,
•string,
•tuple,
•bytes,
•bytearrays,
•array
Examples of unordered containers include:
•dictionary,
•set,
•frozenset.
Containers - Lists
Lists are the most versatile of Python's compound data types. A list contains items separated by commas and
enclosed within square brackets ([]). To some extent, lists are similar to arrays in C. One difference between them is
that all the items belonging to a list can be of different data type. The values stored in a list can be accessed using
the slice operator ( [ ] and [ : ] ) with indexes starting at 0 in the beginning of the list and working their way to end-1.
The plus ( + ) sign is the list concatenation operator, and the asterisk ( * ) is the repetition operator.
Lists:
>>> a = [1, 2, 4, 8, 16] # list of ints
>>> c = [4, 'candles', 4.0, 'handles'] # can mix types
>>> c[1]
'candles'
>>> c[2] = 'knife'
>>> c[-1] # negative indices count from end
'handles'
>>> c[1:3] # slicing
['candles', 'knife']
>>> c[2:] # omitting defaults to start or end
['knife', 'handles']
>>> c[0:4:2] # variable stride (could just write c[::2])
[4, 'knife']
>>> a + c # concatenate
[1, 2, 4, 8, 16, 4, 'candles', 'knife', 'handles']
>>> len(a)
5
Lists Methods
SN Function with Description
SN
Methods with Description
1
list.append(obj)
Appends object obj to list
2
list.count(obj)
Returns count of how many times obj
occurs in list
1
cmp(list1, list2)
Compares elements of both lists.
2
len(list)
Gives the total length of the list.
3
max(list)
Returns item from the list with max value.
3
list.extend(seq)
Appends the contents of seq to list
4
min(list)
Returns item from the list with min value.
4
5
list(seq)
Converts a tuple into list.
list.index(obj)
Returns the lowest index in list that obj
appears
5
list.insert(index, obj)
Inserts object obj into list at offset index
6
list.pop(obj=list[-1])
Removes and returns last object or obj
from list
7
list.remove(obj)
Removes object obj from list
8
list.reverse()
Reverses objects of list in place
9
list.sort([func])
Sorts objects of list, use compare func if
given
List operations
list1 = ['physics', 'chemistry', 1997, 2000]
for item in L:
print list1
print item
del list1[2]
print "After deleting value at index 2 : "
for index, item in enumerate(L):
print list1
print index, item
L = []
#empty list
i = iter(L)
L = list()
item = i.next() # fetch first value
item = i.next() # fetch second value
A = B = [] # both names will point to the same list
A = []
stack = []
B = A # both names will point to the same list
stack.append(object) # push
A = []; B = [] # independent lists
object = stack.pop() # pop from end
queue = []
queue.append(object) # push
object = queue.pop(0) # pop from beginning
Containers - Tuples
A tuple is another sequence data type that is similar to the list. A tuple consists of a number of values separated by
commas. Unlike lists, however, tuples are enclosed within parentheses. The main differences between lists and
tuples are: Lists are enclosed in brackets ( [ ] ), and their elements and size can be changed, while tuples are
enclosed in parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.
Tuples:
>>> q = (1, 2, 4, 8, 16) # tuple of ints
>>> r = (4, 'candles', 4.0, 'handles') # can mix types
>>> s = ('lonely',) # singleton
>>> t = () # empty
>>> r[1]
'candles'
>>> r[2] = 'knife' # cannot change tuples
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
>>> u = 3, 2, 1
# parentheses not necessary
>>> v, w = 'this', 'that'
>>> v
'this'
>>> w
'that'
Use of tuples
def func(x,y):
# code to compute x and y
return (x,y)
(x,y) = func(1,2)
Tuple methods
SN
Function with Description
1
cmp(tuple1, tuple2)
Compares elements of both tuples.
2
len(tuple)
Gives the total length of the tuple.
3
max(tuple)
Returns item from the tuple with max value.
4
min(tuple)
Returns item from the tuple with min value.
5
tuple(seq)
Converts a list into tuple.
Containers - Dictionaries
Python 's dictionaries are hash table type. They work like associative arrays or hashes found in Perl and consist of
key-value pairs. Keys can be almost any Python type, but are usually numbers or strings. Values, on the other hand,
can be any arbitrary Python object. Dictionaries are enclosed by curly braces ( { } ) and values can be assigned and
accessed using square braces ( [] ).
Dictionaries:
>>> a = {'eyecolour': 'blue', 'height': 152.0,
42: 'the answer'}
>>> a['age'] = 28
>>> a
{42: 'the answer', 'age': 28, 'eyecolour': 'blue', 'height': 152.0}
>>> del(a['height'])
>>> a
{42: 'the answer', 'age': 28, 'eyecolour': 'blue'}
>>> b = {}
>>> b['hello'] = 'Hi! '
>>> a.keys()
[42, 'age’, 'eyecolour’]
>>> a.values()
['the answer', 28, 'blue']
Dictionary use
>>> colors = { "blue": (0x30,0x30,0xff), "green": (0x30,0xff,0x97),
... "red": (0xff,0x30,0x97), "yellow": (0xff,0xff,0x30) }
>>> for c in colors:
...
print c, colors[c]
for key, value in someDictionary.items():
# process key and value
print key, "=", value
for value in someDictionary.values():
# process the value
>>> i = { "two":2, "three":3, "quatro":4 }
>>> del i["quatro"]
>>> i
{'two': 2, 'three': 3}
>>> i = { "two":2, "three":3, "quatro":4 }
>>> i.pop("quatro")
4
>>> i
{'two': 2, 'three': 3}
Dictionary methods
SN Function with Description
SN Methods with Description
1
cmp(dict1, dict2)
Compares elements of both dict.
1
dict.clear()
Removes all elements of dictionary dict
2
len(dict)
Gives the total length of the
dictionary. This would be equal to
the number of items in the
dictionary.
2
dict.copy()
Returns a shallow copy of dictionary dict
2
dict.fromkeys()
Create a new dictionary with keys from seq and
values set to value.
3
dict.get(key, default=None)
For key key, returns value or default if key not in
dictionary
4
dict.has_key(key)
Returns true if key in dictionary dict, false otherwise
5
dict.items()
Returns a list of dict's (key, value) tuple pairs
6
dict.keys()
Returns list of dictionary dict's keys
7
dict.setdefault(key, default=None)
Similar to get(), but will set dict[key]=default
if key is not already in dict
8
dict.update(dict2)
Adds dictionary dict2's key-values pairs to dict
9
dict.values()
Returns list of dictionary dict2's values
3
str(dict)
Produces a printable string
representation of a dictionary
4
type(variable)
Returns the type of the passed
variable. If passed variable is
dictionary then it would return a
dictionary type.
Type Conversions
Function
int(x [,base])
Description
Converts x to an integer. base specifies the base if x is a string.
long(x [,base] )
float(x)
Converts x to a long integer. base specifies the base if x is a string.
Converts x to a floating-point number.
complex(real [,imag])
str(x)
Creates a complex number.
Converts object x to a string representation.
repr(x)
eval(str)
Converts object x to an expression string.
Evaluates a string and returns an object.
tuple(s)
list(s)
set(s)
dict(d)
Converts s to a tuple.
Converts s to a list.
Converts s to a set.
Creates a dictionary. d must be a sequence of (key,value) tuples.
frozenset(s)
chr(x)
unichr(x)
Converts s to a frozen set.
Converts an integer to a character.
Converts an integer to a Unicode character.
ord(x)
Converts a single character to its integer value.
hex(x)
oct(x)
Converts an integer to a hexadecimal string.
Converts an integer to an octal string.
Logical Operators
End