How to Use Set add() Method in PythonPython Basics


The add() method adds a given element to the set if the element is not present in the set, otherwise, it returns False.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

Car = {"Mercedes-Benz", "Rolls-royce", "CADILLAC","Honda","Hyundai"}
print(Car)
Car.add("Land Rover")
print(Car)

Output:

{'Honda', 'Rolls-royce', 'Mercedes-Benz', 'Hyundai', 'CADILLAC'}
{'Honda', 'Rolls-royce', 'Mercedes-Benz', 'Hyundai', 'CADILLAC', 'Land Rover'}

Syntax

set.add(x)

Parameters

Name Description
x The element that is added to the set.

Return Value

It returns a new set with the added element if the element is not present in the set, otherwise, it returns False.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us