Please login

Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Python Set add()

Add() in Python sets

 

The add() method adds the given element to the set.

If the element is already present in a set then it does not add anything.

Python Set add()

Syntax :

set1.add(element1)

add() method takes a single parameter.

Return Type :

add() method does not return any value. It returns None.


#create set
#output : {1, 2, 3, 4, 5}
mySet = { 12345 }
print(mySet)

#insert single element using add()
#”PrepInsta” will be added to the set
#output : {‘PrepInsta’, 1, 2, 3, 4, 5}
mySet.add(“PrepInsta”)
print(mySet)

#adding the element which is already present in the set
#output : {‘PrepInsta’, 1, 2, 3, 4, 5}
mySet.add(1)
print(mySet)

Output :

{1, 2, 3, 4, 5}

{'PrepInsta', 1, 2, 3, 4, 5}

{'PrepInsta', 1, 2, 3, 4, 5}