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

How to use add function in python

藏色散人
Release: 2019-06-22 10:59:32
Original
36739 people have browsed it

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)
Copy after login

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)
Copy after login

The output result is:

{'apple', 'banana', 'orange', 'cherry'}
Copy after login

has been If the element exists, the adding operation will not be performed:

Example 2

fruits = {"apple", "banana", "cherry"}
fruits.add("apple")
print(fruits)
Copy after login

The output result is:

{'apple', 'banana', 'cherry'}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template