0
0
Pythonprogramming~10 mins

Iterating over lists in Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to print each fruit in the list.

Python
fruits = ['apple', 'banana', 'cherry']
for [1] in fruits:
    print([1])
Drag options to blanks, or click blank then click option'
Afruit
Bfruits
Citem
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using the list name instead of a variable for each element.
Using a reserved word like 'list' as a variable.
2fill in blank
medium

Complete the code to sum all numbers in the list.

Python
numbers = [1, 2, 3, 4]
total = 0
for num in numbers:
    total += [1]
print(total)
Drag options to blanks, or click blank then click option'
Anum
Bnumbers
Ctotal
Dnumber
Attempts:
3 left
💡 Hint
Common Mistakes
Adding the whole list instead of one number.
Using a variable that is not defined in the loop.
3fill in blank
hard

Fix the error in the loop to print each character in the word.

Python
word = 'hello'
for [1] in word:
    print(char)
Drag options to blanks, or click blank then click option'
Aword
Bstring
Cletter
Dchar
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different variable name in the loop and print.
Using the whole string as the loop variable.
4fill in blank
hard

Fill both blanks to create a dictionary with words as keys and their lengths as values for words longer than 3 letters.

Python
words = ['cat', 'house', 'tree', 'a', 'dog']
lengths = { [1]: [2]([1]) for [1] in words if len([1]) > 3 }
Drag options to blanks, or click blank then click option'
Aword
Blen
Clength
Dsize
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using a non-existent function like length() or size().
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys and their lengths as values for words longer than 2 letters.

Python
words = ['hi', 'hello', 'yes', 'no']
result = { [1]: [2] for [3] in words if len([3]) > 2 }
Drag options to blanks, or click blank then click option'
Aword.upper()
Blen(word)
Cword
Dword.lower()
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing variable names in the loop and dictionary.
Using lower() instead of upper() for keys.