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

dictionaries and sets
SMART_READER_LITE
LIVE PREVIEW

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.


slide-1
SLIDE 1

Fu Fundamentals of Pr Programming (Py Python)

Dictionaries and Sets

Ali Taheri Sharif University of Technology

Spring 2019

slide-2
SLIDE 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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-3
SLIDE 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

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-4
SLIDE 4

Dictionary Operations

Checking the existence of a key:

  • Using in operator

4

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-5
SLIDE 5

List Operations

Deletion:

5

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-6
SLIDE 6

Dictionaries are Mutable

6

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-7
SLIDE 7

Output:

Iteration over Dictionaries

7

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-8
SLIDE 8

Access built-in methods using dot operator:

Dictionary Methods

8

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

Method Meaning dict.keys() Returns a sequence of keys. dict.values() Returns a sequence of values. dict.items() Returns a sequence of tuples (key, value) representing the key-value pairs. dict.clear() Deletes all entries. dict.get(key, default) If dictionary has key returns its value;

  • therwise returns default.
slide-9
SLIDE 9

A sparse matrix is a matrix with lots of zeros!

Sparse Matrices

9

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

slide-10
SLIDE 10

Sets

Set is a collection of unique objects

10

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

x = set(["Postcard", "Radio", "Telegram"]) print(x) {'Telegram', 'Radio', 'Postcard'} y = {"Postcard","Radio","Telegram"} print(y) {'Telegram', 'Radio', 'Postcard'}

slide-11
SLIDE 11

Sets

Iteration

11

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

>>> num_set = set([0, 1, 2, 3, 4, 5]) >>> for n in num_set: print(n) 1 2 3 4 5 >>>

slide-12
SLIDE 12

Sets

Adding items

12

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

>>> color_set = set() >>> color_set.add("Red") >>> print(color_set) {'Red'} >>> color_set.update(["Blue", "Green"]) >>> print(color_set) {'Red', 'Blue', 'Green'} >>>

slide-13
SLIDE 13

Sets

Removing items

13

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

>>> num_set = set([0, 1, 2, 3, 4, 5]) >>> num_set.discard(3) >>> print(num_set) {0, 1, 2, 4, 5} >>>

slide-14
SLIDE 14

Sets

Intersection Union

14

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

>>> setx = set(["green", "blue"]) >>> sety = set(["blue", "yellow"]) >>> setz = setx & sety >>> print(setz) {'blue'} >>> setx = set(["green", "blue"]) >>> sety = set(["blue", "yellow"]) >>> seta = setx | sety >>> print (seta) {'yellow', 'blue', 'green'}

slide-15
SLIDE 15

Sets

Difference Symmetric Difference

15

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

>>> setx = set(["green", "blue"]) >>> sety = set(["blue", "yellow"]) >>> setc = setx ^ sety >>> print(setc) {'yellow', 'green'} >>> setx = set(["green", "blue"]) >>> setz = {'blue'} >>> setb = setx - setz >>> print(setb) {'green'}

slide-16
SLIDE 16

Sets

Subset and Superset

16

ALI TAHERI - FUNDAMENTALS OF PROGRAMMING [PYTHON] Spring 2019

>>> setx = set(["green", "blue"]) >>> sety = set(["blue", "yellow"]) >>> issubset = setx <= sety >>> print(issubset) False >>> issuperset = setx >= sety >>> print(issuperset) False >>> setx = set(["green", "blue"]) >>> sety = set(["blue", "green"]) >>> issubset = setx <= sety >>> print(issubset) True >>> issuperset = setx >= sety >>> print(issuperset) True