0
0
Pythonprogramming~10 mins

any() and all() functions 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 check if any number in the list is greater than 5 using the any() function.

Python
numbers = [1, 3, 7, 2]
result = any(num [1] 5 for num in numbers)
print(result)
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' instead of '>' which checks for smaller numbers.
Using '==' which checks for equality, not greater than.
2fill in blank
medium

Complete the code to check if all numbers in the list are positive using the all() function.

Python
numbers = [4, 6, 8, 1]
result = all(num [1] 0 for num in numbers)
print(result)
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<' which checks if numbers are less than zero.
Using '==' which checks for equality, not positivity.
3fill in blank
hard

Fix the error in the code to correctly check if any word in the list starts with the letter 'a'.

Python
words = ['apple', 'banana', 'cherry']
result = any(word.[1]('a') for word in words)
print(result)
Drag options to blanks, or click blank then click option'
Astart
Bstartwith
Cstartswith
Dstarts_with
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Misspelling the method name as 'startwith' or 'starts_with'.
Using a non-existent method like 'start'.
4fill in blank
hard

Fill both blanks to create a dictionary of words and their lengths, but only include words longer than 3 characters.

Python
words = ['cat', 'elephant', 'dog', 'lion']
lengths = {word: [1] for word in words if len(word) [2] 3}
print(lengths)
Drag options to blanks, or click blank then click option'
Alen(word)
B<=
C>
Dword
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using '<=' instead of '>' which includes shorter words.
Using 'word' instead of 'len(word)' for the dictionary value.
5fill in blank
hard

Fill both blanks to create a dictionary with uppercase words as keys and their lengths as values, including only words with length greater than 4.

Python
words = ['tree', 'mountain', 'river', 'sky']
result = {word.[1](): [2] for word in words if len(word) > 4}
print(result)
Drag options to blanks, or click blank then click option'
A{
Bupper
Clen(word)
D[
Attempts:
3 left
๐Ÿ’ก Hint
Common Mistakes
Using square brackets instead of curly braces for the dictionary.
Forgetting to call the upper() method with parentheses.
Using 'word' instead of 'len(word)' for the value.