×

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

Best Database in Python

In this tutorial, Firstly, we will understand what the term database means. Further, we will see the various databases supported in Python and when to use which one. Further, we...

7 minutes read.

How to create a DataFrames in Python

How to create a DataFrames in Python Data Frame is a data structure in which data is stored in tabular form. They can also be referred as two-dimensional collection of data. Various...

5 minutes read.

Python Sys Stdout

Python Programming Language: Python programming language is one of the most used programming languages, as it is used widely in the field of software and data analysis, web development, etc. It...

3 minutes read.

Python String isdigit() method

Python String isdigit() method The string.isdigit() method returns a boolean value true if all characters in the string are digits else for any other value it returns false. Syntax string.isdigit() Parameter NA Return This method returns a...

2 minutes read.

Python CGI Programming

The Concept of CGI CGI is an abbreviation for Common Gateway Interface. It is not a type of language but a set of rules (specification) that establishes a dynamic interaction between...

9 minutes read.

Python Read CSV file

The CSV stands for “comma-separated value,” which is defined as a simple file format that is used to store data into a tabular form such as a database or spreadsheet. It is...

7 minutes read.

Python Data Visualization

In this tutorial, we will understand what data visualization means in python. Further, we will see different methods of visualizing data in python. Data visualization In a non-technical language, it is a...

4 minutes read.

Python Syntax

Python is a strong object-oriented programming language that is simple to learn. Python was created to be a very readable programming language. The syntax of the Python programming language is...

8 minutes read.

How to Take Input in Python

How To Take Input In Python Python has various in-built functions that help in code redundancy. Let's discuss the usage of some functions- ascii() – it returns a readable representation of objects.format()...

4 minutes read.

Abstraction in Python

What is meant by abstraction generally? A very general notion of a thing or work is known as abstract. To be more precise, the process of having a brief idea but...

4 minutes read.

Function annotations() in Python

What is Function Annotations? Function annotations provide a way related to different parts of a function at compile time with arbitrary Python expressions. It doesn’t have life in Python runtime execution....

3 minutes read.

Python Project Ideas for Beginners

Python project ideas for beginners Advanced Python is an amazing platform to grow and achieve success as a developer in the future. Python is a programming language that can be very...

7 minutes read.

How to create a dictionary in Python?

How to create a dictionary in python Dictionary is a data structure in Python that represents our data in the form of keys and values. Each value in a dictionary can be...

5 minutes read.

How to write a program in python?

How to write a program in python? In python, writing programs is not a big deal. What we need to work on is our logic and some basic rules of this...

4 minutes read.

Writing to Excel using 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...

3 minutes read.

Python String capitalize() method

Python String capitalize() method The string.capitalize() method in Python returns a copy of the string with only its first character capitalized. Syntax string.capitalize() Parameter NA Return This function returns a string where the first character is upper...

1 minute read.

How to Plot Graphs Using Python?

In this article, we are going to learn about how to plot different types of graphs using Python. We are going to use different approaches for every type of graph....

3 minutes read.

Sort a dataframe based on a column in Python

Sorting the dataframe based on a column requires pandas which is An open-source library called Python Pandas is described as offering high-performance data processing in Python. For both professionals and...

4 minutes read.

Find Words in String Python

Python : Python programming language is considered a general purpose; it is a high-level programming language that is not much difficult but easier to learn. Python programming language is rich in...

5 minutes read.

Python hex() function

Python hex() function The hex() function in Python  converts the specified number into a hexadecimal value. Syntax hex(x) Parameter x: The parameter represents an Integer value. Return This function Returns hexadecimal string. Example 1 # Python Program explaining # the hex() function print("The...

1 minute read.