Tuple in Python:
|We can use the PyCharm code editor for this example. If you do not know about it then follow this link- How to install PyCharm for Python and create a program in it.|
(1) A tuple in python is an ordered sequence of elements of different data types, such as integer, float, string, list, or even a tuple. It is a linear data structure which means that the elements are stored in a linear order one after another. Elements of a tuple are enclosed in parentheses (round brackets) and are separated by commas. Like list and string, elements of a tuple can be accessed using index values, starting from 0. A tuple is an immutable data type which means a tuple once created cannot be altered. An attempt to do this would lead to an error. In Python, a tuple is used to handle a large amount of heterogeneous data, without the need to declare many individual variables of different types.
(2) Example of Tuple in Python:
- Create Tuple in Python as shown below.
- Access tuple particular value as shown below.
- Using a for loop we can iterate through each item in a tuple.
(3) Tuple Function:
- min()– Returns the minimum or smallest element of the tuple.
- max()– Returns the maximum or largest element of the tuple.
- count()– Returns the number of times the given element appears in the tuple.
- index()– Returns the index of the first occurrence of the element in the given tuple.
- sum()– Returns the sum of the elements of the tuple.