Introduction

Setup

Operators

variable

String

Number

Dtype & Comment

Lists

Tuples

Sets

Dictionaries

if else statement

For Loop

While Loop

iterator & iterable

Pre-Built functions

Function

Decorators

generators

Types of errors

Exceptions Handling

OOP

Regular expression

Create module

OS module

Date module

Random module

Math

Statistics module

Exercise 1

Exercise 2

Exercise 3

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Python sets

What is set?

Set is used to store multiple collections of value in one variable. We can store too many values in a set and perform a lot of work on it.
The set is both unordered and unindexed and does not allow duplicate values. Set items can appear in a different order every time we use them, and cannot be referred by index number. We can store string, number or
both at the same time in a set. we can use a capital letter and a small letter in a set.

How to create set?

We write sets inside the second bracket{}. The string must be written inside a single or double quotation ('', "") and numbers don't need any single or double quotes. Give a comma between two values.

Example:
Input
x={'A','b','C'}
y={"1","2","3"}
z={"A","2","c"}
s={1,2,3}
v={"1A","2b",3}
u={1,"2b",3,"C"}
print(x)
print(y)
print(z)
print(s)
print(n)
print(u)
Output
{'A','b','C'}
{"1","2","3"}
{"A","2","c"}
{1,2,3}
{"1A","2b",3}
{1,"2b",3,"C"}

Function of set

in

To check whether the value is in the set or not, "in" is used.

Example:
Input
x={"a","b","c","d"}
print("a" in x)
Output
True

remove() function

To remove any value from the set, remove() function is used.

Example:
Input
x={"a","b","c","d"}
x.remove("c")
Output
{"a","b","d"}

add() function

To add value to the set add() function is used.

Example:
Input
x={"a","b","c","d"}
x.add("e")
print(x)
Output
{"a","b","c","d","e"}

update() function

This function is used to add one set element to another set.

Example:
Input
x={"a","b","c","d"}
y={1,2,3,4}
x.update(y)
print(x)
Output
{1, 2, 3, 4, 'd', 'b', 'a', 'c'}

pop() function

To remove a value from the end of a set, the pop() function is used. If we use the pop function one time, then it will remove one value from the end. Every time we do that, the value of the total will decrease from the end.

Example:
Input
x={1,2,3,4}
x.pop()
print(x)
Output
{1,2,3}

clear() function

To remove all the elements of a set at a time, the clear() function is used.

Example:
Input
x={1,2,3,4}
x.clear()
print(x)
Output
set()

union() function

Union function returns a new set of all items from both sets.

Example:
Input
x={1,2,3,4}
y={5,6,7,8} z=x.union(y)
print(z)
Output
{5,6,7,8,1,2,3,4}

intersection_update() function

intersection_update() function keeps all the common items present in both sets. This function will not create a new set. It will just update the set.

Example:
Input
x={1,2,3,4}
y={1,6,3,8}
x.intersection_update(y)
print(x)
Output
{1, 3}

intersection() function

intersection() function keeps all the common items present in both sets. This function will create a new set.

Example:
Input
x={1,2,3,4}
y={1,6,3,8}
z=x.intersection(y)
print(z)
Output
{1, 3}

symmetric_difference_update() function

symmetric_difference_update() function keeps all the items that are not present in both sets. This function will not create a new set. It will just update the set.

Example:
Input
x={1,2,3,4}
y={1,6,3,8}
x.symmetric_difference_update(y)
print(x)
Output
{2, 4, 6, 8}

symmetric_difference() function

symmetric_difference function keeps all the items that are not present in both sets. This function will create a new set.

Example:
Input
x={1,2,3,4}
y={1,6,3,8}
z=x.symmetric_difference_update(y)
print(z)
Output
{2, 4, 6, 8}

difference() function

difference() function shows the difference between multiple sets. This function returns those values which only contain the first set.

Example:
Input
x={1,2,3,4}
y={1,6,3,8}
z=x.symmetric_difference_update(y)
print(z)
Output
{2, 4,}

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.