regularpython@gmail.com
Tuples
Tuples are also used to store sequence data and it will be immutable. Immutable means we cannot modify the data. Tuples are also called read only data type. Tuples are uses parentheses to represents the data.
Examples:
Fruits = (“oranges”, “grapes”, “bananas”, “mangoes”) Numbers = (1,2,3,4,5,6) Student = ({“name”:”sai”, “marks”:513}, {“name”:”ram”, “marks”:500}) Movies = ((‘guru’, ‘telugu’), (‘Aaru’, ‘tamil’)) We can also take all data types in a tuple. Data = (“oranges”, 1,2, {“name”:”sai”, “marks”:513}, (‘guru’, ‘telugu’,[1,2,3])
How to access values from tuple?
fruits = ("oranges", "grapes", "bananas", "mangoes")
print(fruits[0])
output: oranges.
Example2:
fruits = ["oranges", "grapes", "bananas", "mangoes"]
print(fruits[1:3])
output: ['grapes', 'bananas']
Example3:
fruits = ["oranges", "grapes", "bananas", "mangoes"]
print(fruits[-1])
output: mangoes
How to modify the data in tuple?
Actually we can’t modify the data in tuples. Please see the below example. When we try to update the fruits second element, it’s throws an TypeError: 'tuple' object does not support item assignment
Example:
fruits = ("oranges", "grapes", "bananas", "mangoes")
print("Before updating", fruits[1])
print(fruits)
print('----------------------------------------')
fruits[1] = 1
print('After updating', fruits[1])
print(fruits)
output:
Before updating grapes
('oranges', 'grapes', 'bananas', 'mangoes')
----------------------------------------
Traceback (most recent call last):
File "E:\Repository\test\test\test.py", line 5, in <module>
fruits[1] = 1
TypeError: 'tuple' object does not support item assignment
How to delete an element from tuple?
When we try to delete an element from tuple then it’s throwing an error like this
TypeError: 'tuple' object doesn't support item deletion.
Example:
fruits = ("oranges", "grapes", "bananas", "mangoes")
print("Before updating", fruits[1])
print(fruits)
print('----------------------------------------')
del fruits[1]
print('After updating', fruits[1])
print(fruits)
output:
Before updating grapes
('oranges', 'grapes', 'bananas', 'mangoes')
----------------------------------------
Traceback (most recent call last):
File "E:\Repository\test\test\test.py", line 5, in <module>
del fruits[1]
TypeError: 'tuple' object doesn't support item deletion
So finally what we learn, we cannot update and delete the data from tuples. So tuples are immutable and read-only data type.
Tuple operations
How to find the length of a tuple?
There is a length function to find the length of a tuple and this function is most important to find data type length.
fruits = ("oranges", "grapes", "bananas", "mangoes")
print("Length of a tuple", len(fruits))
output:
Length of a tuple 4
How to concatenate tuples in python?
To concatenate tuples in python, we can use + operator.
fruits = ("oranges", "grapes", "bananas", "mangoes")
Vegetables = ("tomato", "onions", "carrots", "potato")
print(fruits + Vegetables)
output:
('oranges', 'grapes', 'bananas', 'mangoes', 'tomato', 'onions', 'carrots', 'potato')
How to repeat tuple in twice or more?
fruits = ("oranges", "grapes", "bananas", "mangoes") * 2
print(fruits)
output:
('oranges', 'grapes', 'bananas', 'mangoes', 'oranges', 'grapes', 'bananas', 'mangoes')
What is the search operator in tuples?
'In' is a search operator in a tuple. If you want to search an element in a tuple you can use 'in operator'.
fruits = ("oranges", "grapes", "bananas", "mangoes")
if "grapes" in fruits:
print('Fruit found')
else:
print("Fruit not found")
output:
Fruit found
How to print tuple elements by using loops?
Using for loop:
fruits = ("oranges", "grapes", "bananas", "mangoes")
for fruit in fruits:
print(fruit)
output:
oranges
grapes
bananas
mangoes
Built-in functions
There are no many pre-defined functions for the tuple. Because tuple is an immutable object.
fruits.count(obj): This function is used to count how many times an element exists in a tuple.
fruits.index(obj): This function is used to find the index value of an element in a tuple.