Creare set, access values, updates and remove values in python ?
In Python, you can create a set, access values, update the set, and remove values using various methods and operations. Here is how to do each of these tasks:
Creating a Set:
You can create a set by enclosing a comma-separated sequence of elements within curly braces {} or by using the set() constructor.
# Creating a set
my_set = {1, 2, 3, 4, 5}
Accessing Values:
Since sets are unordered, you cannot access elements by index as you would with lists. However, you can check for membership using the in operator to see if a value is present in the set.
# Checking if a value exists in the set
if 3 in my_set:
print("3 is in the set")
Updating a Set:
To add elements to a set, you can use add() method. To add multiple elements, you can use the update() method or use the union (|) operator.
# Adding a single element to the set
my_set.add(6)
# Adding multiple elements to the set
my_set.update({7, 8, 9})
# OR
my_set |= {10, 11, 12}
Removing Values:
To remove an element from a set, you can use the remove() method. If the element is not present in set, it raises a KeyError. To avoid this, you can use the discard() method, which removes the element if it exists and does nothing if it doesn't.
# Removing an element from the set
my_set.remove(3)
# Removing an element safely
my_set.discard(7)
Here's a complete example that demonstrates these operations:
# Creating a set
my_set = {1, 2, 3, 4, 5}
# Accessing values
if 3 in my_set:
print("3 is in the set")
# Updating the set
my_set.add(6)
my_set.update({7, 8, 9})
my_set |= {10, 11, 12}
# Removing values
my_set.remove(3) # Removes 3 from the set
my_set.discard(7) # Removes 7 if present, does nothing if not
After performing these operations, the my_set set will contain the elements {1, 2, 4, 5, 6, 8, 9, 10, 11, 12}.
Creating a Set:
You can create a set by enclosing a comma-separated sequence of elements within curly braces {} or by using the set() constructor.
# Creating a set
my_set = {1, 2, 3, 4, 5}
Accessing Values:
Since sets are unordered, you cannot access elements by index as you would with lists. However, you can check for membership using the in operator to see if a value is present in the set.
# Checking if a value exists in the set
if 3 in my_set:
print("3 is in the set")
Updating a Set:
To add elements to a set, you can use add() method. To add multiple elements, you can use the update() method or use the union (|) operator.
# Adding a single element to the set
my_set.add(6)
# Adding multiple elements to the set
my_set.update({7, 8, 9})
# OR
my_set |= {10, 11, 12}
Removing Values:
To remove an element from a set, you can use the remove() method. If the element is not present in set, it raises a KeyError. To avoid this, you can use the discard() method, which removes the element if it exists and does nothing if it doesn't.
# Removing an element from the set
my_set.remove(3)
# Removing an element safely
my_set.discard(7)
Here's a complete example that demonstrates these operations:
# Creating a set
my_set = {1, 2, 3, 4, 5}
# Accessing values
if 3 in my_set:
print("3 is in the set")
# Updating the set
my_set.add(6)
my_set.update({7, 8, 9})
my_set |= {10, 11, 12}
# Removing values
my_set.remove(3) # Removes 3 from the set
my_set.discard(7) # Removes 7 if present, does nothing if not
After performing these operations, the my_set set will contain the elements {1, 2, 4, 5, 6, 8, 9, 10, 11, 12}.
0 Comments