Amazing-Python-Scripts

Форк
0
44 строки · 1.3 Кб
1
''' SETS 
2
    Sets are a collection of unordered elements that are unique.
3
'''
4

5
set = {1, 2, 1, 4, 'A', 'B', 'A', 5}
6

7
print(f"All the Unique elements of Set are {set}", end="\n\n")
8

9

10
''' ADDING ELEMENTS '''
11

12
print("**********Adding Elements**********", end="\n\n")
13

14
set.add('Z')
15

16
print(f"Set after adding Z is {set}", end="\n\n")
17

18
''' 
19
Set Operations 
20
    The union() function combines the data present in both sets.
21
    The intersection() function finds the data present in both sets only.
22
    The difference() function deletes the data present in both and outputs data present only in the set passed.
23
    The symmetric_difference() does the same as the difference() function but outputs the data which is remaining in both sets.
24
'''
25

26
print("**********Set Operations**********", end="\n\n")
27

28
set = {1, 2, 3, 4}
29
set_2 = {3, 4, 5, 6, 'B'}
30

31
print("UNION ", set.union(set_2), '----------', set | set_2, end="\n\n")
32

33
print("INTERSECTION ", set.intersection(set_2),
34
      '----------', set & set_2, end="\n\n")
35

36
print("DIFFERENCE ", set.difference(set_2),
37
      '----------', set - set_2, end="\n\n")
38

39
print("SYMMETRIC-DIFFERENCE ", set.symmetric_difference(set_2),
40
      '----------', set ^ set_2, end="\n\n")
41

42
set.clear()
43

44
print(f"Deleted all elements of set {set}", end="\n\n")
45

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.