Show Menu
Cheatography

Python Sets Cheat Sheet (DRAFT) by

A set is a collection which is unordred and unindexed in a way that we cannot be sure in which order the items will appear.

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Set

A set is a collection which is unordred and unindexed in a way that
we cannot be sure in which order the items will appear.

Set example

RYB_color = {"Red" ,"Yellow", " Blue"}
print( RYB _color)
>>> {'Red' ,'Yellow' ,'Blue'}

Access Items

for x in RYB_color:
print(x)
>>> Red
>>> Yellow
>>> Blue

Change Items

Since a set is not ordererd neither indexed, then we cannot acces to
any item to change its value.

Add Items

RYB_color.add( " White")
print( RYB _color)
>>> " Red " ,"White " ,"Yellow", "Blue "

Get the Length of a Set

print (le n(R YB_ color))
>>> 3

Delete a set

del RYB_color

Remove Item

RYB_color.remove( " Yellow ")
print( RYB _color)
>>> {'Red' ,'Blue'}

Join Two Sets

Second_color = {"Green " ,"Orange", "Purple"}
Color = RYB_color.union (Second_color)
print( Color)
>>> {'Yellow' ,'Orange' ,'Red' ,'Green', 'Blue' ,'Purple'}