0% found this document useful (0 votes)
14 views3 pages

Example: It Is Also Possible To Use The Constructor To Make A Set

A set in Python is an unordered, unchangeable, and unindexed collection that can store multiple items of different data types without duplicates. Sets can be created using curly brackets or the set() constructor, and items can be added or removed using methods like add(), remove(), discard(), pop(), and clear(). The document provides examples of creating sets, adding items, and removing items, demonstrating the flexibility of this data type.

Uploaded by

aizenkurosaki233
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)
14 views3 pages

Example: It Is Also Possible To Use The Constructor To Make A Set

A set in Python is an unordered, unchangeable, and unindexed collection that can store multiple items of different data types without duplicates. Sets can be created using curly brackets or the set() constructor, and items can be added or removed using methods like add(), remove(), discard(), pop(), and clear(). The document provides examples of creating sets, adding items, and removing items, demonstrating the flexibility of this data type.

Uploaded by

aizenkurosaki233
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
You are on page 1/ 3

Set

Set is one of 4 built-in data types in Python used to store multiple items in a single
variable.

 A set is a collection which is unordered, unchangeable*, and unindexed.


 Set items are unchangeable, but you can remove items and add new items.
 Sets are written with curly brackets.
 A set can contain different data types:
 Set items are unordered, unchangeable, and do not allow duplicate
values.

Example
Create a Set:

thisset = {"cbse", "grade8", "oakridge"}


print(thisset)
print(type(myset))

Output: {"cbse", "grade8", "oakridge"}

<class 'set'>

 It is also possible to use the set() constructor to make a set.


thisset = set((“cbse", "grade8", "oakridge"))
print(thisset)

Output: {"cbse", "grade8", "oakridge"}

Add an item to a set

Add an item to a set, using the add() method:

thisset = {"cbse", "grade8", "oakridge"}

thisset.add("bachupally")

print(thisset)

Output: {"cbse", "grade8", "oakridge",”bachupally”}


Add elements from anotherset into thisset:

thisset = {"cbse", "grade8", "oakridge"}


myset = {"ICT", "maths", "english"}

thisset.update(myset)

print(thisset)

Output: {"cbse", "grade8", "oakridge","ICT", "maths", "english"}

Python - Remove Set Items

1. Using the remove() method:


thisset = {"cbse", "grade8", "oakridge"}

thisset.remove("grade8")

print(thisset)

Output: {"cbse", "oakridge"}

2. Using the discard() method:

thisset = {"cbse", "grade8", "oakridge"}

thisset.discard("cbse")

print(thisset)

Output: {"grade8", "oakridge"}

3. Using the pop() method:

thisset = {"cbse", "grade8", "oakridge"}

thisset.pop()

print(thisset)

Output: {“cbse”,"grade8"}
4. The clear() method empties the set:

thisset = {"cbse", "grade8", "oakridge"}

thisset.clear()

print(thisset)

Output: {}

5. The del keyword will delete the set completely:

thisset = {"cbse", "grade8", "oakridge"}

del thisset

print(thisset)

Output: Error : this list not defined.

You might also like