Python Set Add() - Programiz

Tutorials Courses Python JavaScript TypeScript SQL HTML CSS C C++ Java R Ruby RUST Golang Kotlin Swift C# DSA

Become a certified Python programmer.

ENROLL

Popular Tutorials

Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning Python

Popular Examples

Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year Explore Python Examples

Reference Materials

Built-in Functions List Methods Dictionary Methods String Methods View all Programiz Pro Logo

Created with over a decade of experience.

  • Learn
  • Practice
  • Compete
Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA Learn C Learn C++ Learn Java View all Courses on Programiz Pro Logo Python Basics Python Intermediate C++ Basics C++ Intermediate C++ OOP C Programming Java Basics Java Intermediate Java OOP View all Courses on Programiz Pro Logo Python Challenges JavaScript Challenges Java Challenges C++ Challenges C Challenges View all Challenges on Programiz Pro Logo Learn Practice Compete

Certification Courses

Created with over a decade of experience and thousands of feedback.

Learn Python Learn HTML Learn JavaScript Learn SQL Learn DSA View all Courses on Programiz Pro Logo Learn C Learn C++ Learn Java Python JavaScript TypeScript SQL HTML CSS C C++ Java More languages

Become a certified Python programmer.

Try Programiz PRO!

Popular Tutorials

Getting Started With Python Python if Statement while Loop in Python Python Lists Dictionaries in Python Start Learning Python All Python Tutorials

Reference Materials

Built-in Functions List Methods Dictionary Methods String Methods View all Python JavaScript C C++ Java R Kotlin

Become a certified Python programmer.

Try Programiz PRO!

Popular Examples

Add two numbers Check prime number Find the factorial of a number Print the Fibonacci sequence Check leap year All Python Examples

Python Set Methods

  • Python Set remove()
  • Python Set add()
  • Python Set copy()
  • Python Set clear()
  • Python Set difference()
  • Python Set difference_update()
  • Python Set discard()
  • Python Set intersection()
  • Python Set intersection_update()
  • Python Set isdisjoint()
  • Python Set issubset()
  • Python Set pop()
  • Python Set symmetric_difference()
  • Python Set symmetric_difference_update()
  • Python Set union()
  • Python Set update()

Python Tutorials

  • Python Tuple index()
  • Python Set clear()
  • Python Tuple count()
  • Python List index()
  • Python List count()
  • Python Dictionary fromkeys()
Python Set add()

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

Example

prime_numbers = {2, 3, 5, 7} # add 11 to prime_numbers prime_numbers.add(11) print(prime_numbers) # Output: {2, 3, 5, 7, 11}

Syntax of Set add()

The syntax of add() method is:

set.add(elem)

add() method doesn't add an element to the set if it's already present in it.

Also, you don't get back a set if you use add() method when creating a set object.

noneValue = set().add(elem)

The above statement doesn't return a reference to the set but 'None', because the statement returns the return type of add which is None.

Set add() Parameters

add() method takes a single parameter:

  • elem - the element that is added to the set

Return Value from Set add()

add() method doesn't return any value and returns None.

Example 1: Add an element to a set

# set of vowels vowels = {'a', 'e', 'i', 'u'} # adding 'o' vowels.add('o') print('Vowels are:', vowels) # adding 'a' again vowels.add('a') print('Vowels are:', vowels)

Output

Vowels are: {'a', 'i', 'o', 'u', 'e'} Vowels are: {'a', 'i', 'o', 'u', 'e'}

Note: Order of the vowels can be different.

Example 2: Add tuple to a set

# set of vowels vowels = {'a', 'e', 'u'} # a tuple ('i', 'o') tup = ('i', 'o') # adding tuple vowels.add(tup) print('Vowels are:', vowels) # adding same tuple again vowels.add(tup) print('Vowels are:', vowels)

Output

Vowels are: {('i', 'o'), 'e', 'u', 'a'} Vowels are: {('i', 'o'), 'e', 'u', 'a'}

You can also add tuples to a set. And like normal elements, you can add the same tuple only once.

Also Read:

  • Python Set update()
  • Python Set remove()
Previous Tutorial: Python Set remove() Next Tutorial: Python Set copy() Share on: Did you find this article helpful?

Sorry about that.

How can we improve it? Feedback * Leave this field blank

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community

Python References

Python Library

Python Set clear()

Python Library

Python frozenset()

Python Library

Python Set update()

Python Library

Python Set remove()

Tag » How To Add To A Set Python