dictionaries and sets
play

Dictionaries and Sets Ali Taheri Sharif University of Technology - PowerPoint PPT Presentation

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.


  1. Fu Fundamentals of Pr Programming (Py Python) Dictionaries and Sets Ali Taheri Sharif University of Technology Spring 2019

  2. 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]

  3. 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]

  4. Dictionary Operations Checking the existence of a key: ◦ Using in operator 4 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  5. List Operations Deletion: 5 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  6. Dictionaries are Mutable 6 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  7. Iteration over Dictionaries Output: 7 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  8. 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]

  9. Sparse Matrices A sparse matrix is a matrix with lots of zeros! 9 Spring 2019 ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON]

  10. 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]

  11. 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]

  12. 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]

  13. 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]

  14. 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]

  15. 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]

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