-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
sets_operations.py
83 lines (58 loc) · 1.65 KB
/
sets_operations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
__author__ = 'Avinash'
# set of all numbers from 1 to 10
numbers = set()
# set of all even numbers between 1 to 10
even_numbers = set()
# set of all odd numbers between 1 to 10
odd_numbers = set()
# set of all prime numbers between 1 to 10
prime_numbers = set()
# set of all composite numbers between 1 to 10
composite_numbers = set()
# function which finds even and odd numbers
def even_odd_sets(num):
if num % 2 == 0:
even_numbers.add(num)
else:
odd_numbers.add(num)
# function which finds prime and composite numbers
def prime_composite_sets(num):
if num > 1:
for j in range(2, num):
if (num % j) == 0:
composite_numbers.add(num)
break
else:
prime_numbers.add(num)
for i in range(1, 11):
numbers.add(i)
even_odd_sets(i)
prime_composite_sets(i)
print("\nNumbers Set: ", numbers)
print("Even Numbers Set: ", even_numbers)
print("Odd Numbers Set: ", odd_numbers)
print("Prime Numbers Set: ", prime_numbers)
print("Composite Numbers Set: ", composite_numbers)
# SET OPERATIONS
# Length of the set
print("\n\nLength of set numbers:",
len(numbers))
# Intersection of sets
print("Intersection of numbers and even_numbers:",
numbers.intersection(even_numbers))
# Union of sets
print("Union of prime_numbers and composite_numbers:",
prime_numbers.union(composite_numbers))
# Difference of sets
print("Difference between numbers and prime_numbers:",
numbers - prime_numbers)
example = set(['test', 43, 'another', 120])
print("\nExample Set:", example)
# Remove element in a set
example.remove(120)
print(example)
example.discard(43)
print(example)
# Clear the set
example.clear()
print(example)