Tuple in Python

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.
Create Tuple in Python
  • Access tuple particular value as shown below.
Access tuple particular value in python
  • Using a for loop we can iterate through each item in a tuple.

First Method:

iterate python tuple

Second Method:

iterate tuple in python another method

(3) Tuple Function:

  • min() Returns the minimum or smallest element of the tuple.
python tuple min function
  • max() Returns the maximum or largest element of the tuple.
python tuple max function
  • count() Returns the number of times the given element appears in the tuple.
python tuple count function
  • index() Returns the index of the first occurrence of the element in the given tuple.
python tuple index function
  • sum() Returns the sum of the elements of the tuple.
python tuple sum function

Tutorial Python
Tutorial MySQL
Wireless Networking
Wired Networking
Information Security in E-Commerce
Cloud Computing
FTP (File Transfer Protocol)
Python (programming language)– Wikipedia

Comments (No)

Leave a Reply