Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.

Slides:



Advertisements
Similar presentations
Python Data Structures CMSC 201. Built in Types Today we will be talking about some other built in types in python! Tuples Sets Dictionaries.
Advertisements

CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
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
I210 review Fall 2011, IUB. Python is High-level programming –High-level versus machine language Interpreted Language –Interpreted versus compiled 2.
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.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
Python Crash Course Containers 3 rd year Bachelors V1.0 dd Hour 3.
Python Programming Chapter 10: Dictionaries Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
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.
Handling Lists F. Duveau 16/12/11 Chapter 9.2. Objectives of the session: Tools: Everything will be done with the Python interpreter in the Terminal Learning.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
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.
Chapter 7 Lists and Tuples. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. Data Structures.
Collecting Things Together - Lists 1. We’ve seen that Python can store things in memory and retrieve, using names. Sometime we want to store a bunch of.
Built-in Data Structures in Python An Introduction.
CIT 590 Intro to Programming Lecture 5 – completing lists.
And other languages….  Array literals/initialization a = [1,2,3] a2 = [-10..0, 0..10] a3 = [[1,2],[3,4]] a4 = [w*h, w, h] a5 = [] empty = Array.new zeros.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries.
Copyright © 2015 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 Dictionaries and Sets.
Lists CS303E: Elements of Computers and Programming.
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:
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Python Data Structures CMSC 201. Built in Types Today we will be talking about some other built in types in python! Tuples Sets Dictionaries.
Collections Michael Ernst CSE 190p University of Washington.
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.
Introduction to Computing Using Python Dictionaries: Keeping track of pairs  Class dict  Class tuple.
Data Collections CS 127. Lists Lists are ordered sequences of items All programming languages provide a sequence structure similar to a Python list; in.
Guide to Programming with Python Chapter Five Lists and dictionaries (data structure); The Hangman Game.
LECTURE 3 Python Basics Part 2. FUNCTIONAL PROGRAMMING TOOLS Last time, we covered function concepts in depth. We also mentioned that Python allows for.
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
COSC 1306—COMPUTER SCIENCE AND PROGRAMMING PYTHON TUPLES, SETS AND DICTIONARIES Jehan-François Pâris
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Introduction to Python Aug 22, 2013 Hee-gook Jun.
CPSC 252 Tables / Maps / Dictionaries Page 1 Tables, Maps and Dictionaries A table (or map or dictionary) is a collection of key/value pairs. In general.
Python Basics 본 자료는 다음의 웹 사이트를 정리 한 내용이니 참조 바랍니다. ythonBasics.
Dictionaries. The compound types you have learned about - - strings, lists, and tuples – use integers as indices. If you try to use any other type as.
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.
שיעור 8:tuple, פונקציות 2, sets, dict מבוא ל Python שיעור 5: set, פונקציות, dict.
String and Lists Dr. José M. Reyes Álamo.
10 - Python Dictionary John R. Woodward.
Lecture 3 Python Basics Part 2.
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
CMSC201 Computer Science I for Majors Lecture 14 – Tuples
CMSC201 Computer Science I for Majors Lecture 17 – Dictionaries
CMSC201 Computer Science I for Majors Lecture 21 – Dictionaries
Lecture 10 Data Collections
Ruth Anderson CSE 140 University of Washington
CHAPTER THREE Sequences.
Guide to Programming with Python
CS190/295 Programming in Python for Life Sciences: Lecture 6
String and Lists Dr. José M. Reyes Álamo.
COSC 1306 COMPUTER SCIENCE AND PROGRAMMING
Presented by Mirza Elahi 10/29/2018
Ruth Anderson UW CSE 160 Winter 2017
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Michael Ernst CSE 140 University of Washington
Nate Brunelle Today: Dictionaries
Dictionaries: Keeping track of pairs
Ruth Anderson CSE 160 University of Washington
Python Review
Ruth Anderson UW CSE 160 Spring 2018
Lists [] First = [1, 2, 3, 4] Second = list[range(1,5)]
Dictionary.
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming

Announcements Exam 3: Exam 3: –Wednesday, May 1st Assignment 9 will be our last project (and there was much rejoicing!) Assignment 9 will be our last project (and there was much rejoicing!)

Other Data Structures Tuples Tuples Sets Sets Dictionaries Dictionaries

Tuples Sequential data structures Sequential data structures –Order matters –Lists and strings are also sequential Very similar to lists BUT immutable Very similar to lists BUT immutable –Can prevent data from being changed Uses: employee or student records Uses: employee or student records

Tuple Representation Like lists, but with parentheses instead of square brackets myTuple = (1,2,3) myTuple = (“a”, “b”, 3) myTuple = (1,2,1,2,3)

Tuple Operations OperationDescription []Indexing [:]Slicing +Concatenation *Repetition len()Length for elem in myTuple OR for i in range(len(myTuple)) then access with [] Traverse elem in myTupleMembership

Packing and Unpacking Tuples You can also create a tuple by packing values together: You can also create a tuple by packing values together: myTuple = “a”,4,”hello” And you can unpack them: And you can unpack them: letter, num, word = myTuple

Converting Tuples Convert from tuple to list using list Convert from tuple to list using listlist(myTuple) Convert from list to tuple using tuple Convert from list to tuple using tupletuple(myList)

Sets A set is an unordered collection with no duplicates A set is an unordered collection with no duplicates –Similar to mathematical sets

Set Representation Create from a list: Create from a list: mySet = set([1,2,3,4]) myList = [1,2,3,4,5] mySet = set(myList) Duplicate elements will be eliminated and the ordering lost Duplicate elements will be eliminated and the ordering lost

Set Operations Union ( | ) Union ( | ) –Elements in one or both sets Intersection ( & ) Intersection ( & ) –Elements in both sets Difference ( - ) Difference ( - ) Symmetrical Difference ( ^ ) Symmetrical Difference ( ^ ) –Elements in one but not both sets

More Set Operations OperationDescription.add( )Add an element to the set.clear()Remove all elements from the set len()Length for elem in myTupleTraverse elem in myTupleMembership

Question: What do the following datatypes have in common? List, String, Tuple A. All are immutable B. All are mutable C. All are sequential D. All are unordered

Dictionaries Unordered collection of key-value pairs Unordered collection of key-value pairs –Keys are used to find values in the collection –Called hash tables or maps in other languages Key must be an immutable type Key must be an immutable type Keys are unique in the dictionary Keys are unique in the dictionary

Dictionary Representation To represent a dictionary: To represent a dictionary:{<key>:<value>,<key>:<value>}Examples: myDictionary = {} #empty myDictionary = {“hammer”:”tool”, “ham”:”meat”}

Dictionary Operations Add to a dictionary by indexing with a key and assigning a value: Add to a dictionary by indexing with a key and assigning a value:myDictionary[<key>]=<value>Example: myDictionary = {} #empty myDictionary[“apple”]=“fruit” #{“apple”:”fruit”}

Dictionary Operations Change a value associated with a key by simply re-doing the assignment: Change a value associated with a key by simply re-doing the assignment:myDictionary[<key>]=<new_value>Example: myDictionary[“apple”]=“veggie” #{“apple”:”veggie”} myDictionary[“apple”]=“fruit” #{“apple”:”fruit”}

Dictionary Operations Remove from a dictionary by indexing with a key and using del: Remove from a dictionary by indexing with a key and using del: del myDictionary[ ] Example: myDictionary = {“apple”:”fruit”} del myDictionary[“apple”] #empty

Dictionary Operations Membership: Membership: in in Traverse: Traverse: for in for in List of Keys: List of Keys:<dictionary>.keys() List of Values: List of Values:<dictionary>.values()

More Dictionary Operations OperationDescription myDictionary.has_key( )Returns True if the dictionary contains the specified key and False otherwise myDictionary.items()Returns a list of tuples, where each tuple contains a key and value pair myDictionary.get(, )Returns value associated with key if it exists, otherwise returns default value myDictionary.clear()Removes all pairs from the dictionary

Question: For a value to act as a key in a dictionary, it must be: For a value to act as a key in a dictionary, it must be: A. A literal value B. A string C. Immutable D. All of the above

So… What types can be keys in a dictionary? What types can be keys in a dictionary? –Strings –Tuples –Numbers(!)