
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
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, or 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 a tuple.
There are different ways to create the empty tuple. Let's see each way in detail.
-
braces()
-
tuple keyword
Creating an Empty Tuple using braces()
The empty tuple can be created using the braces().
Syntax
The following is the syntax.
variable_name = ()
Where,
-
Variable_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))
Following is an output of the above code -
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))
Following is an output of the above code -
('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
Creating an Empty tuple using 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))
Following is an output of the above code -
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))
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