Python Tuples:
- Tuples are used to store multiple items in a single variable.
- A tuple is a collection which is ordered and unchangeable.
- A tuple is like a list, but it cannot be changed (immutable).
- Written inside round brackets ().
Ex:
tup = (5, 'Welcome', 7.5, True, [1, 2, 3], {'key': 'value'})
print(tup)
Change Tuple Values:
Once a tuple is created, you cannot change its values. Tuples are unchangeable, or immutable as it also is called.
You can convert the tuple into a list, change the list, and convert the list back into a tuple.
Ex:
x = ("apple", "banana", "cherry")
y = list(x)
y[1] = "kiwi"
x = tuple(y)
print(x)
Join Two Tuples:
- To join two or more tuples you can use the + operator
Ex:
tuple1 = ("a", "b" , "c")
tuple2 = (1, 2, 3)
tuple3 = tuple1 + tuple2
print(tuple3)
Set:
Sets are used to store multiple items in a single variable.
A set is a collection which is unordered, unchangeable.
Ex:
thisset = {"apple", "banana", "cherry"}
print(thisset)
- Sets cannot have two items with the same value. Duplicate values will be ignored:
Ex:
thisset = {"apple", "banana", "cherry", "apple"}
print(thisset)
Add Items and Remove Item:
To add one item to a set use the add() method.
To add items from another set into the current set, use the update() method.
Ex:
thisset = {"apple", "banana", "cherry"}
thisset.add("orange")
print(thisset)
thisset = {"apple", "banana", "cherry"}
tropical = {"pineapple", "mango", "papaya"}
thisset.update(tropical)
print(thisset)
Remove Item:
To remove an item in a set, use the remove(), or the discard() method.
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.
Ex:
thisset = {"apple", "banana", "cherry"}
thisset.remove("banana")
print(thisset)
thisset = {"apple", "banana", "cherry"}
x = thisset.pop()
print(x)
print(thisset)
- The clear() method empties the set Ex:
thisset = {"apple", "banana", "cherry"}
thisset.clear()
print(thisset)
Join Sets():
There are several ways to join two or more sets in Python.
The union() and update() methods joins all items from both sets.
The intersection() method keeps ONLY the duplicates.
The difference() method keeps the items from the first set that are not in the other set(s).
The symmetric_difference() method keeps all items EXCEPT the duplicates.
Union():
The union() method returns a new set with all items from both sets.
You can use the | operator instead of the union() method, and you will get the same result.
Ex:
set1 = {"a", "b", "c"}
set2 = {1, 2, 3}
set3 = set1 | set2
print(set3)
Intersection():
The intersection() method will return a new set, that only contains the items that are present in both sets.
We can use the & operator instead of the intersection() method, and you will get the same result.
Ex:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1 & set2
print(set3)
Difference():
The difference() method will return a new set that will contain only the items from the first set that are not present in the other set.
You can use the - operator instead of the difference() method, and you will get the same result.
Ex:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1 - set2
print(set3)
Symmetric Differences():
The symmetric_difference() method will keep only the elements that are NOT present in both sets.
You can use the ^ operator instead of the symmetric_difference() method, and you will get the same result.
Ex:
set1 = {"apple", "banana", "cherry"}
set2 = {"google", "microsoft", "apple"}
set3 = set1 ^ set2
print(set3)
Top comments (0)