Social Icons

Thursday, August 15, 2019

Python Sets

1 - Define a Python Set 

A set in Python(Python set) is an unordered and unindexed collection.
In Python, sets are written with braces {....}.

Creating a set:

mySet = {"Pen", "Pencil", "Eraser"}
print(mySet)
# Poster: {'Eraser', 'Pencil', 'Pen'}

Note

Sets are not ordered, so items will appear in random order.

2 - Access to the elements of a Python set 

You can not access items in a set by referencing an index, because sets are not ordered, items do not have indexes. But you can browse the elements of the set using a for loop or ask if a specified value is present in a set using the in keyword.

 Example: displaying elements of a set

mySet = {"Pen", "Pencil", "Eraser"}
for x in mySet:
    print(x)
This will display:
Pencil 
Rubber 
Pen 
You notice that each time you make a new run of the program, there is a different display order. Which means that the order in the Python sets does not matter!

 Example: checking membership of an element

mySet = {"Pen", "Pencil", "Eraser"}
print("Pencil" in mySet) # displays: True
print("Notebook" in mySet) # displays: False

3 - Length or Cardinal of a Python Set 

To know the length(cardinal) of a Python set, we use the len() method

Example: length of a python set

mySet = {"Pen", "Pencil", "Eraser"}
cardinal = len(mySet)
print("card(mySet) =", cardinal)
# displays card(mySet) = 3

4 - Operations: add, delete or update a Python set 

4.1 Add one or more elements to a Python set 

- To add an element to a Python set, you can use the add() method:

Example: adding an element to a set

mySet = {"Pen", "Pencil", "Eraser"}
mySet.add("Notebook")
print(mySet)
- We can also add several elements at the same time with the update() method:

Example: Add multiple items

mySet = {"Pen", "Pencil", "Eraser"}
mySet.update(["Notebook", "Schoolbag", "Kit"])
print(mySet)

4.2 Remove an element from a Python set 

To remove an element from a Python set, you have two choices for the remove() method or the discard() method.

Example: remove "Pencil" by the remove() method

mySet = {"Pen", "Pencil", "Eraser"}
mySet.remove( "Pen")
print(mySet) # show {'Eraser', 'Pencil'}

Note: 

 If the item to delete does not exist, remove() will generate an error.

Example: delete "Pencil" by the discard() method:

mySet = {"Pen", "Pencil", "Eraser"}
mySet.discard( "Pen")
print(mySet) # show {'Eraser', 'Pencil'}

Note: 

 Unlike the remove() method, the discard() method does not generate an error when the item to delete does not exist! The delete instruction will simply be ignored!

 NOTE: 

 You can also use the pop() method to delete an item, but this method will delete the last item. Remember that sets are not ordered and you will not know which item will be deleted. The deletion is totally random!

 4.3 Empty a Python Set 

- To empty Python together, use the clear() method 

Example: empty a Python set

mySet = {"Pen", "Pencil", "Eraser"}
mySet.clear()
print(mySet) # displays set {} which means an empty set

4.4 Delete a set 

To delete a Python set, you can use the del command

Example: delete a set

mySet = {"Pen", "Pencil", "Eraser"}
mySet
print(mySet)
# displays the error message: builtins.NameError: name 'mySet' is not defined

5 Summary of Methods Associated with a Python Set


  1. add():  add an element to the set 
  2. clear(): removes all elements from the set 
  3. copy(): returns a copy of the set 
  4. difference(): Returns a set containing the difference between two or more sets. 
  5. difference_update(): removes items from a set that are also included in another specified set 
  6. discard(): delete the specified item 
  7. intersection(): returns a set, which is the intersection of two other sets. 
  8. intersection_update(): removes items in this set that are not present in other specified sets. 
  9. isdisjoint(): Indicates whether two sets intersect or not. 
  10. issubset(): indicates whether another game contains this game or not. 
  11. issuperset(): indicates whether this set contains another set or not. 
  12. pop(): removes an element from the set 
  13. remove(): delete the specified element 
  14. symmetric_difference(): returns a set with the symmetric differences of two sets 
  15. symmetric_difference_update(): inserts the symmetric differences of this set and another 
  16. union(): returns a set containing the union of sets 
  17. update(): update the set with the union of this set and others


Younes Derfoufi

No comments:

Post a Comment

Category Of Mobile Courses

Actualités (644) Adsense (1) Affiliation (1) Algebraic Topology (1) Algorithmic (1) all-news (30) Android (5) Android App (8) Android app without code (4) Android Apps (168) Android Development (4) Android download (2) Android OS (3) AngularJS (1) Automata theory and formal language (5) C programming (5) Category and Functor (8) CMS (3) Computer Glossary (18) Create Mobile App With Ionic Framework (2) CSS (2) CSS-Cascading-Style-Sheets (4) Developpement Java (13) Differential Geometry (1) Django-Python-Framework (8) dropshiping (26) Earn Money by Internet (4) Emplois (23) Framework php (2) Fraud (2) HTML (7) Java For Beginners (10) Javascript (12) Kotlin Programming Language (8) Kotlin For Mobile Android (1) Linux Download (2) Marketing (5) Mobile (3) Mobile Courses (4) Mobile Marketing (4) MoneyGram (1) News (721) Node.js (5) Open Source (1) Photoshop (1) Protect Computer (1) Python (32) Python BeautifulSoup (1) Python For Data Science (2) Python-Books (6) Python-DVD-Training (1) Python-Exercises (138) Python-Framework (1) Python-IDE (1) Python-Modules (1) Python-pdf (2) Python-pyQt (1) Référencement (2) Script PHP (2) Security (6) SEO (1) Snipping Tool: Faq (1) Social Networks (1) Source Code (1) Statistics With SPSS (2) Surveillance Software (1) Travail à domicile (6) Tutoriels php en vidéos (2) Tutoriels-MySql (6) tutoriels-php (19) Utilitaires (1) VPS (1) Web Hosting (1) Webcam (1) Webmarketing (11) Western Union (1) Windows 10 (1) Windows 7 (4) Windows 7 Faq (2) Windows 8 (1) Windows Accessories (1) Windows Download (8) Windows Drivers (1) Windows Fonts (1) Windows Power Shell (2) Windows Registry (2) Windows Security (18) Windows Software (2) Windows Spyware (2) Windows utilities (3) Windows Virus (2) Windows Vista (3) Windows Wireless (1) Windows xp (1) Wordpress (1)
 

Sample text

Sample Text

 
Blogger Templates