Fu Fundamentals of Pr Programming (Py Python) Dictionaries and Sets Ali Taheri Sharif University of Technology Spring 2019
Outline 1. Dictionary Basics 2. Dictionary Operations 3. Dictionary are Mutable 4. Iterating over Dictionary 5. Dictionary Methods 6. Sparse Matrices 7. Sets 2 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Dictionary Basics Dictionary is an unordered collection of key-value pairs ◦ Each value can be accessed using its key ◦ Keys can be any immutable object 3 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Dictionary Operations Checking the existence of a key: ◦ Using in operator 4 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
List Operations Deletion: 5 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Dictionaries are Mutable 6 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Iteration over Dictionaries Output: 7 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Dictionary Methods Access built-in methods using dot operator: Method Meaning dict.keys() Returns a sequence of keys. dict.values() Returns a sequence of values. Returns a sequence of tuples (key, value) dict.items() representing the key-value pairs. dict.clear() Deletes all entries. If dictionary has key returns its value; dict.get(key, default) otherwise returns default. 8 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Sparse Matrices A sparse matrix is a matrix with lots of zeros! 9 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Sets Set is a collection of unique objects x = set(["Postcard", "Radio", "Telegram"]) print (x) {'Telegram', 'Radio', 'Postcard'} y = {"Postcard","Radio","Telegram"} print (y) {'Telegram', 'Radio', 'Postcard'} 10 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Sets Iteration >>> num_set = set([0, 1, 2, 3, 4, 5]) >>> for n in num_set: print(n) 0 1 2 3 4 5 >>> 11 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Sets Adding items >>> color_set = set() >>> color_set.add("Red") >>> print(color_set) {'Red'} >>> color_set.update(["Blue", "Green"]) >>> print(color_set) {'Red', 'Blue', 'Green'} >>> 12 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Sets Removing items >>> num_set = set([0, 1, 2, 3, 4, 5]) >>> num_set.discard(3) >>> print(num_set) {0, 1, 2, 4, 5} >>> 13 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Sets Intersection >>> setx = set(["green", "blue"]) >>> sety = set(["blue", "yellow"]) >>> setz = setx & sety >>> print(setz) {'blue'} Union >>> setx = set(["green", "blue"]) >>> sety = set(["blue", "yellow"]) >>> seta = setx | sety >>> print (seta) {'yellow', 'blue', 'green'} 14 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Sets Difference >>> setx = set(["green", "blue"]) >>> setz = {'blue'} >>> setb = setx - setz >>> print(setb) {'green'} Symmetric Difference >>> setx = set(["green", "blue"]) >>> sety = set(["blue", "yellow"]) >>> setc = setx ^ sety >>> print(setc) {'yellow', 'green'} 15 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Sets Subset and Superset >>> setx = set(["green", "blue"]) >>> setx = set(["green", "blue"]) >>> sety = set(["blue", "yellow"]) >>> sety = set(["blue", "green"]) >>> issubset = setx <= sety >>> issubset = setx <= sety >>> print(issubset) >>> print(issubset) False True >>> issuperset = setx >= sety >>> issuperset = setx >= sety >>> print(issuperset) >>> print(issuperset) False True 16 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]
Recommend
More recommend