- It is a collection of data of different data types.
- We can not change the value of tuples.
- It is used to store tuple of values.
- A tuple is created using parentheses.
str_tuple=("Apple","Orange","Mango") int_tuple=(15,25,36,84,59) float_tuple=(2.3,5.6,1.4,9.6) mixed_tuple=("Easy",205,25.3)
- Value of tuple can be accessed using index number.
- Index number is always an integer value and starts with 0.
fruit_tuple=("Apple","Orange","Mango")
print("I like ",fruit_tuple[0])
print("I like ",fruit_tuple[2])
"""
***Output***
I like Apple
I like Mango
"""
int_tuple=(5,10,15,20,25,30,35,40,45,50)
#Print will start at index 1 (included) and end at index 4 (not included).
print(int_tuple[1:4])
"""
***Output***
(10, 15, 20)
"""
- Negative indexes start from the end of the tuple.
- Negative index always starts with -1.
- For example fruit_tuple=("Apple","Orange","Mango") here index of Mango ,Orange and Apple are -1,-2 and -3.
fruit_tuple=("Apple","Orange","Mango")
print(fruit_tuple[-3])#Apple
print(fruit_tuple[-2])#Orange
print(fruit_tuple[-1])#Mango
"""
***Output***
Apple
Orange
Mango
"""
fruit_tuple=("Apple","Orange","Mango")
for name in fruit_tuple:
print("I like ",name)
"""
***Output***
I like Apple
I like Orange
I like Mango
"""
- We can not change the value of tuple.
fruit_tuple=("Apple","Orange","Mango")
#this line will generate error
#because we can't change the value of tuple
fruit_tuple[1]="Banana"
- We can update the value of tuple using list.
fruit_tuple=("Apple","Orange","Mango")
print("Tuple Before Updation:",fruit_tuple)
#Convert tuple into list
fruit_list=list(fruit_tuple)
#Update Orange with Banana
fruit_list[1]="Banana"
#Convert list into tuple
fruit_tuple=tuple(fruit_list)
print("Tuple after Updation:",fruit_tuple)
"""
***Output***
Tuple Before Updation: ('Apple', 'Orange', 'Mango')
Tuple after Updation: ('Apple', 'Banana', 'Mango')
"""
- len() function is used to get length of tuple.
fruit_tuple=("Apple","Orange","Mango")
print("Length of tuple is ",len(fruit_tuple))
"""
***Output***
Length of tuple is 3
"""
- We can't add new item to tuple once a tuple is created.
- Tuples are unchangeable.
fruit_tuple=("Apple","Orange","Mango")
#this line will generate an error
fruit_tuple.append("Banana")
- We can't delete item from tuple because it is unchangeable.
- But we can delete a tuple completely using del keyword.
fruit_tuple=("Apple","Orange","Mango")
print("Fruit tuple:",fruit_tuple)
del fruit_tuple;
print("Deleted successfully")
#this line will generate error
print(fruit_tuple)
"""
***Output***
Fruit tuple: ('Apple', 'Orange', 'Mango')
Deleted successfully
NameError: name 'fruit_tuple' is not defined
"""
- We can join two tuple using plus(+) operator.
tuple1=("Apple","Orange","Mango")
tuple2=("Cherry","Grapes","Melon")
#this line will join tuple1 and tuple2
tuple3=tuple1+tuple2
print("tuple3 items")
print(tuple3)
"""
***Output***
tuple3 items
('Apple', 'Orange', 'Mango', 'Cherry', 'Grapes', 'Melon')
"""
fruit_tuple = ("Apple", "Orange", "Mango")
str=input("Enter any string to search:")
if str in fruit_tuple:
print(str," is found")
else:
print("Not found")
"""
***Output***
Enter any string to search:Orange
Orange is found
"""
Request:-If you found this post helpful then let me know by your comment and share it with your friend.
If you want to ask a question or want to suggest then type your question or suggestion in comment box so that we could do something new for you all.
If you have not subscribed my website then please subscribe my website.
Try to learn something new and teach something new to other. Thanks.
0 Comments