w3resource

Python Exercise: Check if two given sets have no elements in common

Python sets: Exercise-17 with Solution

Write a Python program to check if two given sets have no elements in common.

Sample Solution:-

Python Code:

x = {1,2,3,4}
y = {4,5,6,7}
z = {8}

print("If two given sets have no elements in common")
print("Compare x and y:")
print(x.isdisjoint(y))
print("Compare x and z:")
print(z.isdisjoint(x))
print("Compare y and z:")
print(y.isdisjoint(z))

Sample Output:

If two given sets have no elements in common
Compare x and y:
False
Compare x and z:
True
Compare y and z:
True

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program to check if a given value is present in a set or not.
Next: Write a Python program to check if a given set is superset of itself and superset of another given set.

What is the difficulty level of this exercise?

Test your Python skills with w3resource's quiz



Python: Tips of the Day

Returns True if there are duplicate values in a flat list, False otherwise

Example:

def tips_duplicates(lst):
  return len(lst) != len(set(lst))

x = [2, 4, 6, 8, 4, 2]
y = [1, 3, 5, 7, 9]
print(tips_duplicates(x))
print(tips_duplicates(y))

Output:

True
False