If you have the textbook read Chapter 9, Dictionaries and Sets section 9.2, Sets.
I have posted homework 4 here.
It is due this coming Sunday at 11:59 PM.
I have posted the answers to Quiz 2 here.
Let's review the answers.
Are there any questions before I begin?
bool
conversion function ...if
statements and while
loops>>> value = "" >>> if value: ... print("True") ... else: ... print("False") ... False
>>> value = "x" >>> if value: ... print("True") ... else: ... print("False") ... True
>>> def true_or_false(value): ... if value: ... print("True") ... else: ... print("False") ...
>>> true_or_false(0) False >>> true_or_false(0.0) False
>>> true_or_false(1) True >>> true_or_false(4) True
>>> true_or_false(-1) True >>> true_or_false(-100) True
>>> file = open("students.txt", "r") >>> file <_io.TextIOWrapper name='students.txt' mode='r' encoding='UTF-8'> >>> true_or_false(file) True
None
None
points to nothingNone
is false
>>> true_or_false(None) False
>>> lists_integers = {[1,1]:1, [2,2]:2, [3,3]:3}
Traceback (most recent call last):
File <stdin>, line 1, in <module>
TypeError: unhashable type: 'list'
>>> jumble = {1:1, 2:2.0, 3:"three", 4:(4,4), 5:[5]} >>> for key in jumble: ... print(jumble[key]) ... 1 2.0 three (4, 4) [5]
Method | Description |
---|---|
get(key) | 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. |
pop(key) | 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. |
popitem() | Returns a randomly selected key-value pair as a tuple from the dictionary and removes that key-value pair from the dictionary. |
>>> quiz_scores["Mary Jones"] [98, 95, 93]
>>> quiz_scores["Mary Jonez"]
Traceback (most recent call last):
File <stdin>, line 1, in <module>
KeyError: "Mary Jonez"
>>> quiz_scores.get("Mary Jones") [98, 95, 93]
None
>>> scores = quiz_scores.get("Mary Jonez") >>> print(scores) None
None
in an
if
statement>>> bool(None) False
>>> quiz_scores {"Mary Jones": [98, 95, 93], "Tim Tyler": [88, 81, 79], "John Smith": [100, 90, 85]} >>> quiz_scores.pop("Mary Jones") [98, 95, 93] >>> quiz_scores {"Tim Tyler": [88, 81, 79], "John Smith": [100, 90, 85]}
>>> quiz_scores {"Tim Tyler": [88, 81, 79], "John Smith": [100, 90, 85]} >>> quiz_scores.popitem() ("Tim Tyler", [88, 81, 79]) >>> quiz_scores {"John Smith": [100, 90, 85]}
x ∈ A
A ⊂ B
B ⊃ A
A ∪ B
A ∩ B
A - B
A Δ B
>>> list_1 = [1, 2, 3, 4, 5] >>> type(list_1) <class "list">
>>> nonsense = {"foo", "bar", "bletch"} >>> type(nonsense) <class "set">
set
functionset
takes a single argument for
loopset
>>> num_list = [1,2,3] >>> num_set = set(num_list) >>> num_set {1, 2, 3}
>>> letter_tuple = ("a", "b", "c") >>> letter_set = set(letter_tuple) >>> letter_set {'a', 'c', 'b'}
>>> letter_set_2 = set("abcde") >>> letter_set_2 {'b', 'a', 'c', 'd', 'e'}
>>> numb_set_2 = set({"one": 1, "two": 2, "three": 3}) >>> numb_set_2 {"one", "two", "three"}
set
contains duplicate values>>> letter_set_3 = set("Mississippi") >>> letter_set_3 {"i", "M", "s", "p"}
>>> empty = [] >>> type(empty) <class "list">
>>> empty = {} >>> type(empty) <class "dict">
set
with no arguments
>>> set_1 = set() >>> set_1 set()
>>> set_2 = ()
>>> type(set_2) <class 'tuple'>
empty_set = set()
>>> empty_set set()
>>> set_1 = set() >>> set_1 set()
>>> set_1.add(1) >>> set_1 {1} >>> set_1.add("two") >>> set_1 {1, "two"} >>> set_1.add((3,3,3)) >>> set_1 {(3, 3, 3), 1, "two"}
>>> set_1.add(1) >>> set_1 {(3, 3, 3), 1, "two"}
>>> set_2 = set() >>> set_2 set() >>> set_2.update([1, 2, 3]) >>> set_2 {1, 2, 3} >>> set_2.update("foo") >>> set_2 {1, 2, 3, "f", "o"}
>>> numb_set {1, 2, 3, 4, 5} >>> numb_set.discard(2) >>> numb_set {1, 3, 4, 5} >>> numb_set.remove(4) >>> numb_set {1, 3, 5}
>>> numb_set.discard(2) >>> numb_set {1, 3, 5}
>>> numb_set.remove(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 4
>>> s = set() >>> s set() >>> s.add(1) >>> s.add(2.0) >>> s.add("three") >>> s.add((4,4,4,4)) >>> s {1, 2.0, (4, 4, 4, 4), 'three'}
s.add({})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'dict'
>>> strings = set() >>> strings.add("foo") >>> strings.add("bar") >>> strings.add("bletch") >>> strings.add("ding") >>> strings.add("dong")
>>> strings strings = set() strings.add("foo") strings.add("bar") strings.add("bletch") strings.add("ding") strings.add("dong")