1.Tuples :
1.1 introduction to the Tuple :
-
A tuple is a collection which is ordered and unchangeable. In Python tuples are written with round brackets.
-
Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets.
-
Creating a tuple is as simple as putting different comma-separated values.
-
Technically, lists are mutable objects and tuples are immutable objects. Mutable objects can change (think of mutations), and immutable objects can not change.
Example :
- creating a tuple :
thistuple = ("apple", "banana", "cherry")
print(thistuple) #output ('apple', 'banana', 'cherry')
1.2 Access Tuple Items :
- You can access tuple items by referring to the index number, inside square brackets
Example :
- access the item present in the tuple
thistuple = ("apple", "banana", "cherry")
print(thistuple[1]) #output banana
1.3 Change Tuple Values :
- Once a tuple is created, you cannot change its values.
- Tuples are unchangeable.
Example :
- example tho show the cannot chance the value of tuple
thistuple = ("apple", "banana", "cherry")
thistuple[1] = "blackcurrant"
# The values will remain the same: #output ('apple', 'banana', 'cherry')
print(thistuple)
1.4 Loop Through a Tuple :
- You can loop through the tuple items by using a for loop.
Example :
- Iterate through the items and print the values.
thistuple = ("apple", "banana", "cherry") #output apple
for x in thistuple: banana
print(x) cherry
1.5 Adding and Removing the tuple items :
- Once a tuple is created, you cannot add items to it. Tuples are unchangeable.
Example :
- example to show we cannot add items to a tuple:
thistuple = ("apple", "banana", "cherry")
thistuple[3] = "orange" # This will raise an error
print(thistuple)
#output:
Traceback (most recent call last):
File "demo_tuple_add.py", line 2, in <module>
thistuple[3] = "orange" # This will raise an error
TypeError: 'tuple' object does not support item assignment
- Tuples are unchangeable, so you cannot remove items from it, but you can delete the tuple completely:
- The del keyword can delete the tuple completely:
Example :
- Removing the tuplecompletely :
thistuple = ("apple", "banana", "cherry")
del thistuple
print(thistuple) #this will raise an error because the tuple no longer exists
#output:
Traceback (most recent call last):
File "demo_tuple_del.py", line 3, in <module>
print(thistuple) #this will raise an error because the tuple no longer exists
NameError: name 'thistuple' is not defined
1.6 The tuple() Constructor
- It is also possible to use the tuple() constructor to make a tuple.
- Using the tuple() method to make a tuple constructor
Example :
- constructor in tuple :
thistuple = tuple(("apple", "banana", "cherry")) # note the double round-brackets
print(thistuple)
1.7 Tuple methods
- Python has two built-in methods that you can use on tuples.
| Method |
Desscription |
| count() |
Returns the number of times a specified value occurs in a tuple |
| index() |
Searches the tuple for a specified value and returns the position of where it was found |