{} Introduction to Computer Programming Data Structures CSCI-UA 2 - - PowerPoint PPT Presentation

introduction to computer programming data structures csci
SMART_READER_LITE
LIVE PREVIEW

{} Introduction to Computer Programming Data Structures CSCI-UA 2 - - PowerPoint PPT Presentation

Introduction to Computer Programming Data Structures CSCI-UA 2 Dictionaries {} Introduction to Computer Programming Data Structures CSCI-UA 2 Dictionaries {key: value} Dictionaries {} A dictionary is a data structure for storing pairs


slide-1
SLIDE 1

Introduction to Computer Programming Data Structures CSCI-UA 2 Dictionaries

{}

slide-2
SLIDE 2

Introduction to Computer Programming CSCI-UA 2

Dictionaries {}

Data Structures Dictionaries

{key: value} A dictionary is a data structure for storing pairs of values Values can be accessed by their keys Like lists, dictionaries are mutable Keys are unique and cannot be repeated within a dictionary Keys must be immutable and cannot be a list or dictionary Values are, however, mutable

slide-3
SLIDE 3

Introduction to Computer Programming CSCI-UA 2

Dictionaries Methods

Data Structures Dictionaries

dict.items() dict.keys() dict.values() dict.get() dict.pop() dict.popitem() dict.clear() dict.copy() dict.fromkeys() dict.update()

slide-4
SLIDE 4


 
 
 
 
 


Introduction to Computer Programming Data Structures CSCI-UA 2 Dictionaries

Order

Lists vs. Dictionaries

  • Lists are ordered
  • Dictionaries are unordered

Access

  • Lists require a numeric index to

access individual items

  • Dictionary values are accessed by

their unique key General Guideline

  • If order matters, use a list
  • If you need to access values with a

unique key, use a dictionary

slide-5
SLIDE 5

Introduction to Computer Programming CSCI-UA 2

Tuples ()

Data Structures Tuples

(1, 2.0, 'three') A tuple is an immutable sequence of 0

  • r more values

Enclosed in round brackets, items separated by a comma Tuples with a single item must be followed by a comma: (x,) Once created, it cannot be changed, which can help prevent errors Use indexing and slicing to access individual elements

slide-6
SLIDE 6

Introduction to Computer Programming Data Structures CSCI-UA 2 Sets

Sets set()

A set is a collection of 0 or more items with no duplicates A good way to remove duplicates from a sequence Two categories: mutable sets and immutable frozensets

slide-7
SLIDE 7

Introduction to Computer Programming Data Structures CSCI-UA 2 Dictionaries