Easy Tutorial
❮ Ref Math E Python Att Dictionary Setindex ❯

Python Set add() Method

Python Set


Description

The add() method is used to add an element to the set. If the element already exists in the set, no action is taken.

Syntax

Syntax for the add() method:

set.add(elmnt)

Parameters

Return Value

None.

Examples

The following examples demonstrate the use of the add() method:

Example 1

fruits = {"apple", "banana", "cherry"}
fruits.add("orange")
print(fruits)

Output:

{'apple', 'banana', 'orange', 'cherry'}

Adding an existing element does not perform the addition operation:

Example 2

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

Output:

{'apple', 'banana', 'cherry'}

Python Set

❮ Ref Math E Python Att Dictionary Setindex ❯