the classset
An array is an array that does setnot have a fixed size, can store values of different types simultaneously, and its values cannot be altered or deleted directly.
It also cannot contain duplicate values. That is, if you put the same value twice in it, one value will be stored in it, not two.
The type setdoes not preserve the order in which the elements are entered because it does not add a numberIndexFor each element as does type listand typetuple.
So don't be surprised if you store an array of values inside setand then try to display it. Because every time you run the program again, the values will change.
method of definitionset
To define setwe use the symbol .{ }.
Inside this code you can pass values directly to it provided you put a comma between every two elements.
In the following example, we have defined setand put integers in it.
How to define set integers
numbers = {10, 20, 30, 40, 50} # Contains integers only numbers whose name is set Here we have defined
print(numbers) # ( as we defined it ) as numbers Here we have displayed the content of the object
• We will get a result similar to the following when running because we don't know how the items will be arranged.
In the following example, we have defined setand placed text in it.
How to define set which texts
names = {'Rami', 'Sara', 'Nada'} # Contains text only names His name is set Here we have defined
print(names) # (that is, as we defined it) as it is names Here we have shown what the object contains
• We will get a result similar to the following when running because we don't know how the items will be arranged.
In the following example, we have defined setand put integers and texts in it.
Definition set of integers and texts
data = {1, 'Mhamad', 'Harmush', 1500} # contains integers and strings data name set here we have defined
print(data) # (that is, as we defined it) as data Here we have shown what the object contains
• We will get a result similar to the following when running because we don't know how the items will be arranged.
In the following example, we define setand place strings in it and iterate these strings on purpose to demonstrate that a type setdoes not store the same value more than once.
Fourth example
# Contains texts and note that we have duplicated some values whose names are set here we have defined
names = {'Rami', 'Rami', 'Rami', 'Nada', 'Nada', 'Ahmad'}
# (that is, as we defined it) as it is. Note that it does not contain duplicate names. Here we have shown what the object contains
print(names)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
deletethesetBy sentence delin Python
The sentence delis used to deletethesetAs it is from memory.
Explain how to delete the set by wholesale del
In the following example we have defined setand placed numbers in it. Then we deleted it from memory. Then we tried to show what it contains.
# We put integers in it arr named set Here we have defined
arr = {10, 20, 30, 40, 50}
del arr # As it is from memory arr Here we have deleted the object
print(arr) # which we originally deleted from memory so it will throw an error when we run arr here we tried to display what the object contains
• We will get the following result when running.
Check on the valuesthe">setBy worker inin Python
The operator inis used to search inthesetAbout a specific value or to pass on its value when used in the loop for.
Use the . operator in to search in the set about a certain value
In the following example, we used the . operatorin to search inthesetabout a certain value.
arr = {'Mhamad', 'Rony', 'Rima', 'Sara'} # We put a set of text values in it arr named set Here we have defined
x = 'Rima' # We put the text x here we have defined a variable named
print('Is Rima in the set?')
print(x in arr) # True if found then .arr will be shown in object x here the value will be searched for
• We will get the following result when running.
True
In the following example, we have defined setand placed text in it. Then we display all the values placed in it using the loop for.
second example
# We put text values in it that represent the names of people whose name is set. Here we have defined
names = {'Rami', 'Sara', 'Nada', 'Mhamad', 'Salem'}
# and then x will be printed in the variable names every time the value of an object is set
for x in names:
print(x)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
Salem
Mhamad
Rami
Sara
Class functions setin Python
| Function name and definition | |
|---|---|
add(elem)
Used to add a new item inthesetwho summoned her. |
|
discard(elem)
Used to delete a specific item fromthesetwho summoned her. Where the parameter is elemwe pass an object whose value matches the value of the element we want to delete. |
|
remove(elem)
Used to delete a specific item fromthesetwho summoned her. Where the parameter is elemwe pass an object whose value matches the value of the element we want to delete. Note: The difference between it and the function discard()is that it throws the KeyError exception if an element is not found inthesetIt has the same value as the object we passed in place of the parameterelem. |
|
clear()
Used to delete all itemsthesetwho summoned her. |
|
pop()
Used to return the value of an element that is randomly selected fromthesetThe one who called it, then it is deleted from it. |
|
copy()
Return a copy ofthesetwho summoned her. |
|
difference(*sets)
Returns seta new containing the elements inthesetThe one who summoned it, and it is not present in every setpass, has the place of the parametersets*. |
|
difference_update(*sets)
comparethesetThe one who summoned it with every setpass we pass it has the place of the parametersets*.Then delete fromthe setWho summoned the common elements between them. |
|
intersection(*sets)
Returns seta new containing the elements inthesetWho summoned it and in every setpass it has the place of the parametersets*. |
|
intersection_update(*sets)
comparethesetThe one who summoned it with every setpass we pass it has the place of the parametersets*.Then delete fromthe setWho summoned the uncommon elements among them. |
|
symmetric_difference(anotherSet)
Returns seta new one that contains the elements that are not present in each of thethesetwho summoned her and inthesetWhich we pass to her in place of the parameteranotherSet. |
|
symmetric_difference_update(anotherSet)
comparethesetwho summoned her withthesetWhich we pass to her in place of the parameter anotherSet. then put inthe setThe one who summoned it only the common elements between them and erases any other elements that were in it. |
|
union(sets*)
Returns seta new containing the elements inthesetWho summoned it and the items in each setpass it have a parameter placesets*. |
|
update(sets*)
compare itemsthesetWho summoned it with the items in eachset pass, has a parameter's placesets*.then put inthe setWho summoned the common and uncommon elements between them. |
|
isdisjoint(anotherSet)
compare itemsthesetWho summoned her with itemsthesetWhich we pass to her in place of the parameteranotherSet.Returns Trueif they do not contain elements of equal value. And return Falseif they are not. |
|
issuperset(anotherSet)
compare itemsthesetWho summoned her with itemsthesetWhich we pass to her in place of the parameteranotherSet.Returns Trueif all itemsthesetThe one we pass has a parameter 's place anotherSetinthesetwho summoned her. Otherwise, you will return False. |
|
issubset(anotherSet)
compare itemsthesetWho summoned her with itemsthesetWhich we pass to her in place of the parameteranotherSet.Returns Trueif all itemsthesetThe one who summoned it is inthesetWhich we pass to her in place of the parameteranotherSet.Otherwise, you will return False. |
|
Ready functions in Python to handlethe set in python
| Function name and definition | |
|---|---|
len(set)
Returns an integer representing the number of elementsthesetwhich we pass to her when she is summoned. |
|
min(set)
Returns the smallest value in .thesetwhich we pass to her when she is summoned. |
|
max(set)
Returns the largest value inthesetwhich we pass to her when she is summoned. |
|
set(sequence)
Returns a copy of any object containing a set of elements that we pass to it when called as a class objectThe set. |
|
class set - function add()
Its definition is used to add a new element inthesetwho summoned her.
Build function add()
set.add(elem)
Function parameters add()
Where the parameter is xwe pass the object we want to add inThe set.
function return value add()
It does not return a value.
Example of the function add()
aSet = {'Apple', 'Banana', 'Mango'} # We put 3 elements in it aSet named set Here we have defined
print('Before:', aSet) # aSet Here we have shown what the object contains
aSet.add('Orange') # aSet Here we have added text as a new element at the end of the object
print('After: ', aSet) # Again aSet Here we have shown what the object contains
• We will get a result similar to the following when running because we don't know how the items will be arranged.
After: {'Apple', 'Orange', 'Banana', 'Mango'}
class set - functiondiscard()
Its definition is used to delete a specified element fromthesetwho summoned her.
Build function discard()
set.discard()
Function parameters discard()
Where the parameter is elemwe pass an object whose value matches the value of the element we want to delete.
function return value discard()
It does not return a value.
first example
# We put in it a set of integers called aSet. Here we have defined
aSet = {1, 2, 3, 4, 5}
# aSet Here we have deleted the element that has the value 1 in the object
aSet.discard(1)
# aSet Here we have shown what the object contains
print(aSet)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
second example
# We put in it a set of integers called aSet. Here we have defined
aSet = {1, 2, 3, 4, 5}
# and since there is no element that has this value nothing will happen aSet to delete the element that has value 10 in object discard() here we called the function
aSet.discard(10)
# aSet Here we have shown what the object contains
print(aSet)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
class set - functionisdisjoint()
Function definition isdisjoint()
compare itemsthesetWho summoned her with itemsthesetWhich we pass to her in place of the parameteranotherSet.
Returns Trueif they do not contain elements of equal value. And return Falseif they are not.
Build function isdisjoint()
set.isdisjoint(anotherSet)
Function parameters isdisjoint()
The location of the parameter anotherSetwe passthesetwhose elements will be compared with those ofthesetwho summoned her.
function return value isdisjoint()
return Trueifthesetwho summoned her andthesetThe ones we passed to her do not contain at least one element of equal value.
Otherwise, you will returnFalse.
Functional example isdisjoint()
# We put integer numbers in them set Here we have defined three
set1 = {1, 2, 3}
set2 = {1, 7}
set3 = {4, 5, 6}
# Because they both have equal values False will return .set2 and set1 when comparing the elements of the two isdisjoint() objects Here we have shown what the function will return
print('set1 disjoint set2?', set1.isdisjoint(set2))
# Because they do not have equal values True will return .set3 and set1 when comparing the elements of the two isdisjoint() objects Here we have shown what the function will return
print('set1 disjoint set3?', set1.isdisjoint(set3))
• We will get the following result when running.
set1 disjoint set3? True
class set - functionissuperset()
Define the function issuperset() to compare elementsthesetWho summoned her with itemsthesetWhich we pass to her in place of the parameteranotherSet.
Returns Trueif all itemsthesetThe one we pass has a parameter 's place anotherSetinthesetwho summoned her.
Otherwise, you will returnFalse.
Build function issuperset()
set.issuperset(anotherSet)
Function parameters issuperset()
The location of the parameter anotherSetwe passthesetwhose elements will be compared with those ofthesetwho summoned her.
function return value issuperset()
Returns Trueif all itemsthesetThe one we pass has a parameter 's place anotherSetinthesetwho summoned her.
Otherwise, you will returnFalse.
The first example in the function issuperset()
# We put integer numbers in them set Here we have defined two
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3}
# set2 contains all the elements of set1 because True will return .set2 and set1 when comparing the elements of the two objects issuperset() here we have shown what the function will return
print(set1.issuperset(set2))
• We will get the following result when running.
The second example in the function issuperset()
# We put integer numbers in them set Here we have defined two
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
# set2 does not contain all set1 elements because False will return .set2 and set1 when comparing the elements of the two issuperset() objects here we have shown what the function will return
print(set1.issuperset(set2))
• We will get the following result when running.
class set - functionissubset()
Define the function issubset() to compare elementsthesetWho summoned her with itemsthesetWhich we pass to her in place of the parameteranotherSet.
Returns Trueif all itemsthesetThe one who summoned it is inthesetWhich we pass to her in place of the parameteranotherSet.
Otherwise, you will returnFalse.
Build function issubset()
set.issubset(anotherSet)
Function parameters issubset()
The location of the parameter anotherSetwe passthesetwhose elements will be compared with those ofthesetwho summoned her.
Function return value issubset()
Returns Trueif all itemsthesetThe one who summoned it is inthesetWhich we pass to her in place of the parameteranotherSet.
Otherwise, you will returnFalse.
The first example in the function issubset()
# We put integer numbers in them set Here we have defined two
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
# set1 contains all the elements of set2 because True will return .set2 and set1 when comparing the elements of the two objects issubset() here we have shown what the function will return
print(set1.issubset(set2))
• We will get the following result when running.
The second example in the function issubset()
# We put integer numbers in them set Here we have defined two
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3}
# set1 does not contain all set2 elements because False will return .set2 and set1 when comparing the elements of the two issubset() objects here we have shown what the function will return
print(set1.issubset(set2))
• We will get the following result when running.
class set - functionclear()
Definition of a function clear() used to delete all elementsthesetwho summoned her.
build it
set.clear()
Function parameters clear()
Do not accept any parameters.
Return value
It does not return a value.
Example in the function clear()
# We put 5 elements in it aSet named set Here we have defined
aSet = {10, 20, 30, 40, 50}
# aSet Here we have deleted all the elements in the object
aSet.clear()
# empty set which will appear as aSet Here we have shown what the object contains
print('aSet contains:', aSet)
• We will get the following result when running.
• The set() value that appears in the run result means that the object aSetis an setempty space that does not contain any value.
class set - functioncopy()
Its definition returns a copy ofthesetwho summoned her.
built
set.copy()
Function parameters copy()
Do not accept any parameters.
function return value copy()
Return a copy ofthesetwho summoned her.
Example of the function copy()
# We put 5 elements in it set1 named set here we have defined
set1 = {10, 20, 30, 40, 50}
# set2 in the set1 object here we copied the objects of the object
set2 = set1.copy()
# set2 and set1 Here we have shown what the object contains
print('set1 contains:', set1)
print('set2 contains:', set2)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
set2 contains: {50, 20, 40, 10, 30}
class set - functiondifference()
Its definition returns seta new containing the elements inthesetThe one who summoned it, and it is not present in every setpass, has the place of the parametersets*.
built
set.difference(*sets)
Function parameters difference()
Parameter location We sets*pass one or more objects of the type setwith the indication that they must be comma separated when more than one object is passed.
Return value
Returns seta new containing the elements inthesetThe one who summoned it, and it is not present in every setpass, has the place of the parametersets*.
first example
# We put integer numbers in them set Here we have defined two
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3}
# newSet and we put it in the object set2 and not in the object set1 Here we created a copy of the elements in the object
newSet = set1.difference(set2)
# newSet Here we have shown what the object contains
print('newSet contains:', newSet)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
second example
# We put integer numbers in them set Here we have defined three
set1 = {1, 2, 3, 4, 5, 6}
set2 = {1, 2}
set3 = {3, 4}
# newSet and we put them in the object set3 and set2 , which are not in the two objects set1 Here we created a copy of the elements in the object
newSet = set1.difference(set2, set3)
# newSet Here we have shown what the object contains
print('newSet contains:', newSet)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
class set - functionintersection()
Its definition returns seta new containing the elements inthesetWho summoned it and in every setpass it has the place of the parametersets*.
built
set.intersection(*sets)
Function parameters intersection()
Parameter location We sets*pass one or more objects of the type setwith the indication that they must be comma separated when more than one object is passed.
Return value
Returns seta new containing the elements inthesetWho summoned it and in every setpass it has the place of the parametersets*.
first example
# We put integer numbers in them set Here we have defined two
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3}
# newSet and we put it in the object set2 and set1 Here we have created a copy of the elements in the two objects
newSet = set1.intersection(set2)
# newSet Here we have shown what the object contains
print('newSet contains:', newSet)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
second example
# We put integer numbers in them set Here we have defined three
set1 = {1, 2, 3, 4, 5, 6}
set2 = {1, 2, 3, 4}
set3 = {1, 2, 3}
# With no newSet element repeated and we put it in the object set3 , set2 and set1 here we created a copy of the elements in the objects
newSet = set1.intersection(set2, set3)
# newSet Here we have shown what the object contains
print('newSet contains:', newSet)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
class set - functionpop()
Its definition is used to return the value of an element that is randomly selected fromthesetThe one who called it, then it is deleted from it.
built
set.pop()
Function parameters pop()
Do not accept any parameters.
Return value
Returns the item that was deleted fromThe set.
Possible errors
Throws the TypeError exception if it isthesetEmpty.
Function example pop()
# We put 5 elements in it aSet named set Here we have defined
aSet = {10, 20, 30, 40, 50}
# and display its value aSet here we extracted the last element in the object
print('Returned element:', aSet.pop())
# aSet Here we have displayed the remaining elements in the object
print('Remaining elements:', aSet)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
Remaining elements: {10, 50, 20, 30}
class set - functionremove()
Its definition is used to delete a specified element fromthesetwho summoned her.
built
set.remove(elem)
Function parameters remove()
Where the parameter is elemwe pass an object whose value matches the value of the element we want to delete.
Return value
It does not return a value.
Possible errors
Throws an exception KeyError if no element is found inthesetIt has the same value as the object we passed in place of the parameterelem.
first example
# We put in it a set of integers called aSet. Here we have defined
aSet = {1, 2, 3, 4, 5}
# aSet Here we have deleted the element that has the value 1 in the object
aSet.remove(1)
# aSet Here we have shown what the object contains
print(aSet)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
second example
# We put in it a set of integers called aSet. Here we have defined
aSet = {1, 2, 3, 4, 5}
# When running ValueError and since there is no element that has this value the aSet error will appear to delete an element with a value 10 in the remove() object here we called the function
aSet.remove(10)
# aSet Here we have shown what the object contains
print(aSet)
• We will get the following result when running.
class set - functionsymmetric_difference()
Its definition returns seta new one that contains the elements that are not present in each of thethesetwho summoned her and inthesetWhich we pass to her in place of the parameteranotherSet.
built
set.symmetric_difference(anotherSet)
Function parameters symmetric_difference()
The location of the parameter anotherSetwe passthesetwhose elements will be compared with those ofthesetwho summoned her.
Return value
come backset a new one that contains the elements that are not present in each of thethesetwho summoned her and inthesetWhich we pass to her in place of the parameteranotherSet.
Function example symmetric_difference()
# We put integer numbers in them set Here we have defined two
set1 = {1, 2, 3, 4, 5}
set2 = {1, 2, 3}
# newSet and we put it in the object set2 and set1 Here we have created a copy of the elements that are not in both objects
newSet = set1.symmetric_difference(set2)
# newSet Here we have shown what the object contains
print('newSet contains:', newSet)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
class set - functionsymmetric_difference_update()
Define comparethesetwho summoned her withthesetWhich we pass to her in place of the parameter anotherSet.
then put inthesetThe one who summoned it only the common elements between them and erases any other elements that were in it.
built
set.symmetric_difference_update(anotherSet)
Function parameters symmetric_difference_update()
The location of the parameter anotherSetwe passthesetwhose elements will be compared with those ofthesetwho summoned her.
Return value
It does not return a value.
Function example symmetric_difference_update()
# We put integer numbers in them set Here we have defined two
set1 = {1, 2, 4, 5}
set2 = {1, 2, 3, 5, 6, 7}
# set1 in the set2 and set1 object here we have placed the non-shared elements between the two objects
set1.symmetric_difference_update(set2)
# set1 Here we have shown what the object contains
print('set1 contains:', set1)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
class set - functionunion()
Its definition returns seta new containing the elements inthesetWho summoned it and the items in each setpass it have a parameter placesets*.
built
set.union(sets*)
Function parameters union()
Parameter location We sets*pass one or more objects of the type setwith the indication that they must be comma separated when more than one object is passed.
Return value
Returns seta new containing the elements inthesetWho summoned it and the items in eachthesetWhich we pass to her in place of the parametersets*.
first example
# We put in them random integers set Here we have defined two
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
# With no newSet element being repeated and we put it in the set2 and set1 object here we created a copy of all the elements in the two objects
newSet = set1.union(set2)
# newSet Here we have shown what the object contains
print('newSet contains:', newSet)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
second example
# We put in them random integers set Here we have defined three
set1 = {1, 2, 3, 5, 8, 7}
set2 = {7, 2, 3, 5, 6, 1}
set3 = {3, 4, 5, 8, 7, 9}
# With no newSet element repeated and we put it in the object set3 , set2 and set1 Here we have created a copy of all the elements in the objects
newSet = set1.union(set2, set3)
# newSet Here we have shown what the object contains
print('newSet contains:', newSet)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
class set - functionupdate()
Define it by comparing elementsthesetThe one who summoned it, along with the items in eachset pass, has a parameter's placesets*.
then put inthesetWho summoned the common and uncommon elements between them.
built
set.update(*sets)
Function parameters update()
Parameter location We sets*pass one or more objects of the type setwith the indication that they must be comma separated when more than one object is passed.
Return value
It does not return a value.
first example
# We put integer numbers in them set Here we have defined two
set1 = {1, 2, 3}
set2 = {1, 2, 3, 4, 5}
# With no repetition of any element of course set1 in the object set2 and set1 here we have placed all the elements in the two objects
set1.update(set2)
# set1 Here we have shown what the object contains
print('set1 contains:', set1)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
second example
# We put in them random integers set Here we have defined three
set1 = {1, 2, 3, 5, 8, 7}
set2 = {7, 2, 3, 5, 6, 1}
set3 = {3, 4, 5, 8, 7, 9}
# With no repetition of any element of course set1 in the object set3 and set2 and set1 here we have placed all the elements in the objects
set1.update(set2, set3)
# set1 Here we have shown what the object contains
print('set1 contains:', set1)
• We will get a result similar to the following when running because we don't know how the items will be arranged.
class set - functionlen()
The function definition len() returns an integer representing the number of elementsthesetwhich we pass to her when she is summoned.
Build function len()
len(set)
Function parameters len()
The location of the parameter setwe passthesetfor which we want to get the number of its elements.
function return value len()
Returns an integer representing the number of elementsthesetwhich we pass to her when she is summoned.
Illustrative example of the function len()
# We put in it a set of integers called aSet. Here we have defined
aSet = {1, 2, 3, 4, 5}
# len() that the aSet function will return Here we have shown the number of elements of the object
print('Array length is:', len(aSet))
• We will get the following result when running.
class set - functionmin()
Definition of a function min() that returns the smallest value inthesetwhich we pass to her when she is summoned.
Build function min()
min(set)
Function parameters min()
The location of the parameter setwe passthesetin which we want to get the smallest value.
function return value min()
Returns the smallest value inthesetwhich we pass to her when she is summoned.
An example showing the function min()
# We put in it a set of integers called aSet. Here we have defined
aSet = {5, 2, 4, 6, 3}
# min() which the aSet function will return here we have shown the smallest value in the object
print('Minimum value is:', min(aSet))
• We will get the following result when running.
class set - functionmax()
Function definition max() that returns the largest value inthesetwhich we pass to her when she is summoned.
Build function max()
max(set)
Function parameters max()
Parameter locationsetwe passthesetWhich we want to get the most value in.
function return value max()
Returns the largest value inthesetwhich we pass to her when she is summoned.
Example of the function max()
# We put in it a set of integers called aSet. Here we have defined
aSet = {5, 2, 4, 6, 3}
# max() which the aSet function will return here we have shown the largest value in the object
print('Maximum value is:', max(aSet))
• We will get the following result when running.
class set - functionset()
The function definition set() returns a copy of any object containing a set of elements that we pass to it when called as a class objectThe set.
Build function set()
set(sequence)
Function parameters set()
In place of the parameter sequence, we pass an object that represents an object that represents an array of elements.
function return value set()
Returns a copy of any object containing a set of elements that we pass to it when called as a class objectThe set.
An example showing the function set()
# We put in it an array of integers called aList, here we have defined aList = [1, 2, 3, 4, 5] # aSet that you will return in the set object then we store the .set object to return a copy of it as an object of the aList class on the set() object here we called the function aSet = set(aList) # aSet Here we have shown what the object contains print(aSet)
• We will get the following result when running.