{"id":25053,"date":"2021-11-10T09:37:44","date_gmt":"2021-11-10T04:07:44","guid":{"rendered":"https:\/\/python-programs.com\/?p=25053"},"modified":"2021-11-10T09:37:44","modified_gmt":"2021-11-10T04:07:44","slug":"python-set-isdisjoint-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-set-isdisjoint-method-with-examples\/","title":{"rendered":"Python Set isdisjoint() Method with Examples"},"content":{"rendered":"
Python set() Function with Examples<\/span><\/a><\/p>\n Set isdisjoint() Method in Python:<\/strong><\/p>\n If none of the items are present in both sets, the isdisjoint() method returns true; otherwise, it returns False.<\/p>\n If two sets have no common elements, they are said to be disjoint.<\/p>\n For Example<\/p>\n p={1,2,3,4}<\/p>\n q={5,6,7}<\/p>\n Here p and q are said to be disjoint since they have no common elements.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n set:<\/strong> This is Required. The set to look for items that are similar.<\/p>\n Return Value:<\/strong><\/p>\n This method returns<\/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 <\/p>\n","protected":false},"excerpt":{"rendered":" Prerequisite: Python set() Function with Examples Set isdisjoint() Method in Python: If none of the items are present in both sets, the isdisjoint() method returns true; otherwise, it returns False. If two sets have no common elements, they are said to be disjoint. For Example p={1,2,3,4} q={5,6,7} Here p and q are said to be …<\/p>\nset.isdisjoint(set)<\/pre>\n
\n
Given first set = {'a', 'b', 'c'}\r\nGiven second set = {'p', 'q', 'r'}\r\nGiven third set = {'a', 'g'}<\/pre>\n
Check if first and second sets are disjoint: True\r\nCheck if first and third sets are disjoint: False\r\nCheck if second and third sets are disjoint: True<\/pre>\n
Given first set = {10, 20, 30, 40}\r\nGiven second set = {20, 70, 80}\r\nGiven third set = {100, 200, 300}<\/pre>\n
Check if first and second sets are disjoint: False\r\nCheck if first and third sets are disjoint: True\r\nCheck if second and third sets are disjoint: True<\/pre>\n
Set isdisjoint() Method with Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the first set as static input and store it in a variable.\r\nfst_set = {'a', 'b', 'c'}\r\n# Give the second set as static input and store it in another variable.\r\nscnd_set = {'p', 'q', 'r'}\r\n# Give the third set as static input and store it in another variable.\r\nthrd_set = {'a', 'g'}\r\n# Check if the first and second sets have any common elements using the\r\n# isdisjoint() method and print it.\r\nprint(\"Check if first and second sets are disjoint: \",\r\n fst_set.isdisjoint(scnd_set))\r\n# Check if the first and third sets have any common elements using the\r\n# isdisjoint() method and print it.\r\nprint(\"Check if first and third sets are disjoint: \",\r\n fst_set.isdisjoint(thrd_set))\r\n# Check if the second and third sets have any common elements using the\r\n# isdisjoint() method and print it.\r\nprint(\"Check if second and third sets are disjoint: \",\r\n scnd_set.isdisjoint(thrd_set))\r\n<\/pre>\n
Check if first and second sets are disjoint: True\r\nCheck if first and third sets are disjoint: False\r\nCheck if second and third sets are disjoint: True<\/pre>\n
Method #2: Using Built-in Functions (User Input)<\/h3>\n
\n
# Give the first set as user input using set(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\nfst_set = set(map(int, input(\r\n 'Enter some random Set Elements separated by spaces = ').split()))\r\n# Give the second set as user input using set(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\nscnd_set = set(map(int, input(\r\n 'Enter some random Set Elements separated by spaces = ').split()))\r\n# Give the third set as user input using set(),map(),input(),and split() functions.\r\n# Store it in another variable.\r\nthrd_set = set(map(int, input(\r\n 'Enter some random Set Elements separated by spaces = ').split()))\r\n \r\n# Check if the first and second sets has any common elements using the\r\n# isdisjoint() method and print it.\r\nprint(\"Check if first and second sets are disjoint: \",\r\n fst_set.isdisjoint(scnd_set))\r\n# Check if the first and third sets has any common elements using the\r\n# isdisjoint() method and print it.\r\nprint(\"Check if first and third sets are disjoint: \",\r\n fst_set.isdisjoint(thrd_set))\r\n# Check if the second and third sets has any common elements using the\r\n# isdisjoint() method and print it.\r\nprint(\"Check if second and third sets are disjoint: \",\r\n scnd_set.isdisjoint(thrd_set))\r\n<\/pre>\n
Enter some random Set Elements separated by spaces = 10 20 30 40\r\nEnter some random Set Elements separated by spaces = 20 70 80\r\nEnter some random Set Elements separated by spaces = 100 200 300\r\nCheck if first and second sets are disjoint: False\r\nCheck if first and third sets are disjoint: True\r\nCheck if second and third sets are disjoint: True<\/pre>\n