0% found this document useful (0 votes)
2 views4 pages

Set in Python7

A Set in Python is a built-in data type that stores unique values in an unordered and mutable collection. It is useful for removing duplicates and performing mathematical set operations. Key methods include adding, removing, and updating elements, and sets are commonly used in data cleaning and validation tasks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views4 pages

Set in Python7

A Set in Python is a built-in data type that stores unique values in an unordered and mutable collection. It is useful for removing duplicates and performing mathematical set operations. Key methods include adding, removing, and updating elements, and sets are commonly used in data cleaning and validation tasks.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Rupaalife 👈

Set in Python
1. What is a Set?

A Set in Python is a built-in data type used to store multiple values in a single variable, but
without duplicates.

A set is:

Unordered

Mutable

Contains only unique elements

2. Why Do We Need a Set?

Sets are used when:

Duplicate values must be removed

Only unique data is required

Order of elements is not important

Real-time examples:

Unique email IDs

Unique phone numbers

Unique student roll numbers

3. How to Create a Set

A set is created using curly braces { }.

Syntax:
set_name = {value1, value2, value3}

Example:
numbers = {10, 20, 30, 40}

4. Real-Time Example (Easy Understanding)


Student Roll Numbers
In a class, roll numbers must be unique.

Python representation:

roll_numbers = {101, 102, 103, 104}

If duplicates are added, Python automatically removes them.

5. Set Does Not Allow Duplicate Values

Example:

data = {1, 2, 2, 3, 4, 4}
print(data)

Output:

{1, 2, 3, 4}

6. Set is Unordered

Set elements do not have a fixed position.

Example:

colors = {"Red", "Blue", "Green"}


print(colors)

Output order may change every time.

Because of this:

Indexing is not allowed

Slicing is not possible

7. Modifying a Set

Sets are mutable, but individual elements cannot be changed directly.

Add elements
fruits = {"Apple", "Banana"}
[Link]("Mango")
Add multiple elements
[Link](["Orange", "Grapes"])

8. Removing Elements from a Set


Remove element
[Link]("Banana")

Remove without error


[Link]("Pineapple")

Remove random element


[Link]()

9. Set with Different Data Types

Example:

data = {"Ravi", 25, 75.5, True}

10. Important Set Methods


Method​ Description
add()​ Add one element
update()​ Add multiple elements
remove()​ Remove element
discard()​ Remove without error
pop()​ Remove random element
clear()​ Remove all elements
11. Set Operations
Union
a = {1, 2, 3}
b = {3, 4, 5}
print(a | b)

Intersection
print(a & b)

Difference
print(a - b)

12. Difference Between List, Tuple, Set, Dictionary


Feature​ List​ Tuple​ Set​ Dictionary
Ordered​ Yes​ Yes​ No​ Yes
Mutable​ Yes​ No​ Yes​ Yes
Duplicates​ Allowed​ Allowed​ Not Allowed​ Keys Not Allowed
Access​Index​ Index​ No Index​ Key
13. One-Line Definition (Exam Oriented)
A set is a built-in data type in Python used to store multiple unique values in an unordered
collection.

14. When to Use Set

Use set when:

You want only unique values

You want to remove duplicates

You want to perform mathematical set operations

15. Conclusion

Set is a powerful data structure used to manage unique data efficiently.


It is commonly used in data cleaning, validation, and comparison tasks.

You might also like