Python Sets With Examples

A Python Set is a Collection. Collections in Python include the following: List, Sets, Dictionary and Tuple. Let us now see the key points to understand Sets in Python:

  • A Set is a collection in Python
  • Set items i.e. elements are placed inside curly braces {}
  • Unordered and Unchangeable
  • Unchangeable, but you can add/ remove an item
  • Duplicate values aren’t allowed in a Set

Create a Set in Python

To create a set, place the elements inside curly brackets, separated by comma.

Let us see an example wherein we will create a Set:

The output is as follows:

Create a set with different data types

In this example, we will learn how to create a set with different data types:

The output is as follows:

Loop through the Set and Print all the values

In this example, loop through the set and display the entire set:

The output is as follows:

Get the length of a Set

In this example, we will get the length of a Set i.e. the total number of elements using the len() method:

The output is as follows:

How to access items in Python Sets

In this example, we will learn how to access any item from a Set:

The output is as follows:

Add an item to a Python Set

In this example, we will learn how to add an item to a Set in Python:

The output is as follows:

Add items from another set into the current set (Update)

In this example, we will learn how to add items from another set to the current set:

The output is as follows:

Remove an item from the Set using remove()

In this example, we will remove an item from the Set using remove():

The output is as follows:

Remove an item from the Set using discard()

In this example, we will remove an item from the Set using discard():

The output is as follows:

Empty the Set

In this example, learn how to empty a Set in Python using the clear() method:

The output is as follows:

Delete the complete Set

In this example, learn how to delete a Set in Python using the del() method:

The output is as follows:

Join two Sets in Python

In this example, we will learn how to join two sets in Python:

The output is as follows:

Return the Difference between two or more sets

In this example, we will learn how to return the Difference between two or more sets using the difference() method:

The output is as follows:

Keep only the duplicate items in two sets

In this example, we will keep only the duplicate items in two sets using the intersection_update() method in Python:

The output is as follows:

Keep all the items in the two sets except the duplicates

In this example, keep all the items in the two sets except the duplicates using the symmetric_difference_update() method in Python:

The output is as follows:

Make a copy of the Set

To make a copy of the Set, use the copy() method:

The output is as follows:

Union of Sets

To get the Union of Sets in Python, use the union() method

The output is as follows:

In this tutorial, we learned what are Sets in Python. Moreover, we worked around creating Python Sets and their operations like adding, deleting, updating, union, joining, etc. Clearly, it’s quite easy to work on Python Sets. Apart from this, if you are aware of a topic we missed in the Set, then kindly mention it in the comment section.

Python Tutorial (English)

If you liked the tutorial, spread the word and share the link and our website Studyopedia with others.


For Videos, Join Our YouTube Channel: Join Now


Read More:

Python Classes and Objects
Python Tokens
Studyopedia Editorial Staff
contact@studyopedia.com

We work to create programming tutorials for all.

2 Comments
  • manisha roka
    Posted at 09:23h, 25 December Reply

    I would like to learn python from scratch. I need lectures. And certificates. Is there any certificates available in this site or I have to get it from Coursera, udemy !!! Can you please recommend me !!! I find easy studying from this site. Is this enough for python or i have to learn from udemy !!!! Thank You !!!

Post A Comment

Discover more from Studyopedia

Subscribe now to keep reading and get access to the full archive.

Continue reading