For loops#
1. Fill the missing pieces#
Fill the ____
parts in the code below.
words = ['PYTHON', 'JOHN', 'chEEse', 'hAm', 'DOE', '123']
upper_case_words = []
for ____ in words:
if ____.isupper():
____.append(____)
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
Cell In[1], line 6
4 for ____ in words:
5 if ____.isupper():
----> 6 ____.append(____)
AttributeError: 'str' object has no attribute 'append'
assert upper_case_words == ['PYTHON', 'JOHN', 'DOE']
2. Calculate the sum of dict values#
Calculate the sum of the values in magic_dict
by taking only into account numeric values (hint: see isinstance).
magic_dict = dict(val1=44, val2='secret value', val3=55.0, val4=1)
# Your implementation
sum_of_values =
assert sum_of_values == 100
3. Create a list of strings based on a list of numbers#
The rules:
If the number is a multiple of five and odd, the string should be
'five odd'
If the number is a multiple of five and even, the string should be
'five even'
If the number is odd, the string is
'odd'
If the number is even, the string is
'even'
numbers = [1, 3, 4, 6, 81, 80, 100, 95]
# Your implementation
my_list =
assert my_list == ['odd', 'odd', 'even', 'even', 'odd', 'five even', 'five even', 'five odd']