Data type in Python

Set datatype in Python programming language

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



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 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
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
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
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 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

 

 

Karmehavannan

Share
Published by
Karmehavannan

Recent Posts

Subtract two numbers using method overriding

Subtract two numbers using method overriding   Program 1

3 months ago

PHP Star triangle Pattern program

PHP Star triangle Pattern program Here's a simple Java program that demonstrates how to print…

3 months ago

Using function or method to Write temperature conversion : Fahrenheit into Celsius

Using Function or Method to Write to temperature conversion: Fahrenheit into Celsius In this article,…

1 year ago

Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user

Function or method of temperature conversion from Fahrenheit into Celsius In this article, we will…

1 year ago

Write temperature conversion program: Fahrenheit into Celsius

Write temperature conversion program: from Fahrenheit to Celsius In this article, we will discuss the…

1 year ago

How to write a program to convert Fahrenheit into Celsius

How to write a program to convert Fahrenheit into Celsius In this article, we will…

1 year ago

This website uses cookies.