Lab# 11 and 12 Set
Lab# 11 and 12 Set
Set Items
✓ Set items are unordered, unchangeable, and do not allow duplicate values.
Unordered
✓ Unordered means that the items in a set do not have a defined order.
✓ Set items can appear in a different order every time you use them, and cannot be referred to by
index or key.
Unchangeable
✓ Set items are unchangeable, meaning that we cannot change the items after the set has been
created.
Duplicates Not Allowed
✓ Sets cannot have two items with the same value.
✓ The values True and 1 are considered the same value in sets, and are treated as duplicates.
✓ The values False and 0 are considered the same value in sets, and are treated as duplicates
Get the Length of a Set
✓ To determine how many items a set has, use the len() function.
type()
✓ From Python's perspective, sets are defined as objects with the data type 'set'
Python - Access Set Items
Access Items
✓ Once a set is created, you cannot change its items, but you can add new items.
✓ To add one item to a set use the add() method.
Add Sets
✓ To add items from another set into the current set, use the update() method.
Add Any Iterable
✓ The object in the update() method does not have to be a set, it can be any iterable object
(tuples, lists, dictionaries etc.)
Remove Item
✓ You can also use the pop() method to remove an item, but this method will remove a
random item, so you cannot be sure what item that gets removed.
✓ The return value of the pop() method is the removed item