any() in PythonImagine a situation where we must check 100 conditions to operate. Traditionally, we use conditional statements (if, if-else, elif, nested if) to check if conditions are true. But, for that many conditions, the code becomes lengthy and too many if statements take away the readability of the code. For such situations, the programmer's choice has to be to use Python's any() function. This tutorial explains the use and functionality of the any () function with examples. Example for any():Output: True False True Understanding:In the three cases in the above code, any() returned False only in the second scenario where all the conditions are False. The function requires at least one condition to be True to return True. Mechanism:Lazy evaluation: When we give more than one expression/value to the function, it keeps evaluating each expression from left to right, and once it finds a True expression, it stops evaluation and returns True. It won't even check the succeeding expressions. Syntax: iterable: Any object that can be iterated using loops like a tuple, list, dictionary, set etc. (int, float etc., are not iterable).
Equivalent Boolean values:
Sample Code: Output: 0: False Some non-zero integer: True 0.0: False Some non-zero float: True Empty string: False Non-empty string: True Empty list: False Non-empty list: True None: False
Example usage Scenarios: Generally, if a code is successful, it returns 0; if any unexpected error occurs, it returns some number. Imagine running many test cases and seeing if all the test cases returned 0. We can use any() to see if any test case returned a non-zero value. Code: Output: Find if all the test cases returned zero At least one test case returned a non-zero number To find if any non-empty string is included in a set of empty strings: Code: Output: Find if any empty string is created by accident Atleast one character is included in some string any with lists, tuples and sets: Example: Output: Lists: [0, 0, 0.0, ''] : False [1, 4, 9.0, 'Hi'] : True Tuples: (0, 0, 0.0, '') : False (1, 4, 9.0, 'Hi') : True Sets: {0, ''} : False {1, 4, 9.0, 'Hi'} : True any() with Dictionary:In the case of a dictionary, any checks if at least one key in the dictionary is True. If all the keys are False, it returns False.
Example: Output: Dictionary: {0: 'Hey'} : False {} : False {0: '', 1: 'Hello'} : True any() with a Condition:We can use the any () function to check if at least one element in the iterable satisfies a specified condition. Example: Output: The list: [0, -4, -3, 2, -1] At least one element in the list is greater than 0 OR and any:'OR' is one of Python's Boolean operators. It returns true if at least one condition evaluates to True. Both OR and any follow the lazy approach, and both have the same type of functionality and are not quite the same. Here are some examples showing the similarity between both. Example: Output: True True True True Here are the differences:
Example: Output: True Hi
any() without using any()It is also important to understand how to implement the functionality of any () without actually using the function:
Example: Output: The list: [0, -4, -3, 2, -1] True
Example: Output: The list: [0, -4, -3, 2, -1] True Next TopicArguments and Parameters in Python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India