Python: Add member in a set
Python sets: Exercise-3 with Solution
Write a Python program to add member(s) to a set.
Visual Presentation:
Sample Solution:
Python Code:
# Create a new empty set 'color_set':
color_set = set()
# Print the empty set 'color_set':
print(color_set)
# Print a newline for separation:
print("\nAdd single element:")
# Add a single element "Red" to the 'color_set':
color_set.add("Red")
# Print the 'color_set' with the added element "Red":
print(color_set)
# Print a newline for separation:
print("\nAdd multiple items:")
# Add multiple elements "Blue" and "Green" to the 'color_set' using the 'update' method:
color_set.update(["Blue", "Green"])
# Print the 'color_set' with the added elements "Blue" and "Green":
print(color_set)
Sample Output:
set() Add single element: {'Red'} Add multiple items: {'Green', 'Red', 'Blue'}
Python Code Editor:
Previous: Write a Python program to iteration over sets.
Next: Write a Python program to remove item(s) from a given set.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://198.211.115.131/python-exercises/sets/python-sets-exercise-3.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics