Data types: Complex types (Dictionaries)

Slides:



Advertisements
Similar presentations
Course A201: Introduction to Programming 10/28/2010.
Advertisements

Chapter 6 Lists and Dictionaries CSC1310 Fall 2009.
Dictionaries: Keeping track of pairs
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
Guide to Programming with Python
DICTIONARIES. The Compound Sequence Data Types All of the compound data types we have studies in detail so far – strings – lists – Tuples They are sequence.
Python Dictionary.
Sequences The range function returns a sequence
Lists Introduction to Computing Science and Programming I.
1 Sequences A sequence is a list of elements Lists and tuples – Lists mutable – Tuples immutable Sequence elements can be indexed with subscripts – First.
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
Data Structures in Python By: Christopher Todd. Lists in Python A list is a group of comma-separated values between square brackets. A list is a group.
Built-in Data Structures in Python An Introduction.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
14. DICTIONARIES AND SETS Rocky K. C. Chang 17 November 2014 (Based on from Charles Dierbach, Introduction to Computer Science Using Python and Punch and.
More Sequences. Review: String Sequences  Strings are sequences of characters so we can: Use an index to refer to an individual character: Use slices.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Dictionaries Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
String and Lists Dr. José M. Reyes Álamo.
Intro to CS Nov 21, 2016.
Data types: Complex types (List)
10 - Python Dictionary John R. Woodward.
Python Variable Types.
Input and Output Upsorn Praphamontripong CS 1110
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CSc 110, Spring 2017 Lecture 29: Sets and Dictionaries
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
When to use Tuples instead of Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
CS 1110 Introduction to Programming Spring 2017
CS 100: Roadmap to Computing
CSc 120 Introduction to Computer Programing II
Department of Computer Science,
Announcements Project 4 due Wed., Nov 7
Lecture 10 Data Collections
CSc 110, Spring 2017 Lecture 28: Sets and Dictionaries
CSc 110, Spring 2018 Lecture 32: Sets and Dictionaries
CSc 110, Autumn 2017 Lecture 31: Dictionaries
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Dictionaries GENOME 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
CISC101 Reminders Quiz 2 this week.
CMSC201 Computer Science I for Majors Lecture 20 – Dictionaries
COSC 1323 – Computer Science Concepts I
Bryan Burlingame Halloween 2018
CHAPTER THREE Sequences.
Guide to Programming with Python
4. sequence data type Rocky K. C. Chang 16 September 2018
CS 1111 Introduction to Programming Fall 2018
String and Lists Dr. José M. Reyes Álamo.
Introduction to Dictionaries
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CS 1111 Introduction to Programming Fall 2018
Dictionaries Dictionary: object that stores a collection of data
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Reference semantics, variables and names
CS 1111 Introduction to Programming Spring 2019
CS 1111 Introduction to Programming Spring 2019
CISC101 Reminders Assignment 2 due today.
CHAPTER 4: Lists, Tuples and Dictionaries
Bryan Burlingame Halloween 2018
Python Review
CSE 231 Lab 8.
Introduction to Dictionaries
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Sample lecture slides.
Dictionary.
Presentation transcript:

Data types: Complex types (Dictionaries) Upsorn Praphamontripong CS 1110 Introduction to Programming Spring 2017

Overview: Dictionaries Dictionary = unordered sequence of data Mutable data type Each element in a dictionary consists of 2 parts: key and value Key-value pair Key = index to locate a specific value Deterministic: A particular input(key) can only have one output (value) Example: key = student ID, value = student name CS 1110

Lists vs Dictionaries Lists Dictionaries Complex type Mutable Ordered sequence of data Index = 0, 1, 2, … Complex type Mutable Unordered sequence of data Index = user-defined key values keys values index 1 2 CS 1110

Dictionaries Create a dictionary Retrieve a value from a dictionary key value phonebook = {'george': '111-1111', 'tom':'222-2222'} phonebook2 = { } Empty dictionary Retrieve a value from a dictionary phonebook['tom'] Include quotations for string keys What would happen if we try to access an index that doesn’t exist? CS 1110

Add Items to a Dictionary phonebook = {'george': '111-1111', 'Chris':'222-2222', 'Tom':'333-3333'} phonebook['Joe'] = '444-4444’ value key assignment Dictionary_name[key] = value No duplicate keys in a dictionary When you assign a value to an existing key, the new value replaces the existing value CS 1110

Delete Items from Dictionaries del phonebook['Chris'] key del deletes an element at a particular position phone_number = phonebook.pop('Chris') key pop() gets a value (and use it somewhere else), and deletes an element (a key/value pair) What would happen if we try to delete an item with an index that doesn’t exist? CS 1110

Length of Dictionaries phonebook = {'george': '111-1111', 'Chris':'222-2222', 'Tom':'333-3333'} num_items = len(phonebook) len() is a function to return the length of a dictionary (i.e., the number of items in a dictionary) CS 1110

Retrieve Values, Keys, or Items phonebook.get("Joe”) # retrieve a value for a particular key phonebook.get("Tim", "Not available") # try to access a non-existent key phonebook.items() # retrieve all the keys and values phonebook.keys() # retrieve all the available keys phonebook.values() # retrieve all the values get() gets a particular value based on key items() gets all the keys and values keys() gets all the keys values() gets all the values CS 1110

Mix Data Types in Dictionaries test_scores = {'Tom' : [88, 92, 100], 'John' : [95, 88, 81], 'Ethan' : [70, 75, 78]} print(test_scores) print('John\'s scores : ' + str(test_scores['John'])) # why do we need str()? Ethan_scores = test_scores['Ethan'] print('Ethan\'s scores : ' + str(Ethan_scores)) Keys must be immutable Values can be of any data types CS 1110

in phonebook = {'george': '111-1111', 'Chris':'222-2222', 'Tom':'333-3333'} phonebook['Joe'] = '444-4444’ print("Joe" in phonebook) print("Joe" not in phonebook) print("Joe" in phonebook.keys()) print("444-4444" in phonebook.values()) in is a keyword and can be used to check if a particular item/key/value is in the dictionary/keys/values before trying to get its index CS 1110

Empty the Dictionaries phonebook.clear() clear() empties the dictionary CS 1110

Tracing through Code with Dictionaries Suppose we are using a dictionary to keep track of the number of animals in a small pet store Variables Heap numAnimals = {} numAnimals[‘cat’] = 3 numAnimals[‘fish’] = 22 numAnimals[‘dog’] = 5 numAnimals keys values A100 A100 22 5 3 ‘fish’ ‘dog’ ‘cat’ 2 1 cat 3 fish 22 dog 5 CS 1110

Tracing through Code with Dictionaries Suppose we are using a dictionary to keep track of the number of animals in a small pet store Variables Heap numAnimals = {} numAnimals[‘cat’] = 3 numAnimals[‘fish’] = 22 numAnimals[‘dog’] = 5 print numAnimals[‘dog’] print numAnimals[2] print numAnimals.keys() print numAnimals.values() numAnimals keys values A100 A100 ‘cat’ 3 5 A100 of ‘dog’ ‘dog’ 5 A100 of 2 error ‘fish’ 22 [‘cat’, ‘dog’, ‘fish’] [3, 5, 22] CS 1110

Tracing through Code with Dictionaries Suppose we are using a dictionary to keep track of the number of animals in a small pet store Variables Heap numAnimals = {} numAnimals[‘cat’] = 3 numAnimals[‘fish’] = 22 numAnimals[‘dog’] = 5 print numAnimals[‘dog’] print numAnimals.keys() print numAnimals.values() numAnimals[‘bird’] = 4 numAnimals[‘cat’] = 2 numAnimals keys values A100 A100 22 5 3 ‘fish’ ‘dog’ ‘cat’ 2 ‘bird’ 4 A100 of ‘cat’ CS 1110

Dictionaries (wrap up) dict = {} dict[1] = 'cat' dict['dog'] = -8 dict[False] = 'squirrel’ print(dict.keys()) print(dict.values()) print(dict) if 'dog' in dict.keys(): print('dog has a mapping!') if 'cat' in dict.keys(): print('cat has a mapping!') dict['dog'] = 5 print(dict) declare a dictionary with curly braces add to a dict by specifying a key and assigning it a value a key must be immutable (no lists) the .keys() method returns all the keys (but we can’t rely on an order) the .values() method returns all the values (but we can’t rely on an order) assigning to a key that already has that value overwrites the old value CS 1110

Exercise Create a dictionary of an “experience” object. You will start by getting inputs from users. Inputs contain The name of the experience (e.g., "software engineer”) The company of the experience (e.g., “IBM”) The year of the experience (e.g., “1996”) Add the users’ inputs to an “experience” dictionary The keys in the dictionary will be the year of the experience, while the values will be the name of the experience and the companies, stored as a list. E.g., { ‘1996’ : [‘software engineer’, ‘IBM], ‘1993’ : [‘sale’, ‘Target’] } You should get at least 2 experience inputs from the users. Print each experience in a separate line You may assume that all experiences passed in as arguments never have two experiences with the same company and year. Try to add more actions: retrieve items, delete items, update items, … CS 1110