Python Program for set add() Method

In the previous article, we have discussed Python Program for Dictionary items() Function
Python set:

Python set is a list of items that are not ordered. – Each element in the set must be unique and immutable, and duplicate elements are removed from the sets. Sets are mutable, which means they can be changed after they have been created.

The elements of the set, unlike other Python sets, do not have an index, which means we cannot access any element of the set directly using the index. To get the list of elements, we can either print them all at once or loop them through the collection.

Examples:

Example1:

Input:

Given set =  {'hello', 'this', 'is'}
Value to be added = 'btechgeeks'

Output:

The given set is :
{'this', 'is', 'hello'}
The set is after a given element to the above given set :
{'this', 'is', 'hello', 'btechgeeks'}

Example2:

Input:

Given set = {'good', 'morning', 'this', 'is', 'btechgeeks'}
Value to be added = 'hello'

Output:

The given set is :
{'this', 'btechgeeks', 'good', 'is', 'morning'}
The set is after a given element to the above given set :
{'this', 'hello', 'btechgeeks', 'good', 'is', 'morning'}

Program for Set add() Method in Python

add() Function in set:

The set add() function in Python is used to add a new item to an existing set.

The syntax is as follows:

set_Name.add(element)

The python set add() function only accepts one parameter. You can, however, use a tuple as an argument. Remember that you cannot use this add() method to add an existing (duplicate value) to a set.

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Add a new value to the given set by using the add() function.
  • Print the above-given set after adding a given new value.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {'hello', 'this', 'is'}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Add a new value to the given set by using the add() function.
gven_set.add('btechgeeks')
# Print the above-given set after adding a given new value.
print("The set is after a given element to the above given set :")
print(gven_set)

Output:

The given set is :
{'this', 'is', 'hello'}
The set is after a given element to the above given set :
{'this', 'is', 'hello', 'btechgeeks'}

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the set as user input using the set(), input(), and split() functions.
  • Store it in a variable.
  • Print the above-given set.
  • Give the value to be added as user input using the input() function and store it in another variable.
  • Add the above value to the given set by using the add() function.
  • Print the above-given set after adding a given new value.
  • The Exit of Program.

Below is the implementation:

# Give the set as user input using the set(), input(), and split() functions.
# Store it in a variable.
gven_set = set(input("Enter some random values separated by spaces = ").split())
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Give the value to be added as user input using the input() function and 
# store it in another variable.
k=input("Enter some random value = ")
# Add the above value to the given set by using the add() function.
gven_set.add(k)
# Print the above-given set after adding a given new value.
print("The set is after a given element to the above given set :")
print(gven_set)

Output:

Enter some random values separated by spaces = good morning this is btechgeeks
The given set is :
{'this', 'btechgeeks', 'good', 'is', 'morning'}
Enter some random value = hello
The set is after a given element to the above given set :
{'this', 'hello', 'btechgeeks', 'good', 'is', 'morning'}

II) add Tuple to set :

Method #3: Using Built-in Functions (Static Input)

Approach:

  • Give the set as static input and initialize it with some random values.
  • Store it in a variable.
  • Print the above-given set.
  • Give the tuple as static input and initialize it with some random values.
  • Add the above-given tuple to the given set by using the add() function.
  • Print the above-given set after adding a given tuple.
  • The Exit of Program.

Below is the implementation:

# Give the set as static input and initialize it with some random values.
# Store it in a variable.
gven_set = {11, 12, 13, 14, 15}
# Print the above-given set.
print("The given set is :")
print(gven_set)
# Give the tuple as static input and initialize it with some random values.
gvn_tupl = (16, 17, 18, 19)
# Add the above given tuple to the given set by using the add() function.
gven_set.add(gvn_tupl)
print("The given set after after adding a given tuple:")
# Print the above-given set after adding a given tuple.
print(gven_set)

Output:

The given set is :
{11, 12, 13, 14, 15}
The given set after after adding a given tuple:
{11, 12, 13, 14, 15, (16, 17, 18, 19)}