Presentation is loading. Please wait.

Presentation is loading. Please wait.

Python Crash Course Containers Bachelors V1.0 dd 13-01-2015 Hour 6.

Similar presentations


Presentation on theme: "Python Crash Course Containers Bachelors V1.0 dd 13-01-2015 Hour 6."— Presentation transcript:

1 Python Crash Course Containers Bachelors V1.0 dd 13-01-2015 Hour 6

2 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,

3 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. Mutable Definition: Mutable objects can change their value but keep their id(). Immutable Definition: An object with a fixed value. Immutable objects include numbers, strings and tuples. Such an object cannot be altered. A new object has to be created if a different value has to be stored. They play an important role in places where a constant hash value is needed, for example as a key in a dictionary. Hashable Definition: An object is hashable if it has a hash value which never changes during its lifetime (it needs a hash()method), and can be compared to other objects (it needs an eq() method). Hashable objects which compare equal must have the same hash value.

4 Containers – Mutable, Immutable, Hashable 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: numbers string, frozenset, tuple, bytes The main implication of the mutable/immutable distinction and hashability is that not all container objects 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

5 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.

6 Introduction to language - containers Data typeMutableOrderedLiteral exampleConstructor Sequence types listyes [1,2,3]list() tuplenoyes(1,2,3)tuple() strnoyes“text” / ‘text’str() rangenoyes–range() bytesnoyesb’abcde’ / b”abc”bytes() bytearrayyes –bytearray() array *yes –array.array() Set types setyesno{1,2,3}set() frozensetno –frozenset() Mapping types dictyesno{“key1″: “val”, “key2″: “val”}dict()

7 Containers - Lists 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 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.

8 Lists Methods SNFunction with Description 1cmp(list1, list2) cmp(list1, list2) Compares elements of both lists. 2len(list) len(list) Gives the total length of the list. 3max(list) max(list) Returns item from the list with max index. 4min(list) min(list) Returns item from the list with min index. 5list(seq) list(seq) Converts a tuple into list. SNMethods with Description 1list.append(obj) list.append(obj) Appends object obj to list 2list.count(obj) list.count(obj) Returns count of how many times obj occurs in list 3list.extend(seq) list.extend(seq) Appends the contents of seq to list 4list.index(obj) list.index(obj) Returns the lowest index in list that obj appears 5list.insert(index, obj) list.insert(index, obj) Inserts object obj into list at offset index 6list.pop(obj=list[-1]) list.pop(obj=list[-1]) Removes and returns last object or obj from list 7list.remove(value) list.remove(value) Removes object obj with value from list 8list.reverse() list.reverse() Reverses objects of list in place 9list.sort([func]) list.sort([func]) Sorts objects of list, use compare func if given

9 List operations list1 = ['physics', 'chemistry', 1997, 2000] print list1 del list1[2] print "After deleting value at index 2 : " print list1 L = [] #empty list L = list() A = B = [] # both names will point to the same list A = [] B = A # both names will point to the same list A = []; B = [] # independent lists for item in L: print item for index, item in enumerate(L): print index, item i = iter(L) item = i.next() # fetch first value item = i.next() # fetch second value stack = [] stack.append(object) # push object = stack.pop() # pop from end queue = [] queue.append(object) # push object = queue.pop(0) # pop from beginning

10 List Comprehension >>> squares = [] >>> for x in range(10):... squares.append(x**2)... >>> squares [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] List comprehensions provide a concise way to create lists. Common applications are to make new lists where each element is the result of some operations applied to each member of another sequence or iterable, or to create a subsequence of those elements that satisfy a certain condition. squares = [x**2 for x in range(10)] >>> matrix = [... [1, 2, 3, 4],... [5, 6, 7, 8],... [9, 10, 11, 12],... ] To transpose this matrix: >>> [[row[i] for row in matrix] for i in range(4)] [[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]] Nested list comprehension

11 Containers - Tuples 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 " ", line 1, in TypeError: 'tuple' object does not support item assignment >>> u = 3, 2, 1 # parentheses not necessary >>> v, w = 'this', 'that' >>> v 'this' >>> w 'that' 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. >>> u = 3, 2,1 >>> print u (3, 2, 1) >>> v= 4,6,7 >>> w=v+u >>> print w (4, 6, 7, 3, 2, 1) >>>

12 Use of tuples Python ExpressionResultsDescription len((1, 2, 3))3Length (1, 2, 3) + (4, 5, 6)(1, 2, 3, 4, 5, 6)Concatenation ('Hi!',) * 4('Hi!', 'Hi!', 'Hi!', 'Hi!')Repetition 3 in (1, 2, 3)TrueMembership for x in (1, 2, 3): print x,1 2 3Iteration def func(x,y): # code to compute x and y return (x,y) (x,y) = func(1,2)

13 Tuple methods SNFunction with Description 1cmp(tuple1, tuple2) cmp(tuple1, tuple2) Compares elements of both tuples. 2len(tuple) len(tuple) Gives the total length of the tuple. 3max(tuple) max(tuple) Returns item from the tuple with max value. 4min(tuple) min(tuple) Returns item from the tuple with min value. 5tuple(seq) tuple(seq) Converts a list into tuple. Note: Tuples have no append or extend methods, nor can you remove items as there are no remove or pop methods either.

14 Containers - Sets A set is an unordered collection with no duplicate elements. Basic uses include membership testing and eliminating duplicate entries. Set objects also support mathematical operations like union, intersection, difference, and symmetric difference. >>> basket = ['apple', 'orange', 'apple', 'pear', 'orange', 'banana'] >>> fruit = set(basket) # create a set without duplicates >>> fruit set(['orange', 'pear', 'apple', 'banana']) >>> 'orange' in fruit # fast membership testing True >>> 'crabgrass' in fruit False >>> x = set("A Python Tutorial") >>> x set(['A', ' ', 'i', 'h', 'l', 'o', 'n', 'P', 'r', 'u', 't', 'a', 'y', 'T']) >>> type(x) >>>

15 Containers - Sets No mutable objects >>> cities = set((["Python","Perl"], ["Paris", "Berlin", "London"])) Traceback (most recent call last): File " ", line 1, in TypeError: unhashable type: 'list' >>> >>> cities = set(["Frankfurt", "Basel","Freiburg"]) >>> cities.add("Strasbourg") >>> cities set(['Freiburg', 'Basel', 'Frankfurt', 'Strasbourg']) >>> But sets are mutable >>> cities = frozenset(["Frankfurt", "Basel","Freiburg"]) >>> cities.add("Strasbourg") Traceback (most recent call last): File " ", line 1, in AttributeError: 'frozenset' object has no attribute 'add' >>> frozensets are immutable

16 Sets - operations No mutable objects >>> a = set('abracadabra') >>> b = set('alacazam') >>> a # unique letters in a set(['a', 'r', 'b', 'c', 'd']) >>> a - b # letters in a but not in b set(['r', 'd', 'b']) >>> a | b # letters in either a or b set(['a', 'c', 'r', 'd', 'b', 'm', 'z', 'l']) >>> a & b # letters in both a and b set(['a', 'c']) >>> a ^ b # letters in a or b but not both set(['r', 'd', 'b', 'm', 'z', 'l']) >>> cities = frozenset(["Frankfurt", "Basel","Freiburg"]) >>> cities.add("Strasbourg") Traceback (most recent call last): File " ", line 1, in AttributeError: 'frozenset' object has no attribute 'add' >>> frozensets are immutable

17 Sets - operations SNMethods with Description 1set.add(obj) set.add(obj) Adds object obj to set 2set.clear(obj) set.clear(obj) removes all objects from a set 3set.copy () set.copy () Creates a shallow copy of a set 4set.difference(set) set.difference(set) Returns the difference of two or more sets as a new set. 5set.difference_uodate(set) set.difference_uodate(set) Removes all elements of another set from this set. x.difference_update() is the same as "x = x - y" 6set.discard(obj) set.discard(obj) Removes obj from the set 7set.remove(value) set.remove(value) Works like discard(), but if el is not a member of the set, a KeyError will be raised. SNMethods with Description 8set.intersection(set) set.intersection(set) Returns the intersection of the instance set and the set s as a new set. In other words: A set with all the elements which are contained in both sets is returned. 9set.isdisjoint(ser) set.isdisjoint(ser) This method returns True if two sets have a null intersection. 10set.issubset(set) set.issubset(set) x.issubset(y) returns True, if x is a subset of y. "<=" is an abbreviation for "Subset of" 11set.issuperset(obj) set.issuperset(obj) x.issuperset(y) returns True, if x is a superset of y. ">=" is an abbreviation for "issuperset of" 12set.pop() set.pop() pop() removes and returns an arbitrary set element. The method raises a KeyError if the set is empty

18 Containers - Dictionaries 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'] 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 ( [] ).

19 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] en_ne = {"red" : "rood", "green" : "groen", "blue" : "blauw", "yellow":"geel"} print en_ne["red"] ne_fr = {"rood" : "rouge", "groen" : "vert", "blauw" : "bleu", "geel":"jaune"} print "The French word for red is: " + ne_fr[en_ne["red"]] >>> dic = { [1,2,3]:"abc"} Traceback (most recent call last): File " ", line 1, in TypeError: list objects are unhashable >>> dic = { (1,2,3):"abc", 3.1415:"abc"} >>> dic {(1, 2, 3): 'abc'} dictionaries = {"en_ne" : en_ne, "ne_fr" : ne_fr } print dictionaries["ne_fr"]["blauw"]

20 Dictionary methods SNFunction with Description 1cmp(dict1, dict2) cmp(dict1, dict2) Compares elements of both dict. 2len(dict) len(dict) Gives the total length of the dictionary. This would be equal to the number of items in the dictionary. 3str(dict) str(dict) Produces a printable string representation of a dictionary 4type(variable) type(variable) Returns the type of the passed variable. If passed variable is dictionary then it would return a dictionary type. SNMethods with Description 1dict.clear() dict.clear() Removes all elements of dictionary dict 2dict.copy() dict.copy() Returns a shallow copy of dictionary dict 2dict.fromkeys() dict.fromkeys() Create a new dictionary with keys from seq and values set to value. 3dict.get(key, default=None) dict.get(key, default=None) For key key, returns value or default if key not in dictionary 4dict.has_key(key) dict.has_key(key) Returns true if key in dictionary dict, false otherwise 5dict.items() dict.items() Returns a list of dict's (key, value) tuple pairs 6dict.keys() dict.keys() Returns list of dictionary dict's keys 7dict.setdefault(key, default=None) dict.setdefault(key, default=None) Similar to get(), but will set dict[key]=default if key is not already in dict 8dict.update(dict2) dict.update(dict2) Adds dictionary dict2's key-values pairs to dict 9dict.values() dict.values() Returns list of dictionary dict2's values

21 Dictionary operations for key, value in someDictionary.items(): # process key and 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} # zipping two lists into a dictionary >>> dishes = ["pizza", "sauerkraut", "paella", "Hamburger"] >>> countries = ["Italy", "Germany", "Spain", "USA"] >>> country_specialities_dict = dict(country_specialities) >>> print country_specialities_dict {'Germany': 'sauerkraut', 'Spain': 'paella', 'Italy': 'pizza', 'USA': 'Hamburger'}

22 Type Conversions FunctionDescription int(x [,base])Converts x to an integer. base specifies the base if x is a string. long(x [,base] )Converts x to a long integer. base specifies the base if x is a string. float(x)Converts x to a floating-point number. complex(real [,imag])Creates a complex number. str(x)Converts object x to a string representation. repr(x)Converts object x to an expression string. eval(str)Evaluates a string and returns an object. tuple(s)Converts s to a tuple. list(s)Converts s to a list. set(s)Converts s to a set. dict(d)Creates a dictionary. d must be a sequence of (key,value) tuples. frozenset(s)Converts s to a frozen set. chr(x)Converts an integer to a character. unichr(x)Converts an integer to a Unicode character. ord(x)Converts a single character to its integer value. hex(x)Converts an integer to a hexadecimal string. oct(x)Converts an integer to an octal string.

23 Shallow and Deep copy # Expected behaviour >> colours1 = ["red", "green"] >>> colours2 = colours1 >>> colours2 = ["rouge", "vert"] >>> print colours1 ['red', 'green'] # Watch OUT! >>> colours1 = ["red", "green"] >>> colours2 = colours1 >>> colours2[1] = "blue" >>> colours1 ['red', 'blue'] Python creates real copies only if it has to, i.e. if the user, the programmer, explicitly demands it. # Force to copy >>> list1 = ['a','b','c','d'] >>> list2 = list1[:] >>> list2[1] = 'x' >>> print list2 ['a', 'x', 'c', 'd'] >>> print list1 ['a', 'b', 'c', 'd'] >>> # But be carefull with sublists! >>> lst1 = ['a','b',['ab','ba']] >>> lst2 = lst1[:] >>> lst2[0] = 'c' >>> lst2[2][1] = 'd' >>> print(lst1) ['a', 'b', ['ab', 'd']]

24 Shallow and Deep copy >>> from copy import deepcopy >>> >>> lst1 = ['a','b',['ab','ba']] >>> >>> lst2 = deepcopy(lst1) >>> >>> lst2[2][1] = "d" >>> lst2[0] = "c"; >>> >>> print lst2 ['c', 'b', ['ab', 'd']] >>> print lst1 ['a', 'b', ['ab', 'ba']] >>> Special module copy allows for deepcopy of the whole object

25 Logical Operators End


Download ppt "Python Crash Course Containers Bachelors V1.0 dd 13-01-2015 Hour 6."

Similar presentations


Ads by Google