# Python set

## Sets in Python

A set in Python is a collection of objects. Sets are available in Python 2.4 and newer versions. They are different from lists or tuples in that they are modeled after sets in mathematics.

**Related course**

Complete Python Bootcamp: Go from zero to hero in Python

**Set example**

To create a set, we use the set() function.

#!/usr/bin/env python |

If we add the same item element multiple times, they are removed. A set may not contain the same element multiple times.

#!/usr/bin/env python |

## Related Courses:

- Complete Python Bootcamp: Go from zero to hero in Python
- Automate the Boring Stuff with Python Programming

## Simple notation

If you use Python version 2.6 or a later version, you can use a simplified notation:

#!/usr/bin/env python |

## Set Methods

**Clear elements from set**

To remove all elements from sets:

#!/usr/bin/env python |

**Add elements to a set**

To add elements to a set:

#!/usr/bin/env python |

**Remove elements to a set**

To remove elements to a set:

!/usr/bin/env python |

**Difference between two sets**

To find the difference between two sets use:

#!/usr/bin/env python |

Be aware that x.difference(y) is different from y.difference(x).

**Subset**

To test if a set is a subset use:

#!/usr/bin/env python |

**Super-set**

To test if a set is a super-set:

#!/usr/bin/env python |

**Intersection**

To test for intersection, use:

#!/usr/bin/env python |

Posted in Beginner