Python Tutorial - Master Python Programming For Beginners from Scratch

  • Home
  • Getting Started
  • Basics
  • OOP
  • Advanced Python
  • Tkinter
Home » Python Basics » Python Disjoint Sets

Python Disjoint Sets

Summary: in this tutorial, you’ll learn about disjoint sets and how to use the Python isdisjoint() method to check if two sets are disjoint.

Introduction to Python disjoint sets

Two sets are disjoint when they have no elements in common. In other words, two disjoint sets are sets whose intersection is an empty set.

For example, the {1,3,5} and {2,4,6} sets are disjoint because they have no common elements.

The following Venn diagram illustrates the disjoint sets:

In Python, you use the Set isdisjoint() method to check if two sets are disjoint or not:

set_a.isdisjoint(set_b)
Code language: Python (python)

The isdisjoint() method returns True if the set_a and set_b are disjoint. Otherwise, it returns False.

The isdisjoint() method also accepts any iterable, not just a set.

If you pass a list, a tuple, or a dictionary, the isdisjoint() method will convert it to a set before checking.

Python isdisjoint() method examples

The following example uses the isdisjoint() method to check if the set odd_numbers and set even_numbers are disjoint:

odd_numbers = {1, 3, 5} even_numbers = {2, 4, 6} result = odd_numbers.isdisjoint(even_numbers) print(result)
Code language: Python (python)

Output:

True
Code language: Python (python)

Since no elements in the odd_numbers are present in the set even_numbers, the isdisjoint() method returns True.

The following example uses the isdisjoint() method to check if the set letters and the set alphanumerics are disjoint:

letters = {'A', 'B', 'C'} alphanumerics = {'A', 1, 2} result = letters.isdisjoint(alphanumerics) print(result)
Code language: Python (python)

Output:

False
Code language: Python (python)

It returns False because the letter 'A' in the set alphanumerics is present in the set letters.

The following example passes a list to the isdisjoint() method instead of a set:

letters = {'A', 'B', 'C'} result = letters.isdisjoint([1, 2, 3]) print(result)
Code language: Python (python)

Output:

True
Code language: Python (python)

Summary

  • Two sets are disjoint if they have no element in common.
  • Use Python set isdisjoint() method to check if two sets are disjoint.
  • Was this tutorial helpful ?
  • YesNo
Previous Python issuperset
Next Python for else

Getting Started

  • What is Python
  • Install Python
  • Setup VS Code for Python
  • Develop Python Hello World Program

Python Fundamentals

  • Python Syntax
  • Variables
  • Strings
  • Numbers
  • Boolean
  • Constants
  • Comments
  • Type Conversion

Control Flow

  • if…else
  • Ternary Operator
  • for Loop
  • while Loop
  • break
  • continue
  • pass

Functions

  • Python Functions
  • Default Parameters
  • Keyword Arguments
  • Recursive Functions
  • Lambda Expressions
  • Function Docstrings

Python List

  • List
  • Tuple
  • Sorting a List in Place: sort()
  • Sorting a List: sorted()
  • Slicing a List: [::]
  • Unpacking a List
  • Iterating over a List: for loop
  • Finding Index of an Element: index()
  • Iterables
  • Transform List Elements: map()
  • Filtering List Elements: filter()
  • Reducing List Elements: reduce()
  • List Comprehensions

Python Dictionary

  • Dictionary
  • Dictionary Comprehension

Python Set

  • Set
  • Set Comprehension
  • Union of Sets
  • Intersection of Sets
  • Difference between Sets
  • Symmetric Difference of Sets
  • Subset
  • Superset
  • Disjoint Sets

Exception Handling

  • try…except
  • try…except…finally
  • try…except…else

Python Loop with Else Clause

  • for…else
  • while…else
  • do…while Emulation

More on Functions

  • Unpacking Tuples
  • The *args Parameters
  • The **kwargs Parameters
  • Partial Functions

Modules

  • Modules
  • Module Search Path
  • Python __name__
  • Packages

File I/O

  • Reading a Text File
  • Writing to a Text File
  • Creating a Text File
  • Checking If a File Exists
  • Reading from a CSV File
  • Writing to a CSV File

Managing Third-party Packages

  • Python Package Index (PyPI) & PIP
  • Python Virtual Environments
  • Install pipenv on Windows
  • Using pipenv Tool

About pythontutorial.net

Pythontutorial.net helps you master Python programming from scratch fast.

Site Links

  • Home
  • Contact
  • About
  • Privacy Policy

Recent Python Tutorials

  • Tkinter StringVar
  • Tkinter PhotoImage
  • Tkinter OptionMenu
  • Tkinter ScrolledText
  • How to Change the Appearances of Widgets Dynamically Using Ttk Style map() Method

Python References

  • Python String Methods
  • Python MySQL
  • Python PostgreSQL
  • Python Oracle
  • Python SQLite

Copyright © 2021 · by pythontutorial.net. All rights reserved. Log in