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

Slides:



Advertisements
Similar presentations
Container Types in Python
Advertisements

Announcements You survived midterm 2! No Class / No Office hours Friday.
Dictionaries: Keeping track of pairs
Lecture 12 – ADTs and Stacks.  Modularity  Divide the program into smaller parts  Advantages  Keeps the complexity managable  Isolates errors (parts.
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.
Lists Introduction to Computing Science and Programming I.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
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.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 14 Tuples, Sets, and Dictionaries 1.
Beyond Lists: Other Data Structures CS303E: Elements of Computers and Programming.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
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.
Python Tricks CMSC 201. Overview Today we are learning some new tricks to make our lives easier! Slicing and other tricks Multiple return values Global.
Chapter 8 More On Functions. "The Practice of Computing Using Python", Punch & Enbody, Copyright © 2013 Pearson Education, Inc. First cut, scope.
CIT 590 Intro to Programming Lecture 5 – completing lists.
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.
Chapter 9 Dictionaries and Sets.
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.
Sets Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
CIT 590 Intro to Programming Lecture 4. How are assignments evaluated Pay attention to what the HW says it is trying to teach you about ‘modular programming’
CS190/295 Programming in Python for Life Sciences: Lecture 6 Instructor: Xiaohui Xie University of California, Irvine.
Stacks and Queues CMSC 201. Stacks and Queues Sometimes, when we use a data-structure in a very specific way, we have a special name for it. This is to.
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.
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.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Today… Files from the Web! Dictionaries. Lists of lists. Winter 2016CISC101 - Prof. McLeod1.
CMSC201 Computer Science I for Majors Lecture 08 – Lists
Sharing, mutability, and immutability
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
CMSC201 Computer Science I for Majors Lecture 22 – Searching
Containers and Lists CIS 40 – Introduction to Programming in Python
Department of Computer Science,
Foundations of Programming: Arrays
Ruth Anderson CSE 140 University of Washington
Repeating code We could repeat code we need more than once: i = 1 print (i) i += 1 print (i) #… stop when i == 9 But each line means an extra line we might.
CSc 110, Autumn 2017 Lecture 30: Sets and Dictionaries
Sharing, mutability, and immutability
CISC101 Reminders Slides have changed from those posted last night…
CS190/295 Programming in Python for Life Sciences: Lecture 6
Sharing, mutability, and immutability
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
Sharing, mutability, and immutability
Ruth Anderson UW CSE 160 Winter 2017
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Dictionaries Dictionary: object that stores a collection of data
Sharing, mutability, and immutability
Michael Ernst CSE 140 University of Washington
6. Dictionaries and sets Rocky K. C. Chang 18 October 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
CISC101 Reminders Assignment 2 due today.
Ruth Anderson CSE 160 University of Washington
Python Review
Ruth Anderson UW CSE 160 Spring 2018
Winter 2019 CISC101 5/26/2019 CISC101 Reminders
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Dictionary.
Tuple.
Introduction to Computer Science
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

Python Data Structures CMSC 201

Built in Types Today we will be talking about some other built in types in python! Tuples Sets Dictionaries

Terms We’ll Use Here is some vocab we’ll be using in today’s lecture: Data structure-A way of organizing or storing information. So far, the main data structure we’ve seen is the list. Ordered-The data structure has the elements stored in Mutable-The contents of the data structure can be changed.

Tuples Tuples are ordered, immutable collections of elements. The only difference between a tuple and a list is that once a tuple has been made, it can’t be changed!

Tuples Making a tuple: a = (1, 2, 3) Accessing a tuple: someVar = a[0] The syntax for access is exactly like a list. However, you can’t reassign things.

Tuples So Far We’ve already used tuples without knowing it! def myFunc(): return 1, 2 def main(): result = myFunc() print(result) When you return multiple things and store it in a single variable, it comes back as a tuple!

Tuples So Far Why would you want a tuple? Sometimes it’s important that the contents of something not be modified in the future. Instead of trying to remember that you shouldn’t modify something, just put it in a tuple! A lot of programming is learning to protect you from yourself.

Sets A set is an unordered collection of elements where each element must be unique. Attempts to add duplicate elements are ignored.

Sets Creating a set: mySet = set([‘a’, ‘b’, ‘c’, ‘d’]) Or: myList = [1, 2, 3, 1, 2, 3] mySet2 = set(myList) Note that in the second example, the set would consist of the elements {1, 2, 3}

Sets Things we can do with a set: mySet = set([‘a’]) mySet.add(‘b’) # Adds an element mySet.remove(‘b’) #Removes an element mySet.pop() # Removes and returns a random element

Sets There is also support for combining sets. mySet.union(someOtherSet) – this returns a new set with all the elements from both sets. mySet.intersection(someOtherSet) – this returns a new set with all the elements that both sets had in common. Tons more methods can be found here:

Dictionaries Up until now our storage has been done in lists. Lists can be viewed as a structure that map indexes to values. If I make the list: myList = [‘a’, ‘b’, ‘c’] I have created a mapping from 0 to ‘a’, 1 to ‘b’, and so on. If I put in 0, I’ll get ‘a’ back.

Dictionaries Dictionaries let use whatever kind of keys we want! Instead of having 1 correspond to ‘b’, I can have “hello” correspond to ‘b’. Before:Now I can do things like: 0  ‘a’“Hello”  ‘a’ 1  ‘b’1  ‘b’ 2  ‘c’3.3  ‘c’

Dictionaries This looks exactly like you’d expect! myDict = {} myDict[“hello”] = ‘a’ myDict[1] = ‘b’ myDict[3.3] = ‘c’ print(myDict[“hello”]) Prints: ‘a’

Dictionaries Why would you want to do this? Imagine you have a bunch of university students, and you’re storing their grades in all their classes. student = “Max Morawski” grades = [A, B, C, D, D, C] We can set it up so that if we know a student’s name, it’s easy to look up their grades! gradeDict = {} gradeDict[student] = grades Now if I access gradeDict[“Max Morawski”], I’ll get Max’s grades back out. This isn’t easy to do with a list!

Dictionaries When you look up something in a dictionary, the thing you’re putting in (like the index in a list) is called a key. What we get out is called a value. A dictionary maps keys to values. myDict[“hello”] = 10^ Key Value

Dictionaries Just like in a list, if you do this: myDict[“hello”] = 10 myDict[“hello”] = 11 print(myDict[“hello”]) Prints: 11

Dictionaries If we want to get just the keys, or just the values, there’s a function for that! listOfKeys = myDict.keys() listOfValues = myDict.values()