CH09 - Dictionaries and Sets

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

*values()* Method

A dictionary method; returns all the values in the dictionary as a sequence of tuples.

*pop(key, default)* Method

A dictionary method; returns the value associated with a specified key and removes that key-value pair from the dictionary. If the key is not found, the method returns a default value.

*symmetric_difference()* Method

A set method: Given *set1* and *set2* it creates a set that contains the elements that appear in one set but not in both.

*update()* Method

A set method; can add a group of elements to a set.

Multiple Assignment

A way in which more than one variable can be assigned at once in the following way: *k, v = dictionary.popitem()* In this example the key and value from the element retrieved from the dictionary are being stored in the variables "k" and "v" respectively. (This kind of assignment though is not exclusive to dictionaries as we used it in lab once to store multiple variables that had been returned from a function.)

Set

An object that stores a collection of unique values that operates in a similar fashion to a mathematical structure of the same name.

Value

The second part of a dictionary; is mutable.

*len* Function

The built in function that gets the number of elements in a dictionary.

Key Value Pair

The elements of a dictionary are commonly referred to as a ___-_____ ____.

Key

The first part of a dictionary; is immutable; is what one searches for if one wants a specific value in a dictionary.

Delete Key Value Pair General Format

*del dictionary_name[key]*

Dictionary General Format

*dictionary_name = {key_1 : value1, key_2 : value_2, key_3 : value_3,...}* Ex: *phonebook = {'Chris' : '555-1111', 'Katie' : '555-2222', 'Joanne' : '555-3333'}*

Add Key Value Pair General Format

*dictionary_name[key] = value*

Value Retrieval General Format

*dictionary_name[key]*

Dictionary Iteration General Format

*for var in dictionary:* -----*statement* -----*statement* -----etc.

Set Iteration General Format

*for var in set:* -----*statement* -----*statement* -----*etc.*

Intersection Operator General Format

*set1 & set2*

Difference Operator General Format

*set1 - set2*

Superset Operator General Format

*set1 >= set2*

Symmetric Difference Operator General Format

*set1 ^ set2*

*difference()* Method General Format

*set1.difference(set2)*

*intersection()* Method General Format

*set1.intersection(set2)*

*issuperset()* Method General Format

*set1.issuperset(set2)*

*symmetric_difference()* Method General Format

*set1.symmetric_difference(set2)* or *set2.symmetric_difference(set1)*

*union()* Method General Format

*set1.union(set2)*

Union Operator General Format

*set1|set2*

Subset Operator General Format

*set2 <= set1*

*issubset()* Method General Format

*set2.issubset(set1)*

Important Things to Know About Sets

*~*All the elements in a set must be unique. No two elements can have the same value. *~* Sets are unordered, which means that the elements in a set are not stored in any particular order. *~*The elements that are stored in a set can be of different data types.

*clear()* Method

A dictionary method and a set method; empties the contents of a dictionary or a set.

*get(key, default)* Method

A dictionary method; gets the value associated with a specified key. If the key is not found, the method does not raise an exception. Instead, it returns a default value specified in the argument.

*popitem()* Method

A dictionary method; returns a randomly selected key-value pair as a tuple from the dictionary and removes that key-value pair from the dictionary.

*items()* Method

A dictionary method; returns all the keys in a dictionary AND their associated values as a sequence of tuples.

*keys()* Method

A dictionary method; returns all the keys in a dictionary as a sequence of tuples.

*difference()* Method

A set method; Given *set1* and *set2* it creates a set that contains the elements that appear in *set1* but do not appear in *set2*.

*discard()* Method

A set method; Removes the item that was passed in as the argument. Does not raise an exception if the item is not found in the set.

*remove()* Method

A set method; Removes the item that was passed in as the argument. Raises a KeyError exception if the item is not found in the set.

*issubset()* Method

A set method; Returns *True* if the set is a subset of the set in the argument. Returns *False* if otherwise.

*issuperset()* Method

A set method; Returns *True* if the set is a subset of the set in the argument. Returns *False* if otherwise.

*union()* Method

A set method; creates a set which contains all the elements of two sets.

*intersection()* Method

A set method; creates a set which contains only the elements in both of two sets.

*add()* Method

A set method; when used on a set it accepts an argument which is added to the set as another element.

Subset

A set which all of whose elements are contained in another set is said to be the _____ of the other set.

Superset

A set which contains all the elements of another set is said to be the _____ of the other set.

Dictionary View

A special type of sequence obtained from using the items() method on a dictionary that display each element of said dictionary as a tuple.

Dictionary

In Python, a __________ is an object that stores a collection of data. Each element of this object has two parts: a key and a value.

Mapping

Key-value pairs are often referred to as ________s because each key is mapped to a value.

Symmetric Difference

The _____ _____ of two sets is a set the contains the elements that are not shared by the sets. In other words, it is the elements that are in one set but not in both.

Union

The _____ of two sets is the set that contains all the elements of both sets. In other words it is the set of every element that is in Set 1 OR Set 2 (by the logical inclusive OR meaning the element can be in one set, the other set, or BOTH).

Intersection

The _____ of two sets is the set that contains only the elements that are found in both sets. In other words it is the set of every element that is in Set 1 AND Set 2 (by the logical AND meaning the element must be in in both to be true.)

Difference

The _____ of two sets is the set that contains the elements that appear in the first set but do not appear in the second set.


Ensembles d'études connexes

APUSH: Chapter 9: The Confederation and the Constitution, 1776-1790

View Set

History 1302 Unit Two Quiz Questions

View Set

BIO 222 Digestive System HW (Ch. 24/25)

View Set

Ventricles, Meninges, CSF, Blood Vessels of the Brain

View Set

Economics Applications In Business

View Set

Chapter 54 Mastering Biology: Community Ecology

View Set