Python Tuples

Python Tuples

What is Tuple in Python?

Tuples are the sequences of values of different data types. It is

  • Ordered
  • Immutable
  • Allows duplicate values.

Creating tuple in Python

The elements of a tuple are enclosed between circular brackets, (), with each element separated by a comma.

1. Creating an empty Python tuple:

To create an empty tuple we just need to assign the square brackets to the variable as shown below. We can use the type() function to check the data type.

2. Creating Python tuple with single element:

To create a tuple with a single element, we need to add a comma after the element. If we don’t add the comma, it will be assumed as the data type of that element.

3. Creating Python tuple with multiple elements:

We can create a tuple with multiple elements by separating the elements by comma and enclosing them between circular parentheses. We don’t need to add a comma at the end like in the case of a single element.

4. Packing in Python

Packing is a method of creating a tuple without using parentheses. The below example shows a way of creating tuples with single and multiple elements by packing.

Accessing elements of tuples in Python

Since tuples are ordered, we can access the elements of a tuple using indexing. Similar to the lists, here the indexing starts from zero. That is, the first element has an index of 0.

1. Accessing single element in Python Tuple:

To access an element from a list we can write the tuple name followed by the index in square brackets. We can also give negative indexing, where the first element from the right has the index -1. And this reduces as we go to the left.

2. Accessing multiple elements in Python Tuples:

We can again use indexing to access multiple elements. These are the following cases encountered to get more than one element:

a. To get the elements from index i to j, excluding the jth, we give “i:j” indexing.

b. To get all the elements from the index i, we give “i:” indexing.

c. To get all the elements before index i, excluding the ith, we give “:i” indexing.

3. Accessing multiple elements with a step in Python:

To access multiple elements, not continuous, but that occurs after a few positions we give the indexing as “i:j:k”. Where i is the starting index, j is the ending index and k is the step. The values of i, j, and k can be negative.

4. Accessing value of the inner container in Python

We can access the elements of the inner containers of the tuple like lists, tuples, etc. To do so first we access that container using indexing. Then add another index using square brackets to access a particular value.

5. Getting error on wrong indexing in Python

When we give the index as any value other than an integer, we get an error. Also, the index should be less than the length of the tuple and greater than or equal to the negative of the length of the tuple. If the index does not lie in this range, we get an error because there are no elements outside this range.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了