Tuples are basically a data type in python. These tuples are an ordered collection of elements of different data types. Furthermore, we represent them by writing the elements inside the parenthesis separated by commas. We can also define tuples as lists that we cannot change. Therefore, we can call them immutable tuples. Moreover, we access elements by using the index starting from zero. We can create a tuple in various ways. Moreover, we can extract elements from the tuple in various ways. This process is slicing, as we do in string and list. Let us study tuple slicing.
Before learning about tuple slicing we should know that to access any element in a tuple we use indexing. Furthermore, the indexing is simple as in lists, starting from the index zero. Let us look at a few examples of accessing the elements in a tuple.
>>>tup1 = (10, 3, 4, 22, 1) # for accessing the first element of the tuple >>>tup1 10 # accessing the third element of the tuple >>>tup1 4 >>>tup1 # gives error as the index is only up to 4 IndexError: tuple index out of range >>>tup1[1+3] # the expression inside the square brackets results in an integer index 4. Hence, we get the element at the 4th index. 1
We can use slicing in tuples I’m the same way as we use in strings and lists. Tuple slicing is basically used to obtain a range of items. Furthermore, we perform tuple slicing using the slicing operator. We can represent the slicing operator in the syntax [start:stop:step]. Moreover, it is not necessary to mention the ‘step’ part. The compiler considers it 1 by default if we do not mention the step part. Let us look at the examples to understand tuple slicing in detail.
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1) >>> print(tup[1:4]) # prints 2nd to 4th element (3, 45, 4)
Example 2: If we don’t mention the start value the range by default starts from the first term.
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1) >>> print(tup[:4]) # prints 1st to 4th element (22, 3, 45, 4)
Example 3: If we don’t mention the stop value the range by default ends at the last term.
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1) >>> print(tup[4:]) # prints 5th to the last element (2.4, 2, 56, 890, 1)
Example 4: If we don’t mention both the start and stop value the range by default starts from the first term and ends at the last term.
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1) >>> print(tup[:]) # prints first to the last element (22, 3, 45, 4, 2.4, 2, 56, 890, 1)
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1) >>> print(tup[::2]) # prints first to the last element with a step of 2 i.e. printing the alternate elements (22, 45, 2.4, 56, 1)
Browse more Topics Under Tuples and its Functions
- Immutable Tuples
- Creating Tuples
- Initialising and Accessing Elements in a Tuple
- Tuple Assignment
- Tuple Indexing
- Tuple Functions
Example 6: You can use negative indexes also if you want to print the elements from the end.
>>> tup = (22, 3, 45, 4, 2.4, 2, 56, 890, 1) >>> print(tup[-4]) # prints the fourth element from the last 2 Now, we can use negative indexes in slicing also. >>> print(tup[-4:-1]) # prints elements from last fourth to last first (2, 56, 890)
Frequently Asked Questions (FAQs)
Q1. Where does the tuple index start from?
A1. The tuple index starts from0.
Q2. Which operator is used for printing a range of elements in a tuple?
A2 We make use of the slicing operator [start:stop:step] for doing tuple slicing.
Q3. State true or false:
It is necessary to mention the start and stop index in the slicing operator.
Q4. What does a negative index represent?
A4. A negative index represents the elements starting in a backward manner.