0
0
Pythonprogramming~10 mins

Length and iteration methods 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 find the length of the list.

Python
numbers = [1, 2, 3, 4, 5]
length = [1](numbers)
print(length)
Drag options to blanks, or click blank then click option'
Asize
Bcount
Clen
Dlength
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'count' which counts occurrences of a value, not length.
Using 'size' or 'length' which are not Python functions.
2fill in blank
medium

Complete the code to iterate over each character in the string.

Python
word = "hello"
for [1] in word:
    print(letter)
Drag options to blanks, or click blank then click option'
Achar
Bword
Cstring
Dletter
Attempts:
3 left
💡 Hint
Common Mistakes
Using the string variable itself as the loop variable.
Using a variable name not matching the print statement.
3fill in blank
hard

Fix the error in the code to correctly count the number of times 'a' appears in the list.

Python
letters = ['a', 'b', 'a', 'c', 'a']
count_a = letters.[1]('a')
print(count_a)
Drag options to blanks, or click blank then click option'
Acount
Blength
Cindex
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'length' which is not a list method.
Using 'index' which finds the first position, not count.
Using 'find' which is a string method, not list.
4fill in blank
hard

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

Python
words = ['apple', 'bat', 'carrot', 'dog']
lengths = {word: [1] for word in words if [2] > 3}
Drag options to blanks, or click blank then click option'
Alen(word)
Bword
Attempts:
3 left
💡 Hint
Common Mistakes
Using the word itself instead of its length in the condition.
Mixing up keys and values in the dictionary comprehension.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase words as keys, their original words as values, only for words longer than 3 letters.

Python
words = ['apple', 'bat', 'carrot', 'dog']
result = { [1]: [2] for word in words if len(word) [3] 3}
Drag options to blanks, or click blank then click option'
Aword.upper()
Bword
C>
Dlen(word)
Attempts:
3 left
💡 Hint
Common Mistakes
Using the length as a key instead of uppercase word.
Using wrong comparison operator in the condition.