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.
Output: (‘mango’, ‘grape’,’lemon’)
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.
thistuple=("mango", "grape", "lemon") print(thistuple)
Change tuple values
Once python tuple is changed, you cannot change its values. Unlike the lists, lists are immutable.
thistuple=("mango","grape","lemon") thistuple="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.
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[:])
(‘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
tuple=('aecd',584,2.23,'freshersnow',60.2) tinytuple = (123,'merry') print tuple #Prints complete list print tuple # 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
(‘aecd’, 584,2.23,’freshersnow’,60.2) aecd (584,2.23)
(‘aecd’, 584, 2.23,’freshersnow’,60.2, 123,merry)
check if the item exists
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
Python Add items
Once a Python tuple is created, you cannot add items to it. In PythonTuples are unchangeable.
thistuple=("van","car","truck") thistuple="bus" #This will raise an error print(thistuple)
Output: ‘tuple’ object does not support the item assignment
Python tuple is unchangeable. So you cannot remove items from it, but you can delete the tuple completely.
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.
thistuple=tuple(("van","car","truck"))# note the double round-brackets print(thistuple)
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.
|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.|
my_tuple = ('a','p','p','l','e',) print(my_tuple.count('p')) # Output: 2 print(my_tuple.index('l')) # Output: 3
Tuple membership test
We can test if an item exists in the tuple (or) not by using the keyword in.
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)