×

Python Set add() Method

Python Set add() Method

The set.add() method adds the specified element to a set. If the element is already present in the set, it doesn't add it.

Syntax

set.add(element)

Parameter

element- This parameter represents the element that is to be added to the set.

Return

None

Example 1

# Python program explaining
# the set.add() method 
# initializing the set
fruits = {"watermelon", "banana", "apple"}
print("Actual set: ",fruits) 
# adding the more fruits in the set
fruits.add("orange") 
fruits.add("grapes")
# After adding the more fruits in the set 
print("New set: ",fruits)

Output

Actual set:  {'apple', 'watermelon', 'banana'}
New set:  {'apple', 'watermelon', 'orange', 'banana', 'grapes'}

Example 2

# Python program explaining
# the set.add() method 
# set of week
week = {'Monday', 'Tuesday', 'Wednesday','Thursday'} 
# printing the set
print('Week values:', week)
# passing tuple values
tupleVal = ('Friday', 'Saturday','Sunday') 
# adding tuple
week.add(tupleVal)
print("After adding tuple values...")
print('Week values are:', week) 
# adding same tuple values again
#the tuple elements are already present in the set, it doesn't add.
print("Again adding the tuple values...")
week.add(tupleVal)
print('Week values are:', week) 

Output

Week values: {'Monday', 'Thursday', 'Wednesday', 'Tuesday'}
After adding tuple values...
Week values are: {('Friday', 'Saturday', 'Sunday'), 'Monday', 'Thursday', 'Wednesday', 'Tuesday'}
Again adding the tuple values...
Week values are: {('Friday', 'Saturday', 'Sunday'), 'Monday', 'Thursday', 'Wednesday', 'Tuesday'} 

Related Topics

Decision Tree Classification in Python

In this article, we will learn the implementation of the decision tree in Sklearn, which is nothing but the Scikit Learn library of python. First, we should learn what classification...

12 minutes read.

Python Packages

A package is a collection of Python modules that share a similar namespace and are generated by putting all of the modules in a single directory with certain special files...

6 minutes read.

Python Set union() method

Python Set union() method The set.union() method in Python returns a set that contains all items from the original set and all items from the specified sets. Syntax set.union(set1, set2...) Parameter set1- This parameter represents the first set...

2 minutes read.

Python ascii() Function

Python ascii() Function The ascii() function returns a readable version of any object (Strings, Tuples, Lists, etc). This function will replace any non-ascii characters with escape characters. Syntax ascii(object) Parameter object:  An object, like String, List, Tuple, Dictionary, etc. Return This...

1 minute read.

Python String rjust() method

Python String rjust() method The string.rjust() method in Python returns a right-justified string of a given minimum width where the padding is done using the specified fillchar (default is a space). It returns...

2 minutes read.

Python Dictionary

Dictionary in Python is an unordered collection of data values, which is used to store data values like a map. Unlike different data types that hold just individual assets as...

4 minutes read.

Python Set Methods

[et_pb_section][et_pb_row][et_pb_column type="4_4"][et_pb_text] Python Set Methods A set is a collection which is unordered and unindexed. Python has a set of built-in methods that you can use on sets. Methods ...

4 minutes read.

Writing to a CSV file in Python

Python is an Object-Oriented high-level language. Python has an English-like syntax, which is very easy to read and write codes. Python is an interpreted language which means that it uses...

6 minutes read.

Python Support

Python Python is an interactive and more accessible language than any other programming language. The python programming language uses a variety of libraries to perform the operations in a faster way....

3 minutes read.

Ternary operators in python

Starting from Python version 2.5, ternary operators are also known as Conditional operators. Using these operators, we can evaluate any problem based on a condition. It is an alternative and...

4 minutes read.

Python Boolean

In this article, you will learn the boolean variables in python, bool() function in python, and bool operators with examples, Boolean Objects in Python. There are the only two possible values...

6 minutes read.

Copying a file from one folder to Another with Python

In this article, we shall discuss copying a file from one folder to another using python functions. A file is a collection of data or information stored on a computer....

3 minutes read.

Python String lower() method

Python String lower() method The string.lower() method in Python returns a string where all characters are lower case. Syntax string.lower() Parameter NA Return This method returns a string where all characters are lower case. Example 1 # Python...

1 minute read.

Augmented reality in python

In this article, we shall learn about augmented reality in python. Augmented reality is a technology that captures the world around you and adds virtual content or objects on top of...

3 minutes read.

Python Knapsack problem

Python Knapsack problem Before we dig down about Knapsack problems in Python, first let's have a look at what is actually a knapsack problem. What is a knapsack problem? A problem from the...

5 minutes read.

Python Errors and exceptions

Exceptions and errors are the obstacles a programmer constantly faces while writing a program. Firstly, we need to understand what are errors and exceptions and the difference between these two...

6 minutes read.

Python Dictionary Methods

Python Dictionary Methods Python has a set of built-in methods that dictionary objects can call. All the python dictionary methods are as follow: Methods Description clear() The dictionary.clear() method removes all the elements...

3 minutes read.

Python Stack

Python Stack: The work Stack is defined as arranging a pile of objects or items on top of another. It is the same method of allocating memory in the stack...

10 minutes read.

Python Deep Copy and Shallow Copy

Assignment statements in Python create bindings between a target and an object, not copies of them. The = operator only makes a new variable that shares the reference to the...

3 minutes read.

Python | a += b is not always a = a + b

a += b in Python doesn't always behave the same way as a = a + b; the same operands can produce different outcomes depending on the circumstances. But we...

3 minutes read.