- What is wrong with this __lt__ method?
def __lt__(self, other):
if self.name < other.name:
return "True"
else:
return "False"
Ans: The __lt__ method must return a bool value, not an
str value. Both "True" and
"False" both
convert to True when converted to bool values. (Only the empty string
"" converts to False.) Do this instead:
def __lt__(self, other):
if self.name < other.name:
return True
else:
return False
Furthermore, we can replace the if statement with a simpler return statement:
def __lt__(self, other):
return self.name < other.name:
- A dictionary d is defined as d = dict( ),
which is the same as d = { }. Write a statement that adds the value 12
to d using the key "age". Ans:
d["age"] = 12
- How do you check if "address" is being used as a key in the dictionary
d?
Ans: run the statements
print("address" in d, "age" in d)
# Output:
False True
This is because the key "address" is not contained in the dictionary
d but
the key "age" is contained in d.
- Add missing punctuation to these nested dictionaries to make syntactically correct Python code:
persons = \
{ 1001 : { name : Alice,
address : { street : 123 Main Street
state : IL
zipcode : 60019 }
phone : { areacode : 123,
number : 333-4544 }
}
1002 : { name : Bob,
address : { street : 678 Walnut Street
state : MI
zipcode : 48123 }
phone : { areacode : 456,
number : 777-9753 }
}
}
print(persons)
# Corrected version:
persons = \
{ 1001 : { "name" : "Alice",
"address" : { "street" : "123 Main Street",
"state" : "IL",
"zipcode" : 60019 },
"phone" : { "areacode" : 123,
"number" : "333-4544" }
},
1002 : { "name" : "Bob",
"address" : { "street" : "678 Walnut Street",
"state" : "MI",
"zipcode" : 48123 },
"phone" : { "areacode" : 456,
"number" : "777-9753" }
}
}
- After correcting the punctuation in the previous exercise, write statements that
- prints the person with key 1001.
Ans: print(persons[1001])
- prints the address of the person with key 1001.
Ans: print(persons[1001]['address'])
- prints the zipcode of the person with key 1001.
Ans: print(persons[1001]['address']['zipcode'])
- Look at the Pet dictionary example (PetsDict Example).
- What do each of these regular expression tokens mean?
\d \w \A \Z {3} ?
* + | [1-9] [a-z]
Ans:
\d - any digit 0 to 9
\w - word: any digit or letter
\A - beginning of string
\Z - end of string
{3} - repeat three times, e.g., \d{3}
? - repeat 0 or 1 time (optional)
* - repeat 0 or more times
+ - repeat 1 or more times
| - or
[1-9] - digits 1 to 9
- For each regular expression, which of the strings have at least one match?
There may be more than one correct answer for each question.
- \ABS*T+D\Z
(a) ABSSSTDZ *(b)
BTTTTTTD *(c)
BSTD (d)
BTSSSD
- AG+C+T (ACGT are the letters
of the DNA alphabet). There is no \A and
\Z so we are looking to match a substring.
(a) ACGT
*(b)
DUAGGCTU (c)
SRAGTTTP * (d)
WAGCTUUU
- \A([1-9]|11|12)(:[0-5]\d){2}\Z (legal time)
(a) 7:85:13 *(b)
12:31:04 (c)
12:31:4 *(d)
4:12:09
- \A[_a-z][_a-z0-9]*\Z (legal Python local variable names)
*(a) _n234
*(b) num_customers (c)
numCustomers (d) 8abc_xyz
- Write a Python script that checks the results of Exercise 3. Ans:
import re
# Exercise 3a
regex = r"\ABS*T+D\Z"
inputs = ["ABSSSTDZ", "BTTTTTTD", "BSTD", "BTSSSD"]
for string in inputs:
print(string, re.search(regex, string))
print( )
# Exercise 3b
regex = r"AG+C+T"
inputs = ["ACGT", "DUAGGCTU", "SRAGTTTP", "WAGCTUUU"]
for string in inputs:
print(string, re.search(regex, string))
print( )
# Exercise 3c
regex = r"\A([1-9]|11|12)(:[0-5]\d){2}\Z"
inputs = ["7:85:13", "12:31:04", "12:31:4", "4:12:09"]
for string in inputs:
print(string, re.search(regex, string))
print( )
# Exercise 3d
regex = r"\A[_a-z][_a-z0-9]*\Z"
inputs = ["n234", "num_customers", "numCustomers", "8abc_xyz"]
for string in inputs:
print(string, re.search(regex, string))