# Python Sets

Python Sets are the collection of multiple data like the list and tuple. The main difference between the list and the Python Sets is that the set elements are unordered and unindexed.

#### Declaration of Python sets

`set_name={'a','d','e'}`

We can also create the set by using a set constructor such as `set_name=set(('a','d','e')) `

In python programming, sets are written with the help of curly braces `{}`

### Printing a set

Printing a set is simple like printing a variable.

#### Example of printing a set

``````my_set = {"PHP", "Python", "Java"}
print(my_set)``````
Tutorials Class - Output Window

{‘PHP’, ‘Java’, ‘Python’}

## Topics to remember about Python Sets

Sets in Python are unordered and have a definite count. We can concatenate the sets, so many things.

### Printing a specific element from the sets

We can’t print the specific value from the set by specifying an index. Because set elements don’t have a specific order and index.
But we can do the so by using condition statement.

#### Example to print a specific element from the set using conditional statement

``````letter_set = {'PHP','Python','Java'}
check_element = 'PHP'
if check_element in letter_set:
output = check_element
print(output)``````
Tutorials Class - Output Window

PHP

### Modifying or updating elements of a set

We can’t modify element from the set directly. If we try to do the so, then python will show the error.

``````my_set = ("PHP", "Python", "java")
my_set = "JavaScript"
print(my_set)``````
Tutorials Class - Output Window

Traceback (most recent call last):
File “even.py”, line 2, in
my_set = “JavaScript”
TypeError: ‘set’ object does not support item assignment

### Python Sets – Adding elements to a set

We can add any number of elements in set directly by using some inbuilt functions.

#### Example to add a single element to the set using add() function

``````my_Set = {"PHP", "Java", "Python"}
# by using add() function, we can add the single element to the set in Python
print(my_Set)``````
Tutorials Class - Output Window

{‘PHP’, ‘Python’, ‘JQuery’, ‘Java’}

#### Example to add multiple elements to the set using update() function

``````# by the help of the update() function , we can add multiple elements to the set.
my_Set.update(["JavaScript", "CSS", "HTML"])
print(my_Set)``````
Tutorials Class - Output Window

{‘PHP’, ‘HTML’, ‘Python’, ‘Java’, ‘CSS’, ‘JavaScript’, ‘JQuery’}

### Copying one set to the other Set

We can easily copy one set to the other by using the copy() method.

#### Example to copy one set to other

``````set_1 = {"PHP", "Java", "Python"}
set_2 = set_1.copy()
print(set_2)``````
Tutorials Class - Output Window

{‘Java’, ‘Python’, ‘PHP’}

### Checking length of a Python set:

With the help of `len()` function we can check the length of the set.

#### Example to calculate the length of a set

``````my_set = {'T','u','t','o','r','i','a','l','s','C','l','a','s','s'}
print(len(my_set))``````
Tutorials Class - Output Window

10

##### Description of output

Here you can see the output is10, but the number of elements are 14. This is because set doesn’t consider duplicate values. If a value is present multiple times, then it will be considered only one time in Python Set.

### Removing the specific element from the set

With the help of `discard()` function, we can remove the specific element from the set. This function will delete both the uppercase and lowercase values(similar to the argument) from the set.

#### Example to remove specific element from the set

``````my_set = {'T','u','t','o','r','i','a','l','s','C','l','a','s','s'}
print(my_set)``````
Tutorials Class - Output Window

{‘u’, ‘C’, ‘T’, ‘s’, ‘i’, ‘l’, ‘r’, ‘a’, ‘o’}

##### Description of output

This function will delete both the uppercase and lowercase values from the set.

### Python Sets – Deleting a set

In Python programming, we can delete the whole set as well.

#### Example to delete the whole set

``````my_set = {"apple", "banana", "cherry"}
del my_set``````
##### Description of output

Here you can see that the output is blank because we have deleted the set. So, it will not return anything.

### Removing elements from the set using clear() function

We can also delete the elements of the set by using a `clear()` function. The main difference between the clear and `delete()` is that clear delete all elements while `delete()` deletes the set completely.

``````my_set = {'T','u','t','o','r','i','a','l','s','C','l','a','s','s'}
print(my_set.clear())``````
Tutorials Class - Output Window

my_set={}

##### Description of output

This is will make the set none.

### Checking if any item exists in Python set

Like list, we can easily check if a element is present in the set or not.

#### Example to check a element is existing in the set or not

``````my_set = {"Ramesh", "Suresh", "Rakesh"}
if "Ramesh" in my_set:
print("Yes, 'Ramesh' is in this set")
else:
print("No, 'Ramesh' is not in this set")``````
Tutorials Class - Output Window

Yes, ‘Ramesh’ is in this set

### Finding the maximum value in the set

Like the list, we can also find the maximum value in a set by using max() function.

#### Example to find out the maximum value from the set

``````my_set = {1,2,2,1,2,3}
print(max(my_set))``````
Tutorials Class - Output Window

3

### Finding the minimum value in the set

With the help of the min() we can also find the minimum value in a set.

#### Example to find out the minimum value from the set

``````my_set = {1,2,2,1,2,3}
print(min(my_set))``````
Tutorials Class - Output Window

1

### Finding the sum of values in the set

We can easily the sum of all the values of a set by using sum() method.

#### Example to calculate the sum of set elements

``````my_set = {1,2,2,1,2,3}
print(sum(my_set))``````
Tutorials Class - Output Window

6

##### Description of output

Here you can see that the sum is 6, because set doesn’t consider duplicate values. If a value is present multiple times, then it will be considered only one time in Python Set.

### Python Sets – Finding the average of elements

Like the list, we can calculate the average easily.

#### Example to calculate the average of the set elements

``````my_set = {1,2,2,1,2,3}
print(sum(my_set)/len(my_set))``````
Tutorials Class - Output Window

2.0

##### Description of output

Here you can see that the average is 2, because set doesn’t consider duplicate values. If a value is present multiple times, then it will be considered only one time in Python Set. (1+2+3)/3.
{"ft_lang_label":"__label__en","ft_lang_prob":0.7577255,"math_prob":0.93525076,"size":5774,"snap":"2022-27-2022-33","text_gpt3_token_len":1511,"char_repetition_ratio":0.16343154,"word_repetition_ratio":0.13233724,"special_character_ratio":0.27866298,"punctuation_ratio":0.14440735,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99472636,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-08-14T03:00:56Z\",\"WARC-Record-ID\":\"<urn:uuid:f575ab4d-fac6-47d3-9688-1e4dcb1c9d44>\",\"Content-Length\":\"91625\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:fd84cf46-fbb1-400e-9d81-fe12df64a735>\",\"WARC-Concurrent-To\":\"<urn:uuid:8f6e8ece-fe4a-47fc-a949-aa77f296c035>\",\"WARC-IP-Address\":\"66.45.244.235\",\"WARC-Target-URI\":\"https://tutorialsclass.com/python-sets/\",\"WARC-Payload-Digest\":\"sha1:XSNR6LKHSUWVHBW4XH4SBVXJUMEHJWG6\",\"WARC-Block-Digest\":\"sha1:UGIYK5DBOXHHY2DN2T55NLNCDOEXNAGX\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-33/CC-MAIN-2022-33_segments_1659882571993.68_warc_CC-MAIN-20220814022847-20220814052847-00726.warc.gz\"}"}