Repository navigation
Expand file tree
/
Copy path076-Discard-Element-from-Set.py
More file actions
80 lines (64 loc) · 2.84 KB
/
Copy path076-Discard-Element-from-Set.py
File metadata and controls
80 lines (64 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
"""
===============================================================================
File Name : 76-Discard-Element-from-Set.py
Description : Discard an Element from a Set in Python
Author : Shaik Mahaboob Basha
===============================================================================
Definition:
The discard() method is used to remove a specified element from a set.
If the specified element is not present, no error occurs.
Syntax:
set_name.discard(element)
Example:
numbers.discard(30)
"""
# -----------------------------------------------------------------------------
# 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()
# -----------------------------------------------------------------------------
# Discarding the element 30 from the set.
# -----------------------------------------------------------------------------
numbers.discard(30)
# -----------------------------------------------------------------------------
# Displaying the updated set.
# -----------------------------------------------------------------------------
print("After Discarding 30 :", numbers)
# Output:
# After Discarding 30 : {10, 20, 40, 50}
print()
# -----------------------------------------------------------------------------
# Discarding another element from the set.
# -----------------------------------------------------------------------------
numbers.discard(50)
# -----------------------------------------------------------------------------
# Displaying the updated set.
# -----------------------------------------------------------------------------
print("After Discarding 50 :", numbers)
# Output:
# After Discarding 50 : {10, 20, 40}
print()
# -----------------------------------------------------------------------------
# Attempting to discard an element that is not present in the set.
# -----------------------------------------------------------------------------
numbers.discard(100)
# -----------------------------------------------------------------------------
# Displaying the set after attempting to discard a non-existing element.
# -----------------------------------------------------------------------------
print("After Discarding 100 :", numbers)
# Output:
# After Discarding 100 : {10, 20, 40}
print()
# -----------------------------------------------------------------------------
# Displaying the total number of elements in the set.
# -----------------------------------------------------------------------------
print("Total Elements :", len(numbers))
# Output:
# Total Elements : 3