In Python, you can add an item to a Set using the add() method.
In this example,
numbers
containing some integer elements.numbers
to add a new integer element, such as 6
, to the set.numbers
contains the added element 6
.numbers = {1, 2, 3, 4, 5}
numbers.add(6)
print('Numbers:', numbers)
Numbers: {1, 2, 3, 4, 5, 6}
In this example,
fruits
containing some string elements.fruits
to add a new string element, such as 'Orange'
, to the set.fruits
contains the added element 'Orange'
.fruits = {'Apple', 'Banana', 'Cherry'}
fruits.add('Orange')
print('Fruits:', fruits)
Fruits: {'Banana', 'Cherry', 'Orange', 'Apple'}
In this tutorial, we learned How to add an Item to a Set in Python language with well detailed examples.