Repository navigation
Expand file tree
/
Copy path073-Add-Element-to-Set.py
More file actions
81 lines (65 loc) · 2.84 KB
/
Copy path073-Add-Element-to-Set.py
File metadata and controls
81 lines (65 loc) · 2.84 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
"""
===============================================================================
File Name : 73-Add-Element-to-Set.py
Description : Add an Element to a Set in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The add() method is used to add a single element to a set. If the element
already exists, it will not be added again because sets store only unique
values.
Syntax:
set_name.add(element)
Example:
numbers.add(60)
"""
# -----------------------------------------------------------------------------
# Creating a set of integers.
# -----------------------------------------------------------------------------
numbers = {10, 20, 30, 40, 50}
# -----------------------------------------------------------------------------
# Displaying the original set.
# -----------------------------------------------------------------------------
print("Original Set :", numbers)
# Output:
# Original Set : {10, 20, 30, 40, 50}
print()
# -----------------------------------------------------------------------------
# Adding a new element to the set.
# -----------------------------------------------------------------------------
numbers.add(60)
# -----------------------------------------------------------------------------
# Displaying the set after adding the new element.
# -----------------------------------------------------------------------------
print("After Adding 60 :", numbers)
# Output:
# After Adding 60 : {10, 20, 30, 40, 50, 60}
print()
# -----------------------------------------------------------------------------
# Adding another element to the set.
# -----------------------------------------------------------------------------
numbers.add(70)
# -----------------------------------------------------------------------------
# Displaying the updated set.
# -----------------------------------------------------------------------------
print("After Adding 70 :", numbers)
# Output:
# After Adding 70 : {10, 20, 30, 40, 50, 60, 70}
print()
# -----------------------------------------------------------------------------
# Attempting to add a duplicate element.
# -----------------------------------------------------------------------------
numbers.add(30)
# -----------------------------------------------------------------------------
# Displaying the set after attempting to add a duplicate element.
# -----------------------------------------------------------------------------
print("After Adding Duplicate 30 :", numbers)
# Output:
# After Adding Duplicate 30 : {10, 20, 30, 40, 50, 60, 70}
print()
# -----------------------------------------------------------------------------
# Displaying the total number of elements in the set.
# -----------------------------------------------------------------------------
print("Total Elements :", len(numbers))
# Output:
# Total Elements : 7