Python Tutorials
Python File Handling
Python Modules
myset = {"apple", "banana", "cherry"}
Sets are used to store multiple items in one place.
Set is one of 4 data types built into Python that are used to store data collections, the other 3 List, Tuple, and a dictionary, all with different attributes and uses.
Set a random, unmodified *, and unidentified collection.
*Note: Default items do not change, but you can delete items and add new items.
Sets are written in italics.
Create a Set:
thisset = {"apple", "banana", "cherry"}
print(thisset)
Note: The sets are not in order, so you cannot be sure which system will appear.
The set items are random, unchanged, and do not allow duplicate values.
Non-order means that items in the set do not have a specified order.
Default items may appear in a different format each time you use them, and cannot be referenced by index or key.
The preset items do not change, which means we will not be able to change the items once they have been created.
Once a set has been created, you can't change its elements, but you can remove items and add new ones.
Sets cannot have two items of the same value.
Duplicate values will be ignored:
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
To determine how many items a set has, use the len()
method.
Get the number of items in a set:
thisset = {"apple", "banana", "cherry"}
print(len(thisset))
Set items can be any type of data:
String, int and boolean data types:
set1 = {"apple", "banana", "cherry"}
set2 = {1, 5, 7, 9, 3}
set3 = {True, False, False}
The set may contain different types of data:
A set with strings, integers and boolean values:
set1 = {"abc", 34, True, 40, "male"}
From Python's point of view, sets are defined as data sets:
<class 'set'>
What is the data type of a set?
myset = {"apple", "banana", "cherry"}
print(type(myset))
It is also possible to use the set()
to create the set.
Using the set() constructor to make a set:
thisset = set(("apple", "banana", "cherry")) # note the double round-brackets
print(thisset)
There are four types of data collection in Python editing language:
* Set items do not change, but you can delete items and add new items.
** Starting with version 3.7 of Python, dictionaries are in order. In Python 3.6 and earlier, dictionaries are not organized.
When choosing a type of collection, it is helpful to understand the characteristics of that type. Choosing the right type of specific data set may mean keeping the definition, and, it may mean increased efficiency or security.
You can not access the set items by pointing to the index or key.
But you can enter the settings using a loop, or ask if the specified value is in the set, by using the key in.
Loop through the set, and print the values:
thisset = {"apple", "banana", "cherry"}
for x in thisset:
print(x)
Check if "banana" is present in the set:
thisset = {"apple", "banana", "cherry"}
print("banana"
in thisset)
Once a set has been created, you can not change its elements, but you can add new items.