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. Let us learn about initialising and accessing elements in tuple.
Initialising and Accessing Elements in Tuple
It is not necessary to initialise an empty tuple. Furthermore, we can directly insert elements into the tuple. Moreover, we can access elements in a tuple just like we do in lists and strings that is, by using the indexing method. Let us learn about initialising and accessing elements in tuple in detail through various examples.
Initialising the Tuples
We can initialise a tuple in many ways. We can either initialise a tuple empty or with the elements. There are basically two ways to initialise empty tuples. We can do so either by writing just empty parenthesis () or by using the tuple() constructor. They are as follows:
Example 1: Initialising empty tuple using empty parenthesis()
>>>emptytup1 = () >>>emptytup1 ()
Example 2: Initialising empty tuple using tuple() constructor
>>>emptytup2 = tuple() >>>emptytup2 ()
Besides, we can also create tuples directly with the elements inside them. Furthermore, it is not necessary to initialise a tuple before inserting the elements. There are several ways to initialise or create a tuple. Let us have a look at them.
Initialising Tuples with Elements
We can create or initialise a tuple in various ways by using different types of elements. Since a tuple can contain all elements of the same data type as well as of mixed data types as well. Therefore, we have multiple ways of creating tuples. Let us look at few examples of creating tuples in python.
Example 1: Tuple with integers as elements >>>tup = (22, 33, 5, 23) >>>tup (22, 33, 5, 23) As you can see here, we have only integer type of elements in the tuple. Example 2: Tuple with mixed data type >>>tup2 = ('hi', 11, 45.7) >>>tup2 ('hi', 11, 45.7) As you can see here, we have 'string', 'integer', and 'float' data type elements all in the same tuple tup2. Besides, we can use the tuple() constructor also to create a tuple. We can do this as follows: >>>tup = tuple ((22, 45, 23, 78, 6.89)) >>>tup (22, 45, 23, 78, 6.89)
Browse more Topics Under Tuples and its Functions
Accessing Elements in a Tuple
We can access elements in a tuple in the same way as we do in lists and strings. Hence, we can access elements simply by indexing and slicing. 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
Therefore, in this way, we can perform the function of initialising and accessing elements in tuple. In further topics, we will study in more detail indexing and slicing. That will further help us to understand the concept of initialising and accessing elements in tuple.
Frequently Asked Questions (FAQs)
Q1. State true or false:
We cannot initialise an empty tuple.
A1. False, we can initialise the tuple as empty i.e., with no element inside.
Q2. How do we perform accessing of elements in a tuple?
d) both a and b
A2. d) both a and b
Q3. Which error is displayed if we try to give an index that is not available in a tuple?
A3. If the index is not present python gives the IndexError.
IndexError: tuple index out of range
Q4. >>> tup [4-1]
We can access an element by giving an index as an expression as shown above. State true or false.
A4. True, we can give an expression inside square brackets if it results in an integer that is valid for the tuple index.