Tuples in Python
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. Hence, tuples are not modifiable in nature. Moreover, we access elements by using the index starting from zero. We can create a tuple in various ways. Let us learn about creating tuples in python.
Creating Tuples
We can create 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.
Creating tuples with a Single Data Type
We can create a tuple that contains only a single type of element.
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.
Creating Tuples with Mixed Data Type
We can create a tuple that contains various types of elements.
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.
Creating Tuple with a List as an Element
We can create a tuple that can have a list as an element of a tuple.
Example 3: Tuple with a list as an element
>>>tup3 = (55, [6, 9], 67)
>>>tup3
(55, [6, 9], 67)
Browse more Topics Under Tuples and its Functions
- Immutable Tuples
- Initialising and Accessing Elements in a Tuple
- Tuple Assignment
- Tuple Slicing
- Tuple Indexing
- Tuple Functions
Creating Tuple with a Tuple as an Element
We can create a tuple that can have a tuple itself as an element of a tuple.
Example 4: Tuple with a tuple as an element
>>>tup3 = (55, (6, 'hi'), 67)
>>>tup3
(55, (6, 'hi'), 67)
Creating Tuple with a Single Element Only
If there is only a single element in a tuple we should end it with a comma. Since writing, just the element inside the parenthesis will be considered as an integer.
For example,
>>>tup=(90)
>>>tup
90
>>>type(tup)
<class 'int'>
Correct way of defining a tuple with single element is as follows:
>>>tup=(90,)
>>>tup
(90,)
>>>type(tup)
<class 'tuple'>
Creating Tuple with Duplicate Elements
A tuple allows to include duplicate elements in it.
For example,
>>>tup= (22, 34, 45, 88, 34, 9, 45)
>>>tup
(22, 34, 45, 88, 34, 9, 45)
Creating Tuple by Using the Tuple() Construtor
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)
Tuples are Immutable in Nature
Since we know that tuples are immutable in nature. This means that once we create a tuple we cannot make changes to it. Moreover, if we try to do so it will result in an error.
For example,
>>>tuple1 = (1, 2, 33, 44, 6)
>>>tuple1[4] = 10
TypeError: 'tuple' object does not support item assignment
Frequently Asked Questions (FAQs)
Q1. Give an example of creating a tuple.
A1. We can create a tuple as follows:
>>>tup = ('hi', 44, 5.7, 8, 90)
Q2. State true or false:
We can add elements to a tuple after creating it.
A2. False, we cannot add elements after creating a tuple because tuples are immutable in nature.
Q3. What is the data type of a tuple?
A3. In python, the tuples are recognized as data type ‘tuple’.
>>>tup=(90, 6, 7.9, 88)
>>>tup
(90, 6, 7.9, 88)
>>>type(tup)
<class 'tuple'>
Q4. Which constructor is used to create tuples?
A4. We can use the ‘tuple()’ constructor for creating a tuple.
Leave a Reply