YouTip LogoYouTip

Python Func Any

Python any() Function body { font-family: Arial, sans-serif; line-height: 1.6; margin: 0; padding: 20px; background-color: #f4f4f4; } .container { max-width: 900px; margin: 0 auto; background: white; padding: 30px; border-radius: 8px; box-shadow: 0 0 10px rgba(0,0,0,0.1); } h1, h2, h3 { color: #333; } h1 { border-bottom: 2px solid #007bff; padding-bottom: 10px; } h2 { color: #007bff; margin-top: 30px; } code { background-color: #f8f9fa; padding: 2px 6px; border-radius: 4px; font-family: 'Courier New', Courier, monospace; } pre { background-color: #f8f9fa; padding: 15px; border-radius: 5px; overflow-x: auto; border: 1px solid #ddd; } .description { background-color: #e7f3ff; padding: 15px; border-left: 4px solid #007bff; margin: 20px 0; } .example { margin: 20px 0; } .example pre { background-color: #f0f0f0; } .note { font-style: italic; color: #666; } a { color: #007bff; text-decoration: none; } a:hover { text-decoration: underline; } .navigation { margin: 20px 0; padding: 10px; background-color: #f9f9f9; border-radius: 5px; } .navigation ul { list-style-type: none; padding: 0; } .navigation li { margin: 5px 0; } .footer { margin-top: 40px; padding-top: 20px; border-top: 1px solid #eee; text-align: center; color: #777; font-size: 0.9em; }

Python any() Function

Description

The any() function is used to check if all elements in the given iterable argument are False. If they are all False, it returns False. If at least one element is True, it returns True.

Elements are considered True unless they are 0, empty, or False.

The function is equivalent to:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

Available in Python 2.5 and above.

Syntax

Here is the syntax for the any() method:

any(iterable)

Parameters

  • iterable -- A tuple or list.

Return Value

Returns False if all elements are empty, 0, or False. Returns True if not all elements are empty, 0, or False.


Examples

The following examples demonstrate the use of the any() method:

>>> any(['a', 'b', 'c', 'd'])  # List, all elements are non-empty or non-zero
True
>>> any(['a', 'b', '', 'd'])   # List, contains an empty element
True
>>> any([0, '', False])        # List, all elements are 0, '', False
False
>>> any(('a', 'b', 'c', 'd')) # Tuple, all elements are non-empty or non-zero
True
>>> any(('a', 'b', '', 'd'))  # Tuple, contains an empty element
True
>>> any((0, '', False))       # Tuple, all elements are 0, '', False
False
>>> any([])                   # Empty list
False
>>> any(())                   # Empty tuple
False
← Python Func IntPython Func Staticmethod β†’