Sets in Python
Python Sets
In Python, sets are a powerful data structure used to store multiple items in a single variable. This lesson will explore the characteristics of sets, how to create them, and their usage in Python, emphasizing important SEO keywords such as "Python sets," "set items," and "set data types."
1. What is a Set?
A set is one of the four built-in data types in Python used to store collections of data. The other three are list, tuple, and dictionary, each with its unique qualities and usage.
Characteristics of Sets:
- Unordered: The items in a set do not have a defined order.
- Unchangeable: Once a set is created, you cannot change its items, although you can add or remove items.
- Unindexed: Set items cannot be accessed by index or key.
- No Duplicate Values: Sets do not allow duplicate values.
Example: Create a Set
python
1# Creating a set
2fruits_set = {"apple", "banana", "cherry"}
3print(fruits_set) # Output: {'banana', 'cherry', 'apple'} (Order may vary)
Note: Since sets are unordered, the items may appear in a different order each time you use them.
2. Set Items
Set items possess specific characteristics:
2.1 Unordered
Items in a set do not have a defined order. This means that the items can appear in different orders and cannot be referred to by index.
2.2 Unchangeable
While you cannot modify the items in a set directly, you can remove existing items and add new ones.
Example: Attempt to Add Duplicate Values
python
1# Demonstrating that duplicate values are ignored
2fruits_set = {"apple", "banana", "cherry", "apple"}
3print(fruits_set) # Output: {'banana', 'cherry', 'apple'} (Duplicates are ignored)
2.3 Duplicate Values
Sets do not allow duplicate values. For example, the values True
and 1
are considered the same value in sets.
Example: True and 1 Considered the Same
python
1# Example showing True and 1 are treated as duplicates
2mixed_set = {"apple", "banana", "cherry", True, 1}
3print(mixed_set) # Output: {'banana', 'cherry', 'apple'} (True and 1 are counted as one)
Example: False and 0 Considered the Same
python
1# Example showing False and 0 are treated as duplicates
2another_set = {"apple", "banana", "cherry", False, 0}
3print(another_set) # Output: {'banana', 'cherry', 'apple'} (False and 0 are counted as one)
3. Get the Length of a Set
To determine how many items a set contains, you can use the len()
function.
Example: Get the Number of Items in a Set
python
1# Original set
2fruits_set = {"apple", "banana", "cherry"}
3print(len(fruits_set)) # Output: 3
4. Set Items - Data Types
Set items can consist of any data type. A set can include strings, integers, and booleans.
Example: Different Data Types in Sets
python
1# Sets with various data types
2set_of_strings = {"apple", "banana", "cherry"}
3set_of_numbers = {1, 5, 7, 9, 3}
4set_of_booleans = {True, False}
5
6# A set containing different data types
7mixed_set = {"abc", 34, True, 40, "male"}
8print(mixed_set) # Output: {True, 40, 'abc', 'male', 34} (Order may vary)
5. What is the Data Type of a Set?
From Python's perspective, sets are defined as objects with the data type 'set'.
Example: Check the Data Type of a Set
python
1# Check the data type of a set
2my_set = {"apple", "banana", "cherry"}
3print(type(my_set)) # Output: <class 'set'>
6. The set() Constructor
You can also create a set using the set()
constructor, which allows for the creation of a set from other iterable collections.
Example: Using the set() Constructor
python
1# Creating a set using the set() constructor
2fruits_set = set(("apple", "banana", "cherry")) # Note the double round brackets
3print(fruits_set) # Output: {'banana', 'cherry', 'apple'} (Order may vary)