Bird
Raised Fist0
Pythonprogramming~10 mins

any() and all() functions in Python - Interactive Code Practice

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1.

What does the any() function do in Python?

easy
A. Returns the number of True elements in the iterable
B. Returns True only if all elements in the iterable are True
C. Returns False if any element in the iterable is True
D. Returns True if at least one element in the iterable is True

Solution

  1. Step 1: Understand the purpose of any()

    The any() function checks if at least one element in an iterable is True.
  2. Step 2: Compare with other options

    Returns True only if all elements in the iterable are True describes all(), not any(). Options A, B, and D are incorrect descriptions.
  3. Final Answer:

    Returns True if at least one element in the iterable is True -> Option D
  4. Quick Check:

    any() means at least one True = True [OK]
Hint: Remember: any() means one or more True [OK]
Common Mistakes:
  • Confusing any() with all()
  • Thinking any() counts True elements
  • Assuming any() returns False if one True exists
2.

Which of the following is the correct syntax to check if all elements in a list lst are True?

lst = [True, True, False]
easy
A. all(lst)
B. any(lst)
C. all(lst == True)
D. any(lst == True)

Solution

  1. Step 1: Recall correct usage of all()

    The function all() takes an iterable and returns True if all elements are True. So all(lst) is correct.
  2. Step 2: Analyze other options

    any(lst) uses any(), which checks if any element is True, not all. Options C and D use invalid syntax comparing list to True directly.
  3. Final Answer:

    all(lst) -> Option A
  4. Quick Check:

    Use all(iterable) syntax [OK]
Hint: Use all(iterable) directly, no comparison needed [OK]
Common Mistakes:
  • Writing all(lst == True) which causes error
  • Using any() instead of all()
  • Trying to compare list with True directly
3.

What is the output of the following code?

values = [0, '', None, False]
print(any(values))
print(all(values))
medium
A. True\nTrue
B. True\nFalse
C. False\nFalse
D. False\nTrue

Solution

  1. Step 1: Evaluate any(values)

    All elements are falsy (0, empty string, None, False), so any(values) returns False.
  2. Step 2: Evaluate all(values)

    Since none are True, all(values) returns False.
  3. Final Answer:

    False\nFalse -> Option C
  4. Quick Check:

    Falsy values mean any()=False and all()=False [OK]
Hint: Falsy values make any() and all() both False [OK]
Common Mistakes:
  • Assuming any() returns True if list is not empty
  • Thinking all() returns True if list has falsy values
  • Confusing falsy values with True
4.

Find the error in this code snippet:

nums = [1, 2, 3, 0]
if all(nums > 0):
    print("All positive")
else:
    print("Not all positive")
medium
A. Cannot compare list directly with > operator
B. all() requires a generator expression or iterable of booleans
C. Missing colon after if statement
D. No error, code runs fine

Solution

  1. Step 1: Understand the condition inside all()

    The expression nums > 0 tries to compare a list with an integer, which is invalid in Python.
  2. Step 2: Correct usage of all() with condition

    We should use a generator expression like all(n > 0 for n in nums) to check each element.
  3. Final Answer:

    Cannot compare list directly with > operator -> Option A
  4. Quick Check:

    all() needs iterable of booleans, not list comparison [OK]
Hint: Use all(condition for item in list) for element-wise checks [OK]
Common Mistakes:
  • Trying to compare list directly with >
  • Not using generator expression inside all()
  • Assuming all() works on list comparisons
5.

Given a list of dictionaries representing students' scores, which code correctly checks if all students passed (score >= 50)?

students = [{'name': 'Alice', 'score': 75}, {'name': 'Bob', 'score': 48}, {'name': 'Cara', 'score': 90}]
hard
A. any(student['score'] >= 50 for student in students)
B. all(student['score'] >= 50 for student in students)
C. all(students['score'] >= 50)
D. any(students['score'] >= 50)

Solution

  1. Step 1: Understand the data structure

    Each student is a dictionary with a 'score' key. We must check each student's score.
  2. Step 2: Use all() with generator expression

    all(student['score'] >= 50 for student in students) correctly uses all() with a generator expression to check if every student's score is at least 50.
  3. Step 3: Analyze other options

    any(student['score'] >= 50 for student in students) checks if any student passed, not all. Options C and D try to access 'score' on the list directly, which is invalid.
  4. Final Answer:

    all(student['score'] >= 50 for student in students) -> Option B
  5. Quick Check:

    all() + generator expression for condition on each dict [OK]
Hint: Use all(condition for item in list) for checking all pass [OK]
Common Mistakes:
  • Using any() instead of all() to check all pass
  • Trying to access key on list directly
  • Not using generator expression inside all()