How to use the add function in python?
The add() method in Python is used to add elements to the collection. If the added element already exists in the collection, no operation will be performed.
add() method syntax:
set.add(elmnt)
Parameters
elmnt -- required, the element to be added.
Return value
None.
The following example shows the use of the add() method:
Example 1
fruits = {"apple", "banana", "cherry"} fruits.add("orange") print(fruits)
The output result is:
{'apple', 'banana', 'orange', 'cherry'}
has been If the element exists, the adding operation will not be performed:
Example 2
fruits = {"apple", "banana", "cherry"} fruits.add("apple") print(fruits)
The output result is:
{'apple', 'banana', 'cherry'}
Related recommendations: "Python Tutorial"
The above is the detailed content of How to use add function in python. For more information, please follow other related articles on the PHP Chinese website!