Python Tuples and Sets
In this tutorial you will learn:
- Python Tuples
- Python Sets
- Difference between Lists, Tuples and Sets
- Accessing elements in Tuples and Sets
- Adding elements in Tuples and Sets
- Updating elements in Tuples and Sets
- Removing elements from Tuples and Sets
Python Tuples
Tuple is simply a collection of immutable Python objects it means that those objects cannot be changed. One of the properties of Tuples is that they are ordered. In order to make a Tuple in Python we use round brackets.
For example:
emptyTuple = () #declares an empty Tuple
tupleOfNumbers = (7, 3 ,10) #declares a Tuple of numbers
tupleOfStrings = (“Python”, “Test”) #Tuple of strings
Python Sets
Sets in python is also a collection of objects but they are unordered. One of the major advantages of a Set is that it cannot contain multiple instances of the same element, which makes it very useful in scenarios when we don’t want repetition in our data. Sets are declared using curly brackets.
For example:
emptySet = {} #declares an empty Set
setOfNumbers = {4,1,6} #declares a Set of numbers
setOfStrings = {"Python", "Test", "Programming", "Book", "Table"}#Set of strings
print("Output Result")
print(emptySet)
print(setOfNumbers)
print(setOfStrings)
Difference between Lists, Tuples and Sets
Lists are mutable while Sets and Tuples are immutable and hence unchangeable. Square brackets are used for Lists, round brackets are used for Tuples and curly brackets are used for Sets. Lists and Tuples can contain duplicate elements while Sets cannot contain duplicate elements, this is one of the major difference that differentiates Sets from these other data types. Lists and Tuples are ordered while Sets are unordered so we cannot say in which order the elements would appear in the result. This can also be seen in the result screenshot of the above code. Note the order of printed data it’s not the same as declared in the code.
Accessing elements in a Tuple
In order to access elements in a Tuple we use square brackets just we used to access elements in a List. We can either use the index directly. Or slice the Tuple to get a range of values.
For example:
myTuple1 = (89, 33, "Python", "Test", "Programming")
print("Output Result")
print("Third element of tuple: ", myTuple1[2]) #directly through index
print("Third to 5th element of tuple: ", myTuple1[2:5]) #slicing
Accessing elements in a Set
In order to access elements in a set we cannot use an index with square brackets since sets are unordered and their elements don’t have an index. To check if an element exist in a set we use ‘in’ keyword. Another way is to loop through the Set which will be discussed at a later stage.
For example:
print("Set Output Result")
print("Third element of set: ", "Python" in mySet1) #It will return a true or false if that element exists in set
print("Complete set: ", mySet1)
Adding elements in Tuples
Once a Tuple is created we cannot add elements to it.
Adding elements in Sets
We can add single element in a set using the add() function. In order to add multiple elements Python also provides us with an update function which can add multiple values at once.
For example:
print("\n Adding elements in sets usind add()")
mySet1.add("Coding")
mySet1.add("Skills")
print(mySet1)
print("\n Adding elements in sets using update()")
mySet1.update(["tutorial", 1, 2 ,3 ])
print(mySet1)
Updating elements in Tuples and Sets
As we discussed in the previous tutorial that Tuples and Sets are immutable so their values cannot be changed once they are assigned.
Removing elements from Tuples and Sets
Tuples are unchangeable so you cannot delete individual elements but you can delete the whole Tuple using del keyword. As for Sets elements can be removed using the remove function.
print("\n Removing elements in sets using")
del myTuple1
mySet1.remove("Programming")
mySet1.remove("Skills")
print(mySet1)
Add new comment
- 347 views