Repository navigation
Expand file tree
/
Copy path074-Update-Set.py
More file actions
96 lines (77 loc) · 3.47 KB
/
Copy path074-Update-Set.py
File metadata and controls
96 lines (77 loc) · 3.47 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
"""
===============================================================================
File Name : 74-Update-Set.py
Description : Update a Set in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The update() method is used to add multiple elements from another iterable
such as a set, list, tuple, or string into an existing set.
Syntax:
set_name.update(iterable)
Example:
numbers.update({40, 50})
"""
# -----------------------------------------------------------------------------
# Creating the first set.
# -----------------------------------------------------------------------------
numbers = {10, 20, 30}
# -----------------------------------------------------------------------------
# Displaying the original set.
# -----------------------------------------------------------------------------
print("Original Set :", numbers)
# Output:
# Original Set : {10, 20, 30}
print()
# -----------------------------------------------------------------------------
# Creating another set.
# -----------------------------------------------------------------------------
new_numbers = {40, 50, 60}
# -----------------------------------------------------------------------------
# Updating the first set with another set.
# -----------------------------------------------------------------------------
numbers.update(new_numbers)
# -----------------------------------------------------------------------------
# Displaying the updated set.
# -----------------------------------------------------------------------------
print("After Updating with Another Set :", numbers)
# Output:
# After Updating with Another Set : {10, 20, 30, 40, 50, 60}
print()
# -----------------------------------------------------------------------------
# Creating a list of values.
# -----------------------------------------------------------------------------
extra_numbers = [70, 80]
# -----------------------------------------------------------------------------
# Updating the set with a list.
# -----------------------------------------------------------------------------
numbers.update(extra_numbers)
# -----------------------------------------------------------------------------
# Displaying the updated set.
# -----------------------------------------------------------------------------
print("After Updating with List :", numbers)
# Output:
# After Updating with List : {10, 20, 30, 40, 50, 60, 70, 80}
print()
# -----------------------------------------------------------------------------
# Creating a tuple of values.
# -----------------------------------------------------------------------------
more_numbers = (90, 100)
# -----------------------------------------------------------------------------
# Updating the set with a tuple.
# -----------------------------------------------------------------------------
numbers.update(more_numbers)
# -----------------------------------------------------------------------------
# Displaying the updated set.
# -----------------------------------------------------------------------------
print("After Updating with Tuple :", numbers)
# Output:
# After Updating with Tuple :
# {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}
print()
# -----------------------------------------------------------------------------
# Displaying the total number of elements in the set.
# -----------------------------------------------------------------------------
print("Total Elements :", len(numbers))
# Output:
# Total Elements : 10