Tuple is a data structure in Python. It is another sequence data type that is similar to the list. A tuple consists of a number of values separated by commas. Unlike lists, however, tuples are enclosed within parentheses.
The main difference between lists and tuples are : Lists are enclosed within brackets ( [ ] ), and their elements and size can be changed, while tuples are enclosed within parentheses ( ( ) ) and cannot be updated. Tuples can be thought of as read-only lists.
Example:-
tuple1 = (10, 20, 30, 40, 50, 60)
for i in tuple1:
print(i)
Output:-
10 20 30 40 50 60
Exercises:-
- Write a py program to find the length of tuple
- Write a py program to find the maximum element in tuple
- Write a py program to find the minimum element in tuple
- Write a py program to delete tuple
Why Tuple:-
- Tuples use less memory whereas lists use more memory.
- We can use tuples in a dictionary as a key but it's not possible with lists.
- The tuple is immutable.
Comments
Post a Comment