in And not in OperatorsI have posted the answers to Quiz 1 here.
I have posted homework 3 here.
It is due this coming Sunday at 11:59 PM.
Are there any questions before I begin?
./ex20.py /usr/bin/python3^M bad interpreter: No such file or directory
dos2unix
dos2unix FILENAME
>>> digit_names = {1 : "one", 2 : "two", 3 : "three"}
>>> digit_names
{1: "one", 2: "two", 3: "three"}
>>> students = {"023413" : "Alan Smith", "01234" : "John Doe"}
>>> students["01234"] "John Doe"
email_addresses["Waldo"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: "Waldo"
>>> empty = {}
>>> empty
{}
email_addresses = {}
>>> email_addresses = {} # create empty dictionarey
>>> email_addresses["Alan"] = "alanh@gmail.com" # add first entry
>>> email_addresses["Chris"] = "chrisk@yahoo.com" # add first entry
>>> email_addresses # print the dictionary
{'Chris': 'chrisk@yahoo.com', 'Alan': 'alanh@gmail.com'}
>>> students
{"01234": "John Doe", "023413": "Alan Smith"}
>>> students["023413"] = "Al Smith"
>>> students
{"01234": "John Doe", "023413": "Al Smith"}
for loop has the following format
for LOOP_VARIABLE in SOME_SORT_OF_LIST:
STATEMENT
STATEMENT
...
>>> nonsense
("foo", "bar", "bletch")
>>> for word in nonsense:
... print(word)
...
foo
bar
bletch
for loops also work with dictionaries
>>> digit_names = {"one":1, "two":2, "three":3, "four":4, "five":5 }
>>> digit_names
{'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5}
>>> for key in digit_names:
... print(key, digit_names[key])
...
one 1
two 2
three 3
four 4
five 5
student_data["09329034"] = ("Jane", "Adams", "jadams", "jadams@yahoo.com")
>>> numbers = [1, 2, 3, 4, 5] >>> numbers [1, 2, 3, 4, 5]
>>> numbers = []
>>> numbers = [] >>> numbers.append(1) >>> numbers.append(2) >>> numbers.append(3) >>> numbers.append(4) >>> numbers.append(5) >>> numbers [1, 2, 3, 4, 5]
>>> numbers[0] = 47 >>> numbers [47, 2, 3, 4, 5]
in And not in Operatorsin operator
>>> numbers [1, 2, 3, 4, 5] >>> 5 in numbers True >>> 6 in numbers False
not in operator tells us whether a value is not found in a list
>>> 5 not in numbers False >>> 6 not in numbers True
not and in together make a single operator
>>> words_integers
{"three": 3, "five": 5, "two": 2, "one": 1, "four": 4}
>>> "three" in words_integers
True
>>> 3 in words_integers
False
del statement
del DICTIONARY_VARIABLE[KEY]
where DICTIONARY_VARIABLE is a variable that
points to the dictionary object
>>> words_integers
{"three": 3, "five": 5, "two": 2, "one": 1, "four": 4}
>>> del words_integers["five"]
>>> words_integers
{"three": 3, "two": 2, "one": 1, "four": 4}
del words_integers["six"]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: "six"
len function can be used with a dictionary
>>> email_addresses
{"Chris": "chrisk@yahoo.com", "Alan": "alanh@gmail.com"}
>>> len(email_addresses)
2
>>> words_integers
{"five": 5, "one": 1, "six": 6, "two": 2, "four": 4, "three": 3}
>>> len(words_integers)
6
for loop on a list, the entries appear in list order
>>> numbs = [ 5, 4, 3, 2, 1] >>> for value in numbs: ... print(value) ... 5 4 3 2 1
>>> email_addresses
{'Fred': 'fredsmith@hotmail.com', 'Chris': 'christhek@gmail.com', 'Alan': 'alanh@gmail.com'}
>>> for name in email_addresses:
... print(name, email_addresses[name])
...
Fred fredsmith@hotmail.com
Chris christhek@gmail.com
Alan alanh@gmail.com
sorted
built-in function
sorted take as its argument an object with multiple entries>>> numbs = [4,5,3,1,2] >>> sorted(numbs) [1, 2, 3, 4, 5]
sorted does not change the order of elements in the original list>>> sorted(email_addresses) ['Alan', 'Chris', 'Fred']
sorted on the dictionary variable in the for header
sorted
for name in sorted(email_addresses): ... print(name, email_addresses[name]) ... Alan alanh@gmail.com Chris christhek@gmail.com Fred fredsmith@hotmail.com