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
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

Dictionaries: Keeping track of pairs
Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
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.
Lecture 05 – Dictionaries.  Consider the following methods of sorting information in a sequence. What is the output produced by each program? 2COMPSCI.
Lists Introduction to Computing Science and Programming I.
Python November 18, Unit 7. So Far We can get user input We can create variables We can convert values from one type to another using functions We can.
Debugging CMSC 201. Announcements Hw2, Hw3 grades returned if something doesn’t seem right, ask Midterm next Thurs (Oct 23) we’ll review on Tues no hw.
Main task -write me a program
2/28/2008. >>> Overview Arrays in Python – a.k.a. Lists Ranges are Lists Strings vs. Lists Tuples vs. Lists Map-Reduce Lambda Review: Printing to a file.
JaySummet IPRE Python Review 2. 2 Outline Compound Data Types: Strings, Tuples, Lists & Dictionaries Immutable types: Strings Tuples Accessing.
CMPT 120 Lists and Strings Summer 2012 Instructor: Hassan Khosravi.
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 November 28, Unit 9+. Local and Global Variables There are two main types of variables in Python: local and global –The explanation of local and.
Data Collections: Dictionaries CSC 161: The Art of Programming Prof. Henry Kautz 11/4/2009.
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.
Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries.
Storage Copyright © Software Carpentry 2010 This work is licensed under the Creative Commons Attribution License See
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.
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.
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.
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.
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.
Python Fundamentals: Complex Data Structures Eric Shook Department of Geography Kent State University.
CMSC201 Computer Science I for Majors Lecture 08 – Lists
COMPSCI 107 Computer Science Fundamentals
Sharing, mutability, and immutability
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
CMSC201 Computer Science I for Majors Lecture 22 – Searching
Containers and Lists CIS 40 – Introduction to Programming in Python
Department of Computer Science,
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
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
CISC101 Reminders Slides have changed from those posted last night…
Sharing, mutability, and immutability
CS190/295 Programming in Python for Life Sciences: Lecture 6
6. Lists Let's Learn Python and Pygame
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
Sharing, mutability, and immutability
Michael Ernst CSE 140 University of Washington
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.
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 an ordered sequence 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']) # Add an element: mySet.add('b') #Remove an element: mySet.remove('b') # Remove and return a random element: mySet.pop()

Sets There is also support for combining sets. Returns a new set with all the elements from both sets: mySet.union(someOtherSet) Returns a new set with elements that were in both sets: mySet.intersection(someOtherSet) 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 Imagine you have a bunch of university students, and you're storing their grades in all their classes. Use a dictionary to look up grades by name: student = "Clark Kent" grades = ["A","B","C","D","D","C"] gradeDict = {} gradeDict[student] = grades print( gradeDict["Clark Kent"] )

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() listOfPairs = myDict.items()