
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Define Tuple in Python
A tuple is a collection of python objects that are separated by commas which are ordered and immutable. Tuples are sequences, just like lists. The differences between tuples and lists are, that tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. Let us see about the creation of tuples in a detailed way.
We can define tuple in different ways as follows -
Empty Tuple
As name suggest empty tuple is a tuple with no elements. It can be created using empty parentheses.
Example
Following is a way, to can create an empty tuple.
temp=() print(temp)
Output
The following output is obtained on executing the above program ?
()
Non Empty Tuple
It is a tuple with elements that are separated by commas. In the following code, to give a string value you need to declare it in quotes whereas for a Boolean value you can directly declare it as True or False.
Example
The following is a small python example which shows the creation of a non-empty tuple.
tup=('tutorials', 'point', 2022,True) print(tup)
Output
The following output is obtained on executing the above program ?
('tutorials', 'point', 2022, True)
Create a Tuple with a Single Element
All the components (elements) must be enclosed in parentheses (), each one separated by a comma, to form a tuple. Although using parentheses is not required, it is a good practice to do so. Any number of objects, maybe of various types such as integer, float, list, string, etc., may be included in a tuple.
Example
The following is a small python code snippet which shows the creation of a tuple with a single element.
tup=('tutorialspoint',) print(tup)
Output
The following output is obtained on executing the above program ?
('tutorialspoint',)
Tuple with Mixed Datatypes
In Python, each value has a datatype. In Python programming, everything is an object, hence, variables and data types are both instances (or objects) of the same classes. Few built in datatypes in Python are Numeric datatype (such as int, float etc), boolean datatype, dictionary etc.
Example
Following is an example of tuple having mixed datatypes -
# tuple with datatypes having integers, string and float tuple = (8, "TutorialsPoint", 7.8) print('The tuple with mixed datatype is:',tuple)
Output
Following is an output of the above code ?
The tuple with mixed datatype is: (8, 'TutorialsPoint', 7.8)
Nested Tuple
Lists, dictionaries, and other compound objects, as well as other tuples, can all be contained within a tuple. Tuples are hence capable of nesting inside other tuples. Each tuple in the nested tuple is considered to be an element.
Example 1
In the following example, a for loop is used for accessing all the elements in the nested tuple -
Student = (('Rohit','X-B',87.4), ('Sakshi', 'X-C', 76.9), ('Shweta', 'X-D', '98.7')) for h in Student: print(h)
Output
The following is an output of the nested tuple -
('Rohit', 'X-B', 87.4) ('Sakshi', 'X-C', 76.9) ('Shweta', 'X-D', '98.7')
Example 2
Following is another example of a nested tuple -
# nested tuple tuple = ("TutorialsPoint", [2,6,4], (9,4,7)) print('The nested tuple is:',tuple)
Output
Following is the output of the above code -
The nested tuple is: ('TutorialsPoint', [2, 6, 4], (9, 4, 7))