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