IKH

Operators

Mathematical Operators

union()

  • x.union(y) ⇒ We can use this function to return all elements present in both sets.
  • x.union(y) ⇒ x|y.

Example

Python
x={10,20,30,40}
y={30,40,50,60}
print(x.union(y))
print(x|y)

Output

PowerShell
{40, 10, 50, 20, 60, 30}
{40, 10, 50, 20, 60, 30}

intersection()

  • x.intersection(y) ⇒ x&y.
  • Returns common elements present in both x and y.

Example

Python
x={10,20,30,40}
y={30,40,50,60}
print(x.intersection(y))
print(x&y)

Output

PowerShell
{40, 30}
{40, 30}

difference()

  • x.difference(y) ⇒ x-y.
  • Returns the elements present in x but not in y.

Example

Python
x={10,20,30,40}
y={30,40,50,60}
print(x.difference(y))
print(x-y)
print(y-x)

Output

PowerShell
{10, 20}
{10, 20}
{50, 60}

symmetric_difference()

  • x.symmetric_difference(y) ⇒ x^y.
  • Returns elements present in either x or y but not in both.

Example

Python
x={10,20,30,40}
y={30,40,50,60}
print(x.symmetric_difference(y))
print(x^y)

Output

PowerShell
{10, 50, 20, 60}
{10, 50, 20, 60}

Membership operators

  • To check any element is member of set or not.

Example

Python
s=set("ajay") 
print(s) 
print('a' in s) 
print('z' in s)

Output

PowerShell
{'a', 'j', 'a', 'y'}
True
False

Ungraded Questions

Get ready for an exhilarating evaluation of your understanding! Brace yourself as we dive into the upcoming assessment. Your active participation is key, so make sure to attend and demonstrate your knowledge. Let’s embark on this exciting learning journey together!


Name
Email
Phone

Report an error