{"id":24549,"date":"2021-10-21T10:13:05","date_gmt":"2021-10-21T04:43:05","guid":{"rendered":"https:\/\/python-programs.com\/?p=24549"},"modified":"2021-11-05T19:37:25","modified_gmt":"2021-11-05T14:07:25","slug":"python-any-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-any-function-with-examples\/","title":{"rendered":"Python any() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python Program for ascii() Function<\/a> The function any() returns True if any of the items in an iterable are true; otherwise, False.<\/p>\n The any() function returns False if the iterable object is empty.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n iterable: <\/strong>It may be any iterable object like list, tuple, dictionary, etc.<\/p>\n Note:<\/strong> When applied to a dictionary, the any() function determines whether any of the keys are true, rather than the values.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Find a Comprehensive Collection of Python Built in Functions<\/a> that you need to be aware of and use them as a part of your program.<\/p>\n In the previous article, we have discussed Python Program for ascii() Function any() Function in Python: The function any() returns True if any of the items in an iterable are true; otherwise, False. The any() function returns False if the iterable object is empty. Syntax: any(iterable) Parameters iterable: It may be any iterable object like …<\/p>\n
\n any() Function in Python:<\/strong><\/p>\nany(iterable)<\/pre>\n
Given list = []\r\nGiven tuple = (1, 2, 0, 0)\r\nGiven dictionary = {1: 'hello', 2: 'this is', 0: 'btechgeeks'}<\/pre>\n
The result after applying any() function on the given list = False\r\nThe result after applying any() function on the given tuple = True\r\nThe result after applying any() function on the given dictionary = True<\/pre>\n
Given list = [0, 1, 0, 1]\r\nGiven tuple = (0, 0, 0)\r\nGiven dictionary = {0: 'good', 0: 'morning'}<\/pre>\n
The result after applying any() function on the given list = True\r\nThe result after applying any() function on the given tuple = False\r\nThe result after applying any() function on the given dictionary = False<\/pre>\n
Program for any() Function in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the list as static input and store it in a variable.\r\ngvn_lst = []\r\n# Give the tuple as static input and store it in another variable.\r\ngvn_tupl = (1, 2, 0, 0)\r\n# Give the dictionary as static input and store it in another variable.\r\ngvn_dictnry = {1: 'hello', 2: 'this is', 0: 'btechgeeks'}\r\n# Apply any() function on the given list that returns True if any of the items\r\n# in an iterable are true; otherwise, False.\r\n# Store it in another variable.\r\nrslt_lst = any(gvn_lst)\r\n# Apply any() function on the given tuple and store it in another variable.\r\nrslt_tupl = any(gvn_tupl)\r\n# Apply any() function on the given dictionary and store it in another variable.\r\nrslt_dictnry = any(gvn_dictnry)\r\n# Print the result after applying any() function on the given list.\r\nprint(\"The result after applying any() function on the given list = \", rslt_lst)\r\n# Print the result after applying any() function on the given tuple.\r\nprint(\"The result after applying any() function on the given tuple = \", rslt_tupl)\r\n# Print the result after applying any() function on the given dictionary.\r\nprint(\"The result after applying any() function on the given dictionary = \", rslt_dictnry)\r\n<\/pre>\n
The result after applying any() function on the given list = False\r\nThe result after applying any() function on the given tuple = True\r\nThe result after applying any() function on the given dictionary = True<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the list as User input using list(),map(),input(),and split() functions\r\n# and store it in a variable.\r\ngvn_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Give the tuple as User input using list(),map(),input(),and split() functions\r\n# and store it in another variable.\r\ngvn_tupl = tuple(map(int, input(\r\n 'Enter some random Tuple Elements separated by spaces = ').split()))\r\n# Take a dictionary and initialize it with an empty dictionary using dict() or {}.\r\ngvn_dict = {}\r\n# Give the number of keys as user input using int(input()) and store it in a variable.\r\nnumb_of_kys = int(\r\n input('Enter some random number of keys of the dictionary = '))\r\n# Loop till the given number of keys using for loop.\r\nfor p in range(numb_of_kys):\r\n # Inside the for loop scan the key and value as\r\n # user input using input(),split() functions\r\n # and store them in two separate variables.\r\n keyy, valuee = input(\r\n 'Enter key and value separated by spaces = ').split()\r\n # Initialize the key with the value of the dictionary.\r\n gvn_dict[int(keyy)] = valuee\r\n\r\n# Apply any() function on the given list that returns True if any of the items\r\n# in an iterable are true; otherwise, False.\r\n# Store it in another variable.\r\nrslt_lst = any(gvn_lst)\r\n# Apply any() function on the given tuple and store it in another variable.\r\nrslt_tupl = any(gvn_tupl)\r\n# Apply any() function on the given dictionary and store it in another variable.\r\nrslt_dictnry = any(gvn_dict)\r\n# Print the result after applying any() function on the given list.\r\nprint(\"The result after applying any() function on the given list = \", rslt_lst)\r\n# Print the result after applying any() function on the given tuple.\r\nprint(\"The result after applying any() function on the given tuple = \", rslt_tupl)\r\n# Print the result after applying any() function on the given dictionary.\r\nprint(\"The result after applying any() function on the given dictionary = \", rslt_dictnry)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 0 1 0 1\r\nEnter some random Tuple Elements separated by spaces = 0 0 0\r\nEnter some random number of keys of the dictionary = 2\r\nEnter key and value separated by spaces = 0 good\r\nEnter key and value separated by spaces = 0 value\r\nThe result after applying any() function on the given list = True\r\nThe result after applying any() function on the given tuple = False\r\nThe result after applying any() function on the given dictionary = False<\/pre>\n
\n