×

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

How To Compare Two Strings In Python

In this article, we will discuss how to compare two strings in Python. So, before that, let’s have a quick revision on “What are strings?” Strings are a sequence of characters that...

5 minutes read.

Python TypeError

What is TypeError in python? TypeError is one of the exceptions in the python programming language. This exception occurs when an operation is performed on an unsupported object type or can...

6 minutes read.

Managing Multiple Python Versions With pyenv

Introduction If you had ever wished to assist to an application that makes use of several different Python versions but weren't sure whether you would quickly test them all? Do you...

4 minutes read.

Creating Web Application in python

Web Application :  Web Application is an application software that runs in web browser . Software programs will run on operating system. Whereas Web Applications will run on World Wide Web...

5 minutes read.

Python Project Ideas

One of the most widely used programming languages today is Python. This pattern appears set to continue through 2023 and beyond. Therefore, working on some current Python project ideas is the...

10 minutes read.

Python Key Error

What is an Error? Errors are nothing but problems in the program which occur in a program code, and this will stop the execution of the program. It is also called...

6 minutes read.

Python String Variable

Python programming language: 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...

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 pynmea2

Define Pynmea2 Python Programming Language: 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...

4 minutes read.

Android apps for coding in python

Nowadays, it is the generation of smartphones. The world can be seen and acknowledged with a single click on your mobile phone. Everyone uses mobile phones to do any work,...

9 minutes read.

Python MySQL Client

MySQL client is a project that is the subdivision of the MySQLdb package. This package provides an interface that runs the Python database API. Installing mysqlclient You can easily install mysqlclient with...

5 minutes read.

Composition in Python

Composition in Python Composition is one of the important concepts of Object-oriented programming (OOPs). Composition basically enables us for creating complex types objects by combining other types of objects in the...

6 minutes read.

Python Command line Arguments

Python Command line Arguments The command-line argument is used to the change functionality of the program. It's an extra command the programmer can use while launching a program. These commands have many uses...

7 minutes read.

Python List Methods

Python List Methods Python has a set of built-in methods that you can use on lists or arrays. Following are all of the methods of list objects: Methods Explanation append The list.append() method...

3 minutes read.

Python Read Excel file

Python Read Excel file Excel is the spreadsheet application for Window, which is developed by Microsoft. The Excel stores data in the tabular form. It provides easy access to analyze and maintain the...

3 minutes read.

Python PPTX

Python-pptx is a python library that enables the user to create and update PowerPoint (.pptx) presentations. It is used to create modified PowerPoint presentations from the db content that can...

10 minutes read.

How to Compare two Lists in Python?

How to Compare two Lists in Python The list is a data structure in Python that can hold values of different data types. The values are enclosed in square brackets [...

4 minutes read.

Python exit commands

exit(), quit(), sys.exit(), os._exit() In this tutorial, we will study exit commands used in the Python programming language. Python is undoubtedly the choice of programmer and this is because of the in-built...

3 minutes read.

How to run a Program in Python

How to run a Program in Python Writing a program in Python is an easy task, beginners who are ready to kickstart their career in the world of programming can create...

3 minutes read.

Python logging Module

Python logging Module Introduction By logging word we understand the tracking of the events which happens when we run some software. Logging process is very important part for developing software, debugging...

12 minutes read.