Home >Backend Development >Python Tutorial >How to use add function in python

How to use add function in python

藏色散人
藏色散人Original
2019-06-22 10:59:3236716browse

How to use add function in python

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn