Nate Brunelle Today: Dictionaries

Slides:



Advertisements
Similar presentations
Dictionaries: Keeping track of pairs
Advertisements

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 Crash Course Containers 3 rd year Bachelors V1.0 dd Hour 3.
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.
Python Lists and Such CS 4320, SPRING List Functions len(s) is the length of list s s + t is the concatenation of lists s and t s.append(x) adds.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
Built-in Data Structures in Python An Introduction.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
1 Joe Meehean.  List of names  Set of names  Map names as keys phone #’s as values Phil Bill Will Phil Bill Will Phil Bill Will Phil: Bill:
CS105 STRING LIST TUPLE DICTIONARY. Characteristics of Sequence What is sequence data type? It stores several objects Each object has an order Each object.
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.
Compsci 101.2, Fall Plan for October 29 l Review dictionaries and their use  Very efficient, easy to use  Efficiency doesn't matter much for.
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
More Python Data Structures  Classes ◦ Should have learned in Simpson’s OOP ◦ If not, read chapters in Downey’s Think Python: Think like a Computer Scientist.
Introduction to python programming
Scientific Programming in Python -- Cheat Sheet
Python Variable Types.
Sharing, mutability, and immutability
Lecture 3 Python Basics Part 2.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Dictionaries, File operations
LING 388: Computers and Language
Sharing, mutability, and immutability
Ruth Anderson UW CSE 160 Spring 2018
Dictionaries GENOME 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
Introduction to Strings
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
COSC 1323 – Computer Science Concepts I
Ruth Anderson UW CSE 160 Winter 2017
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Sharing, mutability, and immutability
Nate Brunelle Today: Repetition, Repetition
Nate Brunelle Today: Slicing, Debugging, Style
Nate Brunelle Today: Functions again, Scope
Python for Informatics: Exploring Information
Sharing, mutability, and immutability
Python dicts and sets Some material adapted from Upenn cis391 slides and other sources.
Introduction to Dictionaries
Arrays ICS2O.
Nate Brunelle Today: Turtles
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Dictionaries Dictionary: object that stores a collection of data
Python Primer 1: Types and Operators
Sharing, mutability, and immutability
Introduction to Strings
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
(more) Python.
CS1110 Today: collections.
Dictionaries: Keeping track of pairs
CISC101 Reminders Assignment 2 due today.
Nate Brunelle Today: Conditional Decision Statements
Introduction to Strings
Python Review
Nate Brunelle Today: Dictionaries
CSE 231 Lab 8.
Gavin Restifo March 1, 2019 Today: Repetition Part 2 - For Loops
Nate Brunelle Today: Style, Collections
Nate Brunelle Today: Strings, Type Casting
Sample lecture slides.
3, 6, 9, 12, __, 18 ____ Pattern What comes next in the pattern?
Dictionary.
Python - Tuples.
Tuple.
Presentation transcript:

Nate Brunelle Today: Dictionaries CS1110 Nate Brunelle Today: Dictionaries

Questions?

Collections Order doesn’t matter, No Repetition Order Matters, Repetition ok Examples: String List Tuple Range Counting starts at 0 collection[index] gives a specific value in the collection Order doesn’t matter, No Repetition Examples: Set Dict Map Table

Set Things are unordered Cannot have repetition

Dict A set of key:value mappings d = { } # How we create one d[‘name’] = 1 # How we insert somthing d[‘name’] # Access something

Dict is like variables Gives names to values Create using = Reassign using = Easy to go from name to a value Very hard to go from value to name

Dict is like a List Index to access members Key to access members Key can be any immutable type No rules for values of keys To add in a new thing: d[key] = … Index to access members Index must be an int Indexes cannot be skipped To add in a new thing: lst.append(…)

Using a dict as a Collection d.keys() Gives a list of all of the keys in the dict The key is the thing in square brackets The “word” When you use a dict as a collection, it defaults to this E.g.: for thing in d d.values() Gives a list of all the values in the dict The value is what you get when you access using a key The “definition” d.items() Gives a list of all (key, value) tuples in the dict

Key Value Bananas 12.0 Gloves 29.0 Suit 100.0 Bananas 10.0 Gloves 29.0 Suit 100.0 Bananas 2.0 If that thing was in the dictionary: add to that value Else: add that thing to the dictionary, with value equal to the cost