
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
Syntax of Tuple Declaration in Python
Python provides a variety of data structure collections such as lists, tuples, sets, and dictionaries. However, these tuples are very similar to lists. Because lists are a commonly used data structure, developers frequently mistake how tuples differ from lists.
Python tuples, like lists, are a collection of items of any data type, but tuples are immutable, which means that once assigned, we cannot change the elements of the tuple or the tuple itself, whereas list elements are mutable
In this article, we will explain to you what is a tuple in python and the various operations on it.
Creating a Tuple
Tuples can only be created when they are assigned, so inserting all of the items inside the parenthesis, separated by a comma, will result in the creation of a tuple.
Example 1
In this example we are creating a tuple with three string values. All the values are inside round brackets and they are separated by commas.
# tuple consisting of strings demoTuple_1 = ("TutorialsPoint", "python", "codes") print("demoTuple_1:", demoTuple_1)
On executing the above program will generate the following output -
demoTuple_1: ('TutorialsPoint', 'python', 'codes')
Example 2
Here the tuple has different types of values like an integer, a float number and a string. A tuple may have different types of data.
# tuple consisting of integer, float number, string demoTuple_2 = (25, 6.8, "TutorialsPoint") print("demoTuple_2:", demoTuple_2)
When you run the program, it will show this output -
demoTuple_2: (25, 6.8, 'TutorialsPoint')
Example 3
In this example the tuple has a string and a list inside it. So we can say that a tuple can also save a list as one of its items.
# tuple consisting of string & list demoTuple_3 = ("Welcome", [5, 8, 11]) print("demoTuple_3:", demoTuple_3)
After running the program, you will get this result -
demoTuple_3: ('Welcome', [5, 8, 11]).
Example 4
Here is an example of a nested tuple. Means we can store one tuple inside another tuple.
# nested tuple(one tuple inside another tuple) demoTuple_4 = ((100, 80, 60), (2, "TutorialsPoint")) print("demoTuple_4:", demoTuple_4)
This output will be displayed when the program runs -
demoTuple_4: ((100, 80, 60), (2, 'TutorialsPoint'))
Accessing Tuple Elements
To access the elements in a tuple, we need indices. We can also have negative indices in tuples. Since indices begin with 0, we use 0 to access the first element of a tuple, 1 to access the second element, and so on.
# tuple consisting of strings inputTuple = ("TutorialsPoint", "python", "sample", "codes") # printing tuple print("Input Tuple: ", inputTuple) # accessing the first element of the tuple print("first element of tuple: ", inputTuple[0]) # accessing the fourth element of the tuple print("fourth element of tuple: ", inputTuple[3])
You will see this result after executing the program -
Input Tuple: ('TutorialsPoint', 'python', 'sample', 'codes') first element of tuple: TutorialsPoint fourth element of tuple: codes
Accessing Tuple Elements Using Negative Indexing
Negative indexes, like list and string indexes, can be used to access tuple elements from the end. We can use -1 to access the last element, -2 to access the second last element, and so on.
# tuple consisting of strings inputTuple = ("TutorialsPoint", "python", "sample", "codes") # printing tuple print("Input Tuple: ", inputTuple) # accessing the last element of the tuple print("last element of tuple: ", inputTuple[-1]) # accessing the last second element of the tuple print("last second element of tuple: ", inputTuple[-2])
The program gives this output when it is run -
Input Tuple: ('TutorialsPoint', 'python', 'sample', 'codes') last element of tuple: codes last second element of tuple: sample
Update/ Delete Tuple Elements
A tuple is immutable, i.e, the items of a tuple cannot be changed. As a result, once a tuple is created, any operation that attempts to change its items is restricted. The following program attempts to update/modify the elements of an input tuple, but raises an error since the tuple is immutable -
# tuple consisting of strings inputTuple = ("TutorialsPoint", "python", "sample", "codes") # changing the 1st index element of the input tuple(python) to 'Java' # It raises a TypeError since the tuple is immutable(tuple items cannot be changed) inputTuple[1] = 'Java'
Running the code will produce the following output -
TypeError Traceback (most recent call last) <ipython-input-6-92f3425fb83d> in <module> 4 # changing the 1st index element of the input tuple(python) to 'Java' 5 # It raises a TypeError since the tuple is immutable(tuple items cannot be changed) ----> 6 inputTuple[1] = 'Java' TypeError: 'tuple' object does not support item assignment
The following program attempts to delete the elements of an input tuple, but raises an error since the tuple is immutable -
# tuple consisting of strings inputTuple = ("TutorialsPoint", "python", "sample", "codes") # deleting the 1st index element of the input tuple(python) # It raises a TypeError since the tuple is immutable(tuple items cannot be changed) del inputTuple[1]
When you run the program, it will show this output -
TypeError Traceback (most recent call last) <ipython-input-7-924b49eaac45> in <module> 4 # deleting the 1st index element of the input tuple(python) 5 # It raises a TypeError since the tuple is immutable(tuple items cannot be changed) ----> 6 del inputTuple[1] TypeError: 'tuple' object doesn't support item deletion