- Related Questions & Answers
- What is the difference between equals() method and == operator in java?
- Difference between == and is operator in python.
- Explain difference between == and is operator in Python.
- What is the difference between java method and native method?
- What is the difference between on, over, and above?
- What is the difference between MySQL ISNULL() function and IS NULL operator?
- What is the difference between the dot (.) operator and -> in C++?
- What is the difference between new operator and object() constructor in JavaScript?
- What is the significant difference between MySQL LIKE and equal to (=) operator?
- What is the difference between del, remove and pop on lists in python ?
- What is the difference between working of append and + operator in a list in Python?
- What is the difference between a method and a function?
- What is the difference between = and == operators in Python?
- What is the difference between a Java method and a native method?
- What is the difference between method hiding and method overriding in Java?

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

Python's set object represents built-in set class. Different set operations such as union, intersection, difference and symmetric difference can be performed either by calling corresponding methods or by using operators.

>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1.union(s2) {1, 2, 3, 4, 5, 6, 7, 8} >>> s2.union(s1) {1, 2, 3, 4, 5, 6, 7, 8}

>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1|s2 {1, 2, 3, 4, 5, 6, 7, 8}

>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1.intersection(s2) {4, 5} >>> s2.intersection(s1) {4, 5}

>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1&s2 {4, 5} >>> s2&s1 {4, 5}

>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1.difference(s2) {1, 2, 3} >>> s2.difference(s1) {8, 6, 7}

>>> s1={1,2,3,4,5} >>> s2={4,5,6,7,8} >>> s1-s2 {1, 2, 3} >>> s2-s1 {8, 6, 7}

Advertisements