Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to print a simple greeting using NLP.
ML Python
text = "Hello, world!" print([1])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'Text' instead of 'text' causes a NameError.
Putting print(text) inside print() prints the greeting followed by 'None'.
✗ Incorrect
The variable 'text' holds the string to print. Using 'text' prints the greeting.
2fill in blank
mediumComplete the code to split a sentence into words using NLP.
ML Python
sentence = "I love learning NLP" words = sentence.[1](' ') print(words)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'join' causes a TypeError because it expects an iterable.
Using 'replace' causes a TypeError because it requires two arguments.
✗ Incorrect
The split(' ') method breaks the sentence into a list of words separated by spaces.
3fill in blank
hardFix the error in the code to count word frequency.
ML Python
from collections import Counter text = "NLP helps computers understand language" words = text.split() word_counts = Counter([1]) print(word_counts)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Passing 'text' counts characters, not words.
Passing 'Counter' or 'split' counts characters, not words.
✗ Incorrect
The Counter needs a list of words, so we pass 'words', not the original text string.
4fill in blank
hardFill both blanks to create a dictionary of word lengths for words longer than 3 letters.
ML Python
words = ['chat', 'ai', 'language', 'data'] lengths = {word: [1] for word in words if len(word) [2] 3} print(lengths)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' filters shorter words, not longer.
Using 'word' instead of len(word) gives wrong values.
✗ Incorrect
We use len(word) to get length and '>' to filter words longer than 3 letters.
5fill in blank
hardFill all three blanks to create a dictionary of uppercase words and their lengths for words shorter than 6 letters.
ML Python
words = ['hello', 'world', 'python', 'ai'] result = { [1]: [2] for w in words if len(w) [3] 6 } print(result)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' filters longer words, not shorter.
Using 'w' as key does not convert to uppercase.
✗ Incorrect
We use w.upper() as key, len(w) as value, and filter words with length less than 6.