Python Tuples

Python Tuples: In Python, a tuple is a collection which is ordered and unchangeable. It is another sequence data type which is similar to list. A tuple consists of no.of values separated by commas. The main difference between Python tuples and list are, lists are enclosed in brackets ([]) and their elements and sizes can be changed. While tuples are enclosed in parentheses (()) and cannot be updated. Tuples can be thought of as read-only lists.



Example

thistuple=("mango","grape","lemon")
print(thistuple)

Output: (‘mango’, ‘grape’,’lemon’)

Python Tuples

Access the Python tuple Items

You can access the python tuple items by referring to the index number. And the indexing value must be an integer. So that we cannot change into other values. It will give us TypeError.

Example

thistuple=("mango", "grape", "lemon") 
print(thistuple[1])

Output: grape

Change tuple values

Once python tuple is changed, you cannot change its values. Unlike the lists, lists are immutable.

Example

thistuple=("mango","grape","lemon")
thistuple[1]="papaya"#It will remains the same 
print(thistuple)

Output: (‘mango’, ‘grape’,’lemon’)

Slicing of tuples

We can access a range of elements by using the slicing operator (:). Slicing can be best visualized by considering the index to be between the elements as shown below. So if we want to access a range, we need the index that will slice the portion from the tuple.

Example

my_tuple = ('f','r','e','s','h','e','r','s','n','o','w')
# elements 2nd to 4th
# Output: ('e', 's', 'h')
print(my_tuple[1:4])
# elements beginning to 2nd
# Output: ('f', 'r')
print(my_tuple[:-7])
# elements 8th to end
# Output: ('s','n','o', 'w')
print(my_tuple[7:])
# elements beginning to end
print(my_tuple[:])

Output
(‘r’, ‘e’, ‘s’)
(‘f’, ‘r’, ‘e’, ‘s’)
(‘s’, ‘n’, ‘o’, ‘w’)
(‘f’, ‘r’, ‘e’, ‘s’, ‘h’, ‘e’, ‘r’, ‘s’, ‘n’, ‘o’, ‘w’)

Loop through a tuple

You can loop through then tuple items by using for loop

Example

tuple=('aecd',584,2.23,'freshersnow',60.2)
tinytuple = (123,'merry') 
print tuple #Prints complete list
print tuple[0] # Prints first element of the list
print tuple[1:3] #Prints elements starting from 2nd till 3rd  
print tuple[2:] #Prints elements starting from the 3rd element
print tinytuple*2 #Prints list two times 
print tuple+tinytuple #Prints concatenated lists

Output
(‘aecd’, 584,2.23,’freshersnow’,60.2) aecd (584,2.23)
(2.20,’freshersnow’,60.2)
(123,’freshersnow’,123,’freshersnow’)
(‘aecd’, 584, 2.23,’freshersnow’,60.2, 123,merry)

check if the item exists

Example

thistuple=("van","car","truck")
if "car" in thistuple: Print ("Yes,'car' is in the vehicles tuple")

Output: Yes, ‘car’ is in the vehicle’s tuple

Python Tuple Length

Example

thistuple=("van","car","truck")
print(len(thistuple))

Output: 3

Python Add items

Once a Python tuple is created, you cannot add items to it. In PythonTuples are unchangeable.

Example

thistuple=("van","car","truck")
thistuple[3]="bus" #This will raise an error
print(thistuple)

Output: ‘tuple’ object does not support the item assignment

Remove Items

Python tuple is unchangeable. So you cannot remove items from it, but you can delete the tuple completely.

Example

thistuple=("van","car","truck")
del thistuple print(thistuple)#this will raise an error because the tuple no longer exists

Output: Name Error: name ‘thistuple’ is not defined

Python tuple constructor

For making python tuple ( ) we can use tuple constructor.

Example

thistuple=tuple(("van","car","truck"))# note the double round-brackets 
print(thistuple)

Output: (‘van’,’car’,’truck’)

Python Tuple Methods

In Python tuple methods, in the context of object-oriented programming, is a procedure or a function in the class. As a part of the class, a method defines the behavior of class instance. A class can have more than one method.

Python has two built-in methods that can be used in tuples.

Method Description
count( ) Returns the no.of times a specified value occurs in the tuple
Index( ) Searches the tuple for a specified value and returns the position where it is found.

Example

my_tuple = ('a','p','p','l','e',)
print(my_tuple.count('p'))  # Output: 2
print(my_tuple.index('l'))  # Output: 3

Output
2
3

Tuple membership test

We can test if an item exists in the tuple (or) not by using the keyword in.
Example

my_tuple = ('m','a','n','g','o',)
#In operation
#Output: True
print('a' in my_tuple)
#Output: False
print('b' in my_tuple)
#Not in operation
#Output: True
print('g' not in my_tuple)

Output
True
False
False