Tuples In Python and Built-in-Methods
Hi everyone, in this article, we are going to talk about Python Tuples.
First of all, let’s talk about Python Tuples in general. Tuples are used to store data of multiple types like str, int, float, boolean, etc in a single variable. Lists and tuples have a lot in common and lots of differences as well. First of all, in both data types, the elements are ordered, so the items have a defined order, that will not change. Also, both are dynamic in size data types. So we don’t need to define the size of the tuple in advance, as Python takes care of that. Moreover, in both data types duplicate values are allowed. On the other hand, the big difference between lists and tuples is that a tuple is an immutable data type, so we cannot add, update, or delete the elements of a tuple after its declaration.
When to use Tuple?
A general guide is to use tuples only if you are sure that the data will not change during the execution of the program. For example, if you want to store the days of the week you can store them in a tuple.
To create a tuple we use round brackets, or we can use the constructor tuple()
The following are Built-in-methods of tuple:
1.count()
The method returns the number of times a specified value appears in the Tuple.
2.index()
The method finds the first occurrence of the specified value.
I hope you found this information on Tuple helpful! enjoy working with tuples and Python!
Thanks
#vevcodelab