In this article, we will discuss on Python tuple. The following are the contents of this article:
- Introduction
- Access item
- Negative Indexing
- Range of indexing
- Range of negative indexing
- Tuple length
- Join tuples
- Remove Item
- Loop through tuple
- Checking
- Change Value
- Constructor
- Tuple method
Introduction
To store a collection of items we use tuple in Python. They are immutable because once they are created can’t be altered. They are represented within round brackets “( )”.
Access Tuple Items
By referring the index item within the square brackets we access items of a tuple.
tuple1 = ("mango", "apple", "banana")
print(tuple1[1]) #returns apple
Negative Indexing
Negative indexing refers to the access of items from the end of the tuple.
tuple1 = ("mango", "apple", "banana")
print(tuple1[-1]) #returns banana
Range of Indexes
In Python, we can access a particular number of items present in the tuple by using the range of indexes. The first index and the last index of the range is separated by a colon “: “.
tuple1 = ("mango", "apple", "banana", "guava", "grapes")
print(tuple1[1:4]) #returns ('apple', 'banana', 'guava')
Range of Negative
Specify negative indexes if you want to start the search from the end of the tuple:
tuple1 = ("mango", "apple", "banana", "guava", "grapes", "cucumber", "lichi")
print(tuple1[-4:-1]) #returns ('guava', 'grapes', 'cucumber')
Tuple Length
In order to determine the tuple length, we use the “len()” method.
tuple1 = ("mango", "apple", "banana", "guava", "grapes")
print(len(tuple)) #returns 5
Now there’s a question that can we create a tuple of a single item?
The answer is – Of course!
By using a comma “, “ after the single item we can create a tuple. If we do not use the comma Python will treat it as a string type data. To know more about this you can refer to this site.
tuple1 = ("mango",)
print(type(tuple1)) #returns <class 'tuple'>
tuple1 = ("mango")
print(type(tuple1)) #returns <class 'str'>
Join Two Tuples
By using the ” + ” character we can join two tuples.
tuple1 = ("mango", "apple", "banana", "guava", "grapes")
tuple2 = ("rose", "sunflower", "jui", "jasmine", "tulip")
tuple3= tuple1 + tuple2
print(tuple3)
Remove Items
As tuples are immutable so we can’t remove any item from the tuple but we can delete the entire tuple.
tuple1 = ("mango", "apple", "banana", "guava", "grapes")
del tuple
print(tuple) #this will raise an error because the tuple no longer exists
Loop Through a Tuple
You can use loop through tuple by using for() loop
tuple1 = ("mango", "apple", "banana", "guava", "grapes")
for i in tuple1:
print(i)
The following is the output of the above statements.
Check if Item Exists
We use ” in ” if an item is present in a tuple or not.
tuple = ("apple", "banana", "mango")
if "apple" in tuple:
print("Yes, 'apple' is in the fruits tuple")
Change Tuple Values
We have already know that tuples are immutable. So we can’t change the value directly. What we can do is that we can simply convert the tuple into a list and then only can change the value.
tuple1 = ("apple", "banana", "grapes")
x = list(tuple1)
x[1] = "orange"
tuple1 = tuple(x)
print(x) # returns ['apple', 'orange', 'grapes']
The tuple() Constructor
We use “tuple()” constructor in Python to use tuple
tuple1 = tuple(("apple", "banana", "orange")) # note the double round-brackets
print(tuple1) #returns ('apple', 'banana', 'orange')
Tuple Methods
Python has two built-in methods that you can use on tuples.
- 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