
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How do we 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.
Empty Tuple
Empty tuple means a tuple with no elements.
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 code snippet 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 parenthesis (), each one separated by a comma, to form a tuple. Although using parenthesis is not required, it is a good practise 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 of 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 nested tuple −
# nested tuple tuple = ("TutorialsPoint", [2,6,4], (9,4,7)) print('The nested tuple is:',tuple)
Output
The nested tuple is: ('TutorialsPoint', [2, 6, 4], (9, 4, 7))
- Related Articles
- How do we define lists in Python?
- How do we define dictionary in Python?
- How do we grep a particular keyword from Python tuple?
- How do we define drift speed?
- How we can use Python Tuple within a Tuple?
- How can I define duplicate items in a Python tuple?
- How do we define a dialog box in HTML?
- How can I do Python Tuple Slicing?
- How we can update a Python tuple element value?
- How do we define an area in an image map with HTML?
- How do we use function literal to define a function in JavaScript?
- How do we declare variable in Python?
- How do we provide comments in Python?
- How do we use enum keyword to define a variable type in C#?
- How can we define a Python function at runtime?
