Implement Array Sets In Python
An array specifies a variable that can be indexed as a list in rows and columns.
The first index is zero as is common is most programming languages. Python does not have an array data type. Instead the data type list, tuple, set or dictionary can be used.
A set is a collection which is ordered and unchangeable not allowing duplicate members. Sets are created using curly brackets..
Common Functions For Manipulating Sets In Python
Common Set Functions
Name | Description | Example |
---|---|---|
len() | Determine how many items | len(fruits) |
add() | Add one item | fruits.add(“Orange”) |
update() | Add elements from one set | fruits.update(“fruits2”) |
remove() | Remove specified item | fruits.remove(“Banana”) |
discard() | Discard specified item | fruits.discard(“Cranberry”) |
clear() | Remove all values | fruits.clear() |
difference() | Returns a set containing the difference between two or more sets | set3.difference(set2) |
difference_update() | Removes the items in this set that are also included in another, specified set | set1.difference_update(set2) |
intersection() | Returns a set, that is the intersection of two other sets | set3 = set1 & set2 |
intersection_update() | Returns a set, that is the intersection of two other sets | set1.intersection_update(set2) |
symmetric_difference() | Returns a set with the symmetric differences of two sets | set3 = set1.symmetric_difference(set2) |
symmetric_difference_update() | Inserts the symmetric differences from this set and another | set3 = set1.symmetric_difference_update(set2) |
union() | Return a set containing the union of sets | set3 = set1.union(set2) |
count() | Number of elements with specific value | set1.count(2) |
index() | Returns index of first element with specified value | set1.index(4) |
Name | Description | Example |
Python Set Snippet
# OjamboShop.com Learning Python Array Set Tutorial # fruits = {"Apple", "Banana", "Cranberry"} print(fruits) print(len(fruits)) # Number Of Items In Set fruits.add("Orange") # Add Item To Set print(fruits) fruits2 = {"pineapple", "mango"} fruits.update(fruits2) # Add Items From Another Set print(fruits) fruits.remove("pineapple") # Remove Item From Set print(fruits) fruits.discard("pineapple") # Discard Item From Set print(fruits) fruits.clear() # Remove All Items print(fruits) set1 = {"AA", "BB", "CC"} set2 = {11, 22, 33} set3 = set1.union(set2) # New Set With All Items From Both Sets print(set3)
Usage
You can use any IDE or text editor and the command line to compile and execute Python code. For this tutorial, the OjamboShop.com Learning Python Course Web IDE can used to input and compile Python code for sets.
Open Source
Python is licensed under the Python Software Foundation License. This allows commercial use, modification, distribution, and allows making derivatives proprietary.
Learn Programming Courses:
Courses are optimized for your web browser on any device.
Limited Time Offer:
OjamboShop.com is offering 20% off coupon code SCHOOL for Learning Python Course or for Learning PHP Course until End Day 2024.
Learn Programming Ebooks:
Ebooks can be downloaded to your reader of choice.
Conclusion:
Python makes it easy to use sets. Use sets when you need an unordered, unchangeable array without duplicate members such as unique memberships.
Take this opportunity to learn the Python or PHP programming language by making a one-time purchase at Learning Python Course or Learning PHP Course. A web browser is the only thing needed to learn Python or PHP in 2024 at your leisure. All the developer tools are provided right in your web browser.
If you prefer to download ebook versions for your reader then you may purchase at Learning Python Ebook or Learning PHP Ebook
For custom websites, app development and one-on-one tutorials, go to OjamboServices.com.
References:
- Learning Python Course on OjamboShop.com
- Learning PHP Course on OjamboShop.com
- Learning Python Ebook on Amazon
- Learning PHP Ebook on Amazon
- OjamboServices.com For Custom Websites, Applications & Tutorials