What is syntax of tuple declaration in Python?

PythonServer Side ProgrammingProgramming

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

# tuple consisting of strings demoTuple_1 = ("TutorialsPoint", "python", "codes") print("demoTuple_1:", demoTuple_1)

Output

On executing, the above program will generate the following output −

demoTuple_1: ('TutorialsPoint', 'python', 'codes')

Example 2

# tuple consisting of integer, float number, string demoTuple_2 = (25, 6.8, "TutorialsPoint") print("demoTuple_2:", demoTuple_2)

Output

On executing, the above program will generate the following output −

demoTuple_2: (25, 6.8, 'TutorialsPoint')

Example 3

# tuple consisting of string & list demoTuple_3 = ("Welcome", [5, 8, 11]) print("demoTuple_3:", demoTuple_3)

Output

demoTuple_3: ('Welcome', [5, 8, 11])
.

Example 4

# nested tuple(one tuple inside another tuple) demoTuple_4 = ((100, 80, 60), (2, "TutorialsPoint")) print("demoTuple_4:", demoTuple_4)

Output

demoTuple_4: ((100, 80, 60), (2, 'TutorialsPoint'))

Accessing Tuple Elements

To access the elements in a tuple, we need indexes

We can also have negative indices in tuples. Since indexes begin with 0, we use 0 to access the first element of a tuple, 1 to access the second member, and so on.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input tuple.

  • Print the input tuple.

  • Use the positive indexing, to access the first element of the input tuple(tuple index always starts from 0).

  • Use the positive indexing, to access the fourth element of the input tuple(inputTuple[3]).

Code

# 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])

Output

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

-1 to access the last element, -2 to access the second last element, and so on.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input tuple.

  • Print the input tuple.

  • Use the negative indexing, to access the last element of the input tuple(negative tuple index always starts from -1).

  • Use the negative indexing, to access the last second element of the input tuple(inputTuple[-1]).

Code

# 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])

Output

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 immutabl −

# 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'

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]

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

Conclusion

This article demonstrated how to create tuples using four different examples. We also learned how to use indexing to access the tuple elements. We learned how to use negative indexing to access the tuple elements from the end. We demonstrated how the tuple throws errors when updating or removing elements.

raja
Updated on 08-Nov-2022 11:52:44

Advertisements