How to create an empty tuple in Python?


Tuple is one of the data structures of the Python programming language. It is used to store multiple values separated by commas in an ordered manner.

It is immutable in the sense that once the tuple is created cannot perform any operations like deleting, appending etc. The elements in the tuple can be int, float, string, binary data types and it allows duplicates of the elements. It uses indexing for accessing the elements. It allows a single element to be in tuple.

Indexing is the concept used for accessing the elements from particular data structures. There are two ways of performing indexing as positive indexing and negative indexing. In positive indexing the index value of the starting element will be 0 and the end element will be length of the tuple where as in negative indexing the index value of the starting element will be -length of tuple and end element will be -1. 

There are different ways for creating the empty tuple. Let’s see each way in detail.

  • braces()

  • tuple keyword

Using Braces() to create tuple

The empty tuple can be created using the braces().

Syntax

The following is the syntax.

variable_name = ()

Where,

  • varibale_name is the name of the tuple

  • () is the symbol for creating the empty tuple

Example

As you can observe in the following example, when we assign the braces "()" to a variable an empty tuple will be created.

tuple1 = ()
print("The empty tuple created using brackets: ",tuple1)
print(type(tuple1))

Output

The empty tuple created using brackets:  ()
<class 'tuple'>

Example

In this example we have created an empty tuple using the braces. Since the tuple in Python is immutable, when we try to append values to it, it will generate an error.

tuple1 = ()
print("The empty tuple created using brackets: ",tuple1)
tuple1[0] = 10
print(type(tuple1))

Output

('The empty tuple created using brackets: ', ())
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    tuple1[0] = 10
TypeError: 'tuple' object does not support item assignment

Using the tuple() function

The empty tuple can be created using the tuple() function available in Python.

Syntax

The syntax is as follows.

variable_name = tuple()

Where,

  • varibale_name is the name of the tuple

  • tuple is the keyword for creating the empty tuple

Example

Here, when we assign the function tuple() to a variable an empty tuple will be created.

tuple1 = tuple()
print("The empty tuple created using brackets, ",tuple1)
print(type(tuple1))

Output

The empty tuple created using brackets,  ()
<class 'tuple'>

Example

In this example, we created the tuple using the tuple keyword function and when we try to append an element, it will raise an error as the tuple is immutable.

tuple1 = tuple()
print("The empty tuple created using brackets, ",tuple1)
tuple1[0] = "Niharika"
print(type(tuple1))

Output

The following is the output of the empty tuple created using the tuple keyword.

('The empty tuple created using brackets, ', ())
Traceback (most recent call last):
  File "main.py", line 3, in <module>
    tuple1[0] = "Niharika"
TypeError: 'tuple' object does not support item assignment

Updated on: 09-Sep-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements