Sets Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.

Slides:



Advertisements
Similar presentations
CS-2851 Dr. Mark L. Hornick 1 Tree Maps and Tree Sets The JCF Binary Tree classes.
Advertisements

CS355 - Theory of Computation Lecture 2: Mathematical Preliminaries.
CIT 590 Intro to Programming Lecture 5 – completing lists.
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.
Introduction to Strings Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Chapter P Prerequisites: Fundamental Concepts of Algebra 1 Copyright © 2014, 2010, 2007 Pearson Education, Inc. 1 P.1 Algebraic Expressions, Mathematical.
Tuples and Dictionaries Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Collections Michael Ernst CSE 190p University of Washington.
Sets and Maps Ellen Walker CPSC 201 Data Structures Hiram College.
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.
Repetition Intro to Computer Science CS1510 Dr. Sarah Diesburg.
Dictionaries Intro to Computer Science CS 1510 Dr. Sarah Diesburg.
Thinking about programming Intro to Computer Science CS1510 Dr. Sarah Diesburg.
CompSci 101 Introduction to Computer Science Feb 24, 2015 Prof. Rodger.
Introduction to Files Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg 1.
Properties A property is something that is true for all situations.
Chapter 2 1. Chapter Summary Sets (This Slide) The Language of Sets - Sec 2.1 – Lecture 8 Set Operations and Set Identities - Sec 2.2 – Lecture 9 Functions.
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.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
Types, Truth, and Expressions Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg.
FILES AND EXCEPTIONS Topics Introduction to File Input and Output Using Loops to Process Files Processing Records Exceptions.
CompSci 101 Introduction to Computer Science March 1, 2016 Prof. Rodger.
String and Lists Dr. José M. Reyes Álamo.
CMSC201 Computer Science I for Majors Lecture 05 – Comparison Operators and Boolean (Logical) Operators Prof. Katherine Gibson Based on slides by Shawn.
Sets Set is an unordered list of items
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Department of Computer Science,
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
Ruth Anderson CSE 140 University of Washington
Chapter 8 Namespaces and Memory Realities
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Introduction to Strings
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS 1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
String Encodings and Penny Math
Types, Truth, and Expressions (Part 2)
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions (Part 2)
String and Lists Dr. José M. Reyes Álamo.
Ruth Anderson UW CSE 160 Winter 2017
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Topics Dictionaries Sets Serializing Objects. Topics Dictionaries Sets Serializing Objects.
Michael Ernst CSE 140 University of Washington
Types, Truth, and Expressions (Part 2)
Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg
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
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson CSE 160 University of Washington
Chapter 8 Namespaces and Memory Realities
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson UW CSE 160 Spring 2018
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions
CSE 231 Lab 9.
Introduction to Strings
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Types, Truth, and Expressions (Part 2)
Types, Truth, and Expressions
Intro to Computer Science CS1510 Dr. Sarah Diesburg
Ruth Anderson UW CSE 160 Winter 2016
Presentation transcript:

Sets Intro to Computer Science CS1510, Section 2 Dr. Sarah Diesburg

PA08 Any lingering questions about PA08?

Sets, as in Mathematical Sets In mathematics, a set is a collection of objects, potentially of many different types. In a set, no two elements are identical. That is, a set consists of elements each of which is unique compared to the other elements. There is no order to the elements of a set A set with no elements is the empty set

Creating a Set mySet = set(“abcd”) The “set” keyword creates a set. The single argument that follows must be iterable, that is, something that can be walked through one item at a time with a for. The result is a set data structure: print(mySet) {'a', 'c', 'b', 'd'}

Diverse Elements A set can consist of a mixture of different types of elements: mySet = {‘a’,1, ,True} As long as the single argument can be iterated through, you can make a set of it.

No Duplicates Duplicates are automatically removed. mySet = set(“aabbccdd”) print(mySet) {'a', 'c', 'b', 'd‘}

Common Operators Most data structures respond to these: len(mySet)  the number of elements in a set element in mySet  boolean indicating whether element is in the set for element in mySet:  iterate through the elements in mySet

Set Operators The set data structure provides some special operators that correspond to the operators you learned in middle school. These are various combinations of set contents.

Set Ops, Union mySet=set(“abcd”); newSet=set(“cdef”) mySet.union(newSet) mySet | newSet returns {‘a’,’b’,‘c’,’d’,’e’,’f’} a b c d e f

Set Ops, Intersection mySet=set(“abcd”); newSet=set(“cdef”) mySet.intersection(newSet) mySet & newSet returns {‘c’,’d’} e f c d a b c d

Set Ops, Difference mySet=set(“abcd”); newSet=set(“cdef”) mySet.difference(newSet) mySet – newSet returns {‘a’,’b’} e f a b a b c d

Set Ops, symmetricDifference mySet=set(“abcd”); newSet=set(“cdef”) mySet.symmetric_difference(newSet) mySet ^ newSet returns {‘a’,’b’,’e’,’f’} e f a b a b c d

Set Ops, super and sub set mySet=set(“abc”); newSet=set(“abcdef”) mySet.issubset(newSet) mySet <= newSet returns True a b c d e f

Set Ops, super and sub set mySet=set(“abc”); newSet=set(“abcdef”) newSet.issuperset(mySet) newSet>= mySet returns True a b c d e f

Other Set Ops mySet.add(“g”)  Adds to the set, no effect if item is in set already. mSet.clear()  Empties the set. mySet.remove(“g”) versus mySet.discard(“g”)  remove throws an error if “ g ” isn’t there. discard doesn’t care. Both remove “ g ” from the set. mySet.copy()  Returns a shallow copy of mySet.

Copy vs. Assignment mySet=set(“abc”) myCopy=mySet.copy() myRefCopy=mySet mySet.remove(‘b’) print myCopy print myRefCopy mySet myCopy myRefCopy {‘a’,‘c’} {‘a’,‘b’,‘c’}

A Useful Example Common/Unique Words Code Listings