Dictionary basics Consider typing the word “infinite” in the Google search:

Slides:



Advertisements
Similar presentations
Dictionaries (aka hash tables or hash maps) Genome 559: Introduction to Statistical and Computational Genomics Prof. James H. Thomas.
Advertisements

Data import and export Skills: none IT concepts: data import and export, common data format This work is licensed under a Creative Commons Attribution-Noncommercial-
Guide to Programming with Python
Python Dictionary.
GOOGLE TIPS. TO FIND AN EXACT QUOTE Put quotation marks around the words "Ask not what your country can do for you"
Lecture 23 – Python dictionaries 1 COMPSCI 101 Principles of Programming.
CS 177 Week 11 Recitation Slides 1 1 Dictionaries, Tuples.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Intro to GIS and ESRI Trainers: Randy Jones, GIS Technician, Douglas County Jon Fiskness, GISP GIS Coordinator, City of Superior.
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.
Relational Databases (MS Access)
ICOM 4035 – Data Structures Lecture 11 – Map ADT Manuel Rodriguez Martinez Electrical and Computer Engineering University of Puerto Rico, Mayagüez ©Manuel.
Databases Organizing Sorting Querying A Presentation by Karen Work Richardson.
Chapter 9 Dictionaries and Sets.
Design Exercise UW CSE 190p Summer 2012 download examples from the calendar.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Python 101 Dr. Bernard Chen University of Central Arkansas PyArkansas.
Note: Statistics based on Location Address FinCEN Suspicious Activity Report by Money Services Businesses Geographic Distribution for the State of New.
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.
The Five Boroughs of New York City. Made by Natasha Burmistrova Teacher:T.Makarova.
7. Lists 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Python Data Structures By Greg Felber. Lists An ordered group of items Does not need to be the same type – Could put numbers, strings or donkeys in the.
Dictionaries and File I/O George Mason University.
Dictionaries Alexandra Stefan CSE1310 – University of Texas at Arlington.
Microsoft Office Access 2010 Lab 1
Concept & Examples of pyspark
Intro to CS Nov 17, 2016.
10 - Python Dictionary John R. Woodward.
Python Variable Types.
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
Address Book Example
Announcements Project 4 due Wed., Nov 7
Data types: Complex types (Dictionaries)
LING 388: Computers and Language
Dictionaries GENOME 559: Introduction to Statistical and Computational Genomics Prof. William Stafford Noble.
CISC101 Reminders Quiz 2 this week.
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
COSC 1323 – Computer Science Concepts I
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Downloading & Uploading Files
Guide to Programming with Python
Access Lesson 2 Creating a Database
6. Lists Let's Learn Python and Pygame
[Your company] Business Plan [Street Address City, State & Zip Code
Coding Concepts (Data Structures)
Introduction to Dictionaries
Downloading & Uploading Files
Background survey Skills: none
HTML Links.
CS 1111 Introduction to Programming Fall 2018
Dictionaries Dictionary: object that stores a collection of data
Nate Brunelle Today: Dictionaries
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Reference semantics, variables and names
Displaying and Editing Data
CHAPTER 4: Lists, Tuples and Dictionaries
JSON for Linked Data: a standard for serializing RDF using JSON
Nate Brunelle Today: Dictionaries
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Introduction to Dictionaries
[Your company] Business Plan [Street Address City, State & Zip Code
CMSC201 Computer Science I for Majors Lecture 19 – Dictionaries
Files A common programming task is to read or write information from/to a file.
Sample lecture slides.
Dictionary.
Strings String basics String formatting.
Lists basics A container is a construct used to group related values together and contains references to other objects instead of data. A list is a container.
Presentation transcript:

Dictionary basics Consider typing the word “infinite” in the Google search:

Dictionary basics A dictionary is a Python container used to describe associative relationships. A dictionary is represented by the dict object type. A dictionary associates (or "maps") keys with values. A key is a term that can be located in a dictionary, such as the word "infinite" in the Google search. A value describes some data associated with a key, such as a definition. A key can be any immutable type, such as a number, string, or tuple; a value can be any type.

Dictionary basics A dict object is created using curly braces { } to surround the key:value pairs that comprise the dictionary contents. Example: myDict = { “street address”: ”2155 University Avenue”, “city”: “Bronx”, “state” : ”New York”, “zip code” : 10453, “phone” : “(718) 289-5100”, “admissions” : “(718) 289-5895”}

Dictionary basics Dictionaries are typically used in place of lists when an associative relationship exists. Example: If a program contains a collection of anonymous student test scores, those scores should be stored in a list. However, if each score is associated with a student name, a dictionary could be used to associate student names to their score.

Download and save file Dict1.py from our web-site Dictionary basics: in-class work I have 5 students: Cute Princess, Fairy Queen, Evil Don, Fussy Cat, and Lazy Daisy. I also have the record of 4 of their test scores. Let’s create a dictionary students, where student’s IDs will serve as keys, and the value will be a list with five elements/members: student’s name, and 4 test scores. Name ID Test 1 Test 2 Test 3 Test 4 Cute Princess 846563 89 67 98 100 Fairy Queen 736542 76 56 83 99 Evil Don 287563 52 81 79 27 Fussy Cat 294512 38 75 Lazy Daisy 975321 88 66 77 Download and save file Dict1.py from our web-site

Dictionary basics: in-class work students dictionary we got: keys 846563 736542 287563 294512 975321 1 2 3 4 “Cute Princess” 89 67 98 100 “Fairy Queen” 76 56 83 99 “Evil Don” 52 81 79 27 “Fussy Cat” 38 75 “Lazy Daisy” 88 66 77

Dictionary basics: in-class work Now, let’s print some information: put the following lines into Dict1.py: print(students[975321]) print(students[846563]) See what happens! keys 846563 736542 287563 294512 975321 1 2 3 4 “Cute Princess” 89 67 98 100 “Fairy Queen” 76 56 83 99 “Evil Don” 52 81 79 27 “Fussy Cat” 38 75 “Lazy Daisy” 88 66 77

Dictionary basics: in-class work Let’s now calculate Lazy Daisy’s average test score: add the following lines of code into Dict1.py s = students[975321] averageTestScore = (s[1]+s[2]+s[3]+s[4])/4 print(“Lazy Daisy average test score is”, averageTestScore) keys 846563 736542 287563 294512 975321 1 2 3 4 “Cute Princess” 89 67 98 100 “Fairy Queen” 76 56 83 99 “Evil Don” 52 81 79 27 “Fussy Cat” 38 75 “Lazy Daisy” 88 66 77 s s[0] s[1] s[2] s[3] s[4]

Dictionary basics: in-class work Now, let’s add one more record and display the dictionary: By adding the following line in Dict1.py: students[625342] = [“Glad Lad”,98,76,48,80] print(students) Name ID Test 1 Test 2 Test 3 Test 4 “Glad Lad” 625342 98 76 48 80 keys 846563 736542 287563 294512 975321 625342 1 2 3 4 “Cute Princess” 89 67 98 100 “Fairy Queen” 76 56 83 99 “Evil Don” 52 81 79 27 “Fussy Cat” 38 75 “Lazy Daisy” 88 66 77 “Glad Lad” 48 80

Dictionary basics: in-class work Now, let’s delete the record about Fussy Cat from the dictionary By adding the following line in Dict1.py: del students[294512] print(students) keys 846563 736542 287563 294512 975321 625342 1 2 3 4 “Cute Princess” 89 67 98 100 “Fairy Queen” 76 56 83 99 “Evil Don” 52 81 79 27 “Fussy Cat” 38 75 “Lazy Daisy” 88 66 77 “Glad Lad” 48 80

Dictionary basics: in-class work Review all operations we did in the file Dict1.py with dictionary students before proceeding to the handout. In class activity: follow the handout

This OER material was produced as a result of the CS04ALL CUNY OER project. This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 4.0 License.