Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
10 views

Class 15 Python

Uploaded by

shivangiupa123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Class 15 Python

Uploaded by

shivangiupa123
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

PYTHON SETS

Python by Computer G
SETS A set is a collection which is
unordered and unindexed. In Python
sets are written with curly brackets.
Example Create a Set:
thisset = {"apple", "banana", "cherry"}
print(thisset) # returns {'cherry', 'banana', 'apple'}
Note: Sets are unordered, so you cannot be sure in which order
the items will appear.
ACCESS ITEMS
You cannot access items in a set by referring to an
index, since sets are unordered the items has no
index.
CHECK IF ITEM EXISTS
To determine if a specified item is present in a set use the in
keyword:
Example Check if "banana" is present in the set:
thisset = {"apple", "banana", "cherry"}
print("banana" in thisset)
CHANGE ITEMS
Once a set is created, you cannot change its items,
but you can add new items.
ADD ITEMS
To add one item to a set use the add() method.
To add more than one item to a set use the update()
method. Example Add an item to a set, using the add()
method: thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset) # returns {'apple', 'cherry', 'orange', 'banana'}
ADD ITEMS
Example Add multiple items to a set, using the update()
method:
thisset = {"apple", "banana", "cherry"}
thisset.update(["orange", "mango", "grapes"])
print(thisset) # returns {'apple', 'grapes', 'orange', 'banana',
'mango', 'cherry'}
GET THE LENGTH OF A SET
To determine how many items a set has, use the len()
method.
Example Get the number of items in a set:
thisset = {"apple", "banana", "cherry"}
print(len(thisset)) # returns 3
REMOVE ITEM
To remove an item in a set, use the remove(), or the
discard() method.
Example Remove "banana" by using the remove()
method:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset) # returns {'cherry', 'apple'}
REMOVE ITEM
Example Remove "banana" by using the discard()
method:
thisset = {"apple", "banana", "cherry"}
thisset.discard("banana")
print(thisset) # returns {'cherry', 'apple'}
REMOVE ITEM
You can also use the pop(), method to remove an item,
but this method will remove the last item. Remember
that sets are unordered, so you will not know what item
that gets removed.
The return value of the pop() method is the removed
item.
REMOVE ITEM
Example Remove the last item by using the pop() method:
thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x) # returns apple
print(thisset) # returns {'cherry', 'banana'}
Note: Sets are unordered, so when using the pop() method,
you will not know which item that gets removed.
REMOVE ITEM
Example The clear() method empties the set:
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset) # returns set()
Example The del keyword will delete the set completely:
thisset = {"apple", "banana", "cherry"}
del thisset
Thanks For Watching

You might also like