{"id":24553,"date":"2021-10-21T10:14:55","date_gmt":"2021-10-21T04:44:55","guid":{"rendered":"https:\/\/python-programs.com\/?p=24553"},"modified":"2021-11-05T19:36:59","modified_gmt":"2021-11-05T14:06:59","slug":"python-all-function-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-all-function-with-examples\/","title":{"rendered":"Python all() Function with Examples"},"content":{"rendered":"
In the previous article, we have discussed Program for Python String swapcase() Function<\/a> The function all() returns If all of the items in an iterable are true, it returns True; otherwise, it returns False.<\/p>\n The all() function returns True 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, set, etc.<\/p>\n Note:<\/strong> When applied to a dictionary, the all() function determines whether all of the keys are true, rather than the values.<\/p>\n Return value:<\/strong><\/p>\n The function all() returns:<\/p>\n Note:<\/strong><\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/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 Program for Python String swapcase() Function all() Function in Python: The function all() returns If all of the items in an iterable are true, it returns True; otherwise, it returns False. The all() function returns True if the iterable object is empty. Syntax: all(iterable) Parameters iterable: It may …<\/p>\n
\nall() Function in Python:<\/strong><\/p>\nall(iterable)<\/pre>\n
\n
when 0 is given in the form as string, then it returns true\r\n0 = False\r\n'0' = True\r\nfor example: number = '00000'\r\n# output = True<\/pre>\n
Given list = [0, 1, 2, 3]\r\nGiven tuple = (10, 30, -10)\r\nGiven dictionary = {0: 'good', 1: 'morning'}<\/pre>\n
The result after applying all() function on the given list = False\r\nThe result after applying all() function on the given tuple = True\r\nThe result after applying all() function on the given dictionary = False<\/pre>\n
Given list = ['True', 'True', 'True']\r\nGiven tuple = ()\r\nGiven dictionary = {3: 'welcome', 2: 'to', 1: 'Python-programs'}<\/pre>\n
The result after applying all() function on the given list = True\r\nThe result after applying all() function on the given tuple = True\r\nThe result after applying all() function on the given dictionary = True<\/pre>\n
Program for all() 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 = [0, 1, 2, 3]\r\n# Give the tuple as static input and store it in another variable.\r\ngvn_tupl = (10, 30, -10)\r\n# Give the dictionary as static input and store it in another variable.\r\ngvn_dictnry = {0: 'good', 1: 'morning'}\r\n# Apply all() function on the given list that returns true if all of the\r\n# items in an iterable are true, otherwise, it returns False.\r\n# Store it in another variable.\r\nrslt_lst = all(gvn_lst)\r\n# Apply all() function on the given tuple and store it in another variable.\r\nrslt_tupl = all(gvn_tupl)\r\n# Apply all() function on the given dictionary and store it in another variable.\r\nrslt_dictnry = all(gvn_dictnry)\r\n# Print the result after applying all() function on the given list.\r\nprint(\"The result after applying all() function on the given list = \", rslt_lst)\r\n# Print the result after applying all() function on the given tuple.\r\nprint(\"The result after applying all() function on the given tuple = \", rslt_tupl)\r\n# Print the result after applying all() function on the given dictionary.\r\nprint(\"The result after applying all() function on the given dictionary = \", rslt_dictnry)\r\n<\/pre>\n
The result after applying all() function on the given list = False\r\nThe result after applying all() function on the given tuple = True\r\nThe result after applying all() function on the given dictionary = False<\/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 all() function on the given list that returns true if all of the\r\n# items in an iterable are true, otherwise, it returns False.\r\n# Store it in another variable.\r\nrslt_lst = all(gvn_lst)\r\n# Apply all() function on the given tuple and store it in another variable.\r\nrslt_tupl = all(gvn_tupl)\r\n# Apply all() function on the given dictionary and store it in another variable.\r\nrslt_dictnry = all(gvn_dict)\r\n# Print the result after applying all() function on the given list.\r\nprint(\"The result after applying all() function on the given list = \", rslt_lst)\r\n# Print the result after applying all() function on the given tuple.\r\nprint(\"The result after applying all() function on the given tuple = \", rslt_tupl)\r\n# Print the result after applying all() function on the given dictionary.\r\nprint(\"The result after applying all() function on the given dictionary = \", rslt_dictnry)\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 10 25 35 0\r\nEnter some random Tuple Elements separated by spaces = 1 1 1 1\r\nEnter some random number of keys of the dictionary = 3\r\nEnter key and value separated by spaces = 3 welcome\r\nEnter key and value separated by spaces = 2 to\r\nEnter key and value separated by spaces = 1 Python-programs\r\nThe result after applying all() function on the given list = False\r\nThe result after applying all() function on the given tuple = True\r\nThe result after applying all() function on the given dictionary = True<\/pre>\n
\n