-
Definition of Sets
- Sets are used to store multiple items in a single variable.
- One of four built-in data types in Python for collections, alongside List, Tuple, and Dictionary.
-
Characteristics of Sets
- Unordered: Items do not have a defined order and appear in a random sequence.
- Unchangeable: Items cannot be modified after creation, but items can be added or removed.
- Unindexed: Items cannot be accessed via an index or key.
- No Duplicates: Each item must be unique within the set.
-
Creating a Set
- Sets are defined using curly brackets (
{}
). - Example:
myset = {"apple", "banana", "cherry"}
. - Sets can also be created using the
set()
constructor.
- Sets are defined using curly brackets (
-
Duplicate Values
- Sets do not allow duplicate values.
- The values
True
and1
are treated as duplicates. - The values
False
and0
are treated as duplicates.
-
Checking the Length
- Use the
len()
function to determine the number of items in a set. - Example:
len(myset)
.
- Use the
-
Data Types in Sets
- Set items can be of any data type (e.g., strings, integers, booleans).
- A set can contain mixed data types.
-
Using the
set()
Constructor- Example:
set(("apple", "banana", "cherry"))
creates a set.
- Example:
-
Python Collections (Comparison)
- List: Ordered, changeable, allows duplicates.
- Tuple: Ordered, unchangeable, allows duplicates.
- Set: Unordered, unchangeable, unindexed, no duplicates.
- Dictionary: Ordered (Python 3.7+), changeable, no duplicates.
-
Exercise
- Example question: Which one is a set?
- Correct answer:
myset = {'apple', 'banana', 'cherry'}
.
- Correct answer:
- Example question: Which one is a set?
-
Efficiency and Usefulness
- Choosing the right collection type can improve efficiency, security, and retention of meaning.
💥 YouTube https://www.youtube.com/channel/UCJojbxGV0sfU1QPWhRxx4-A
💥 Blog https://localedxcelcambridgeictcomputerclass.blogspot.com/
1. Access Items in a Set
- Indexing Not Allowed: Items in a set cannot be accessed using an index or a key.
- Looping Through Items: You can loop through the set using a
for
loop.- Example:
thisset = {"apple", "banana", "cherry"} for x in thisset: print(x)
- Example:
- Check for Item Presence: Use the
in
keyword to check if a value exists in the set.- Example:
print("banana" in thisset)
- Example:
- Check for Item Absence: Use the
not in
keyword to check if a value does not exist in the set.- Example:
print("banana" not in thisset)
- Example:
2. Changing Items in a Set
- Items Cannot Be Changed: Once a set is created, its items cannot be modified.
- Adding New Items: You can add new items to a set.
These points summarize all key aspects of accessing and working with items in a set.
💥 YouTube https://www.youtube.com/channel/UCJojbxGV0sfU1QPWhRxx4-A
💥 Blog https://localedxcelcambridgeictcomputerclass.blogspot.com/
1. Adding Items to a Set
- Items Cannot Be Changed: Once a set is created, its existing items cannot be modified.
- Adding New Items: You can add new items to a set using the
add()
method.- Example:
thisset = {"apple", "banana", "cherry"} thisset.add("orange") print(thisset)
- Example:
2. Adding Items from Another Set
- Use the
update()
Method: To add all items from one set to another, use theupdate()
method.- Example:
thisset = {"apple", "banana", "cherry"} tropical = {"pineapple", "mango", "papaya"} thisset.update(tropical) print(thisset)
- Example:
3. Adding Any Iterable
- Accepts Iterable Objects: The
update()
method can add items from any iterable object, such as tuples, lists, or dictionaries.- Example (Adding a List):
thisset = {"apple", "banana", "cherry"} mylist = ["kiwi", "orange"] thisset.update(mylist) print(thisset)
- Example (Adding a List):
4. Exercise
- Correct Syntax for Adding Items:
- Answer:
add()
- Answer:
These points summarize how to add items to a set in Python.
--
Here is a list of all key points from the text:
1. Removing Items from a Set
- Methods for Removing Items: Use the
remove()
ordiscard()
methods.-
Example (Using
remove()
):thisset = {"apple", "banana", "cherry"} thisset.remove("banana") print(thisset)
Note: If the item does not exist,
remove()
will raise an error. -
Example (Using
discard()
):thisset = {"apple", "banana", "cherry"} thisset.discard("banana") print(thisset)
Note: If the item does not exist,
discard()
will NOT raise an error.
-
2. Removing a Random Item
- Use the
pop()
Method: Removes a random item from the set.- Example:
Note: Since sets are unordered, you cannot predict which item will be removed.thisset = {"apple", "banana", "cherry"} x = thisset.pop() print(x) # Removed item print(thisset) # Remaining set
Return Value: The removed item is returned by thepop()
method.
- Example:
3. Clearing the Set
- Use the
clear()
Method: Empties the set.- Example:
thisset = {"apple", "banana", "cherry"} thisset.clear() print(thisset) # Outputs: set()
- Example:
4. Deleting the Set
- Use the
del
Keyword: Deletes the set entirely.- Example:
thisset = {"apple", "banana", "cherry"} del thisset # Accessing `thisset` after this will raise an error as it no longer exists.
- Example:
5. Exercise
- Correct Syntax for Removing an Item:
- Answer:
remove()
- Answer:
💥 YouTube https://www.youtube.com/channel/UCJojbxGV0sfU1QPWhRxx4-A
💥 Blog https://localedxcelcambridgeictcomputerclass.blogspot.com/
Here is a list of key points from the text:
1. Looping Through Set Items
- Using a
for
Loop: You can iterate over items in a set using afor
loop.- Example:
Note: Since sets are unordered, the order of items in the loop is not guaranteed.thisset = {"apple", "banana", "cherry"} for x in thisset: print(x)
- Example:
2. Exercise
- Correct Syntax for Looping Through Set Items:
- Answer:
for x in {'apple', 'banana', 'cherry'}: print(x)
- Answer:
These points summarize how to loop through a set and identify the correct syntax for iterating over set items.
-------------
Python - Join Sets: Key Points
1. Joining Sets
union()
method: Combines all items from both sets into a new set.- Example:
set1 = {"a", "b", "c"} set2 = {1, 2, 3} set3 = set1.union(set2) print(set3)
- Example:
|
operator: An alternative tounion()
for joining sets.- Example:
set3 = set1 | set2 print(set3)
- Example:
2. Joining Multiple Sets
- Using
union()
method: Add multiple sets in parentheses separated by commas.- Example:
myset = set1.union(set2, set3, set4) print(myset)
- Example:
- Using
|
operator: Separate multiple sets with additional|
operators.- Example:
myset = set1 | set2 | set3 | set4 print(myset)
- Example:
3. Joining a Set and Other Data Types
union()
method: Allows joining sets with lists, tuples, etc., resulting in a set.- Example:
x = {"a", "b", "c"} y = (1, 2, 3) z = x.union(y) print(z)
- Note: The
|
operator works only with sets.
- Example:
4. Updating Sets
update()
method: Adds all items from one set to another and modifies the original set.- Example:
set1 = {"a", "b", "c"} set2 = {1, 2, 3} set1.update(set2) print(set1)
- Example:
5. Intersection of Sets
intersection()
method: Returns a new set with only common items (duplicates) from both sets.- Example:
set3 = set1.intersection(set2) print(set3)
- Example:
&
operator: Alternative tointersection()
.- Example:
set3 = set1 & set2 print(set3)
- Example:
intersection_update()
method: Keeps only duplicates in the original set.- Example:
set1.intersection_update(set2) print(set1)
- Example:
6. Difference of Sets
difference()
method: Returns items from the first set not present in the second set.- Example:
set3 = set1.difference(set2) print(set3)
- Example:
-
operator: Alternative todifference()
.- Example:
set3 = set1 - set2 print(set3)
- Example:
difference_update()
method: Modifies the original set to keep items not in the other set.- Example:
set1.difference_update(set2) print(set1)
- Example:
7. Symmetric Difference of Sets
symmetric_difference()
method: Keeps all items except the duplicates from both sets.- Example:
set3 = set1.symmetric_difference(set2) print(set3)
- Example:
^
operator: Alternative tosymmetric_difference()
.- Example:
set3 = set1 ^ set2 print(set3)
- Example:
symmetric_difference_update()
method: Keeps all except duplicates in the original set.- Example:
set1.symmetric_difference_update(set2) print(set1)
- Example:
8. Special Cases
- Duplicate Value Consideration:
True
and1
are considered the same value.False
and0
are considered the same value.- Example:
set1 = {"apple", 1, "banana", 0} set2 = {False, "google", 1, True} set3 = set1.intersection(set2) print(set3)
9. Exercise
- Correct Syntax for Joining Sets:
- Answer:
set3 = set1.union(set2)
💥 YouTube https://www.youtube.com/channel/UCJojbxGV0sfU1QPWhRxx4-A
💥 Blog https://localedxcelcambridgeictcomputerclass.blogspot.com/
Set Methods and Descriptions
-
add()
Adds an element to the set. -
clear()
Removes all the elements from the set. -
copy()
Returns a copy of the set. -
difference()
(-
)
Returns a set containing the difference between two or more sets. -
difference_update()
(-=
)
Removes the items in this set that are also included in another, specified set. -
discard()
Removes the specified item without raising an error if the item doesn't exist. -
intersection()
(&
)
Returns a set that is the intersection of two other sets (common items). -
intersection_update()
(&=
)
Removes the items in this set that are not present in other, specified set(s). -
isdisjoint()
Returns whether two sets have no intersection (True if they share no elements). -
issubset()
(<=
)
Returns whether all items in this set are present in another specified set. -
<
Returns whether this set is a proper subset of another set. -
issuperset()
(>=
)
Returns whether all items in another set are present in this set. -
>
Returns whether this set is a proper superset of another set. -
pop()
Removes and returns an arbitrary element from the set (raises an error if the set is empty). -
remove()
Removes the specified element (raises an error if the element does not exist). -
symmetric_difference()
(^
)
Returns a set with the symmetric differences of two sets (items not present in both sets). -
symmetric_difference_update()
(^=
)
Updates the set with the symmetric differences of this set and another. -
union()
(|
)
Returns a set containing the union of two or more sets (all unique elements from all sets). -
update()
(|=
)
Updates the set with the union of this set and others (adds all items from the specified sets).
These methods provide a wide range of operations to manipulate and analyze sets in Python.
💥 YouTube https://www.youtube.com/channel/UCJojbxGV0sfU1QPWhRxx4-A
💥 Blog https://localedxcelcambridgeictcomputerclass.blogspot.com/
💥 WordPress https://computerclassinsrilanka.wordpress.com
💥 Facebook https://web.facebook.com/itclasssrilanka
💥 Wix https://itclasssl.wixsite.com/icttraining
💥 Web https://itclasssl.github.io/eTeacher/
💥 Medium https://medium.com/@itclasssl
💥 Quora https://www.quora.com/profile/BIT-UCSC-UoM-Final-Year-Student-Project-Guide
💥 mystrikingly https://bit-ucsc-uom-final-year-project-ideas-help-guide-php-class.mystrikingly.com/
💥 https://elakiri.com/threads/bit-ucsc-uom-php-mysql-project-guidance-and-individual-classes-in-colombo.1627048/
💥 https://bitbscucscuomfinalprojectclasslk.weebly.com/
💥 https://www.tiktok.com/@onlinelearningitclassso1
💥 https://payhip.com/eTeacherAmithafz/
💥 https://discord.gg/cPWAANKt
No comments:
Post a Comment