Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c

Set datatype in Python programming language

Posted on October 11, 2017September 29, 2019

Table of Contents

  • Set datatype in Python programming language
      • How to add Elements to set
      • How to updating set elements
    • Set Operations
      • set union
      • Set Intersection
      • Set Difference
      • Set Symmetric Difference

Set datatype in Python programming language

In this tutorial, we will discuss the concept of Set datatype in Python programming language.

Set is a data type in Python which is an unordered collection of unique elements. Set has no duplicates and we can add or remove from the set.

How to make a set in python?

Set is a Datatype in Python. When creating a set, we can place element inside the curly braces {}, and elements separated by a comma, or we can use the pre-defined function set(). 

Set can have different data types (float, integer, string, tuple) and any number of items.

Set datatype
Set datatype



Example of set

when the above code is executed, it produces the following results
 

set([24, 36, 12, 48])



Set can not have duplicate elements

 

When the above code is executed, it produces the following results
 

set([32, 1, 43, 21, 54, 56])

Only display single elements

 

How to add Elements to set

We can update a single element using add() method
When the above code is executed, it produces the following results
 

set([8, 2, 4, 6])
set([8, 1, 2, 4, 6])

How to updating set elements

We can update multiple-element using update() method, We can get tuples, list, strings or other sets as its argument.
 
When the above code is executed, it produces the following results

set([8, 2, 4, 6])
set([1, 2, 3, 4, 5, 6, 7, 8])

How to remove or discard element from set

use remove() function

A particular item can be removed from the set using  remove()   method

When the above code is executed, it produces the following results
 
set([15, 41, 11, 21, 13])
set([15, 11, 21, 13])


use discard() function


A particular item can be removed from the set using discard() method

When the above code is executed, it produces the following results

set([15, 41, 11, 21, 13])

set([15, 41, 11, 21])


How to clear the element from the set

All item can be cleared from the set using clear() method


when the above code is executed, it produces the following results

(‘alphabets :’, set([‘a’, ‘c’, ‘b’, ‘e’, ‘d’, ‘f’]))

 

(‘alphabets :’, set([]))

 

How to pop element from the set

We can remove an item using the pop() method


When the above code is executed, it produces the following results

set([‘e’, ‘d’, ‘H’, ‘l’, ‘o’, ‘r’, ‘w’])
e

d


Set Operations

set union

Set datatype
Set datatype

Set union of X and Y is a set of all elements from both sets.
The union is performed using  | operator and same can be fulfilled by the union() pre-defined method in python.

Example 1
when the above code is executed, it produces the following results
 
(‘union of x and y is’, set([1, 2, 3, 4, 5, 6, 7, 8]))
 
Find union using union() function
when the above code is executed, it produces the following results
 
set([1, 2, 3, 4, 5, 6, 7, 8])
set([1, 2, 3, 4, 5, 6, 7, 8])

Set Intersection

Set intersection is performed using & operator and same can be fulfilled by the intersection() pre-defined method in python.
The set intersection of X and Y is a set of the element that is common to both.
Set datatype
Set datatype
 
Program 1
When the above code is executed, it produces the following results.
 
set([4, 7])
set([4, 7])
program 2
Find the intersection using intersection() build-in function

 

When the above code is executed, it produces the following results
 
set([4, 7])
set([4, 7])

Set Difference

The difference is performed using – operator and same can be fulfilled by difference() in pre-defined method in python.
The difference between X and Y (X-Y) is a set of the element in X but without in Y
Set datatype
Set datatype
Program 1
When the above code is executed, it produces the following results
set([8, 2, 6])
set([1, 3, 5])
Find the difference using difference() build-in function
 
Program 2
When the above code is executed, it produces the following results
set([8, 2, 6])
set([1, 3, 5])

Set Symmetric Difference

Symantec difference is performed using ^ operator and same can be fulfilled by Symentic_difference() in pre-defined method in python.
Symantec difference of X and Y is a set of the element in both X and Y without those that are common to both.
Set datatype
Set datatype
Program 1
When the above code is executed, it produces the following results.
 
set([1, 2, 3, 5, 6, 8])
Program 2
 
Find the symmetric_difference use symmetric_difference() build in function
Set datatype
Set data type example
 
When the above code is executed, it produces the following results.
 
set([1, 2, 3, 5, 6, 8])
set([1, 2, 3, 5, 6, 8])
 

Related post

Data type in Python                                     List in Python       

Tuple in Python                                            Dictionary in Python

 

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes