0
1
2
3
4
for
loopfor in range():
for x in list:
count_vowels
string
argumentdictionary
dictionary
with the count of every lowercase vowel in string
def count_vowels(string):
counts = {"a": 0, "e": 0, "i": 0, "o": 0, "u": 0}
for char in string:
if char in counts:
counts[char] += 1
return counts
def main():
assert count_vowels("") == {"a": 0, "e": 0, "i": 0, "o": 0, "u": 0}
assert count_vowels("banana") == {"a": 3, "e": 0, "i": 0, "o": 0, "u": 0}
print("Passed all tests.")
main()
Passed all tests.
count_chars
string
argumentdictionary
dictionary
with the count of every characters in string
tally_negatives
numbers
as argumentnumbers
to its frequency in numbers
Test cases:
Submit your tally_negatives
function to Gradescope for attendance.
Name your file tally_negatives.py
for k in dictionary:
We can use .values()
to get only the values in a dictionary:
dict_values([10, 25, 27, 10, 5])
We can use .keys()
to get only the keys in a dictionary:
dict_keys(['A', 'B', 'C', 'D', 'E'])
We can use .items()
to get tuples for keys and values:
for x in list
10
25
27
10
5
for key, value in dictionary.items()
keys_and_values
dictionary
as argumentdictionary
Test cases:
merge_dictionaries
dict_1
and dict_2
dict_1
, by adding to it all key-values pairs in dict_2
.update()
Test cases: