w3resource

Python: Add member in a set

Python sets: Exercise-3 with Solution

Write a Python program to add member(s) in a set.

Sample Solution:

Python Code:

#A new empty set
color_set = set()
print(color_set)
print("\nAdd single element:")
color_set.add("Red")
print(color_set)
print("\nAdd multiple items:")
color_set.update(["Blue", "Green"])
print(color_set)

Sample Output:

set()

Add single element:
{'Red'}

Add multiple items:
{'Green', 'Red', 'Blue'}

Pictorial Presentation:

NumPy Sets: Add member in a set.

Visualize Python code execution:

The following tool visualize what the computer is doing step-by-step as it executes the said program:


Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

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 Python skills with w3resource's quiz



Python: Tips of the Day

While-else construct:

i = 5

while i > 1:
    print("Whil-ing away!")
    i -= 1
    if i == 3:
        break
else:
    print("Finished up!")