Tuples are a very useful data type in Python that allows you to store multiple items in a single variable. Tuples are ordered, immutable, and can contain items of different data types. In this tutorial, we will focus on how to create an empty tuple in Python.
Empty Tuple in Python
An empty tuple in Python is a tuple that does not contain any items. This is useful as a placeholder when you need to create a tuple that will be populated later, or when you need to represent a lack of data in a structured way.
How to create an empty tuple in Python
There are several ways to create an empty tuple in Python:
1. Using Empty Parentheses ()
The simplest and most common way to create an empty tuple in Python is to use an empty pair of parentheses. This is also considered the most Pythonic way.
empty_tuple = ()
Example:
Here is an example of an empty tuple in Python.
# Create an empty tuple using empty parentheses
empty_tuple = ()
# Print the empty tuple
print(empty_tuple)
# Check its type
print(type(empty_tuple))
Output:
()
<class 'tuple'>
You can check the output in the screenshot.
2. Using the tuple() Constructor
You can also create an empty tuple in Python using the tuple()
constructor without any arguments.
empty_tuple = tuple()
This approach is slightly less concise, but it makes it clear that you are creating a tuple in Python. This might be preferred in some contexts for clarity.
Example:
Here is an example of creating an empty tuple using the tuple() Constructor in Python.
# Create an empty tuple using the tuple() constructor
empty_tuple = tuple()
# Print the empty tuple
print(empty_tuple)
# Check its type
print(type(empty_tuple))
Output:
()
<class 'tuple'>
You can see the screenshot below:
Why Would You Need an Empty Tuple in Python?
You might be wondering why someone would need a Python empty tuple. Here are a few scenarios:
- As a Placeholder: When you need a placeholder for a variable that will hold a tuple later.
- Function with No Arguments: When defining a function that takes no arguments but you still want to use the function call syntax.
- Immutability: Unlike lists, tuples are immutable. So, sometimes it is useful to have an empty immutable collection.
Summary
In this tutorial, we learned two simple ways to create an empty tuple in Python by using either empty parentheses ()
or the tuple()
constructor. Depending on the context, you might choose one over the other for readability and clarity. Empty tuples can be useful in various scenarios such as placeholders, function definitions, and when you need an immutable collection.
You may also like the following tutorials:
- Convert NumPy Tuple to List in Python
- How to concatenate tuples in Python
- How to convert Python tuple to a dictionary
- Python program to sort list of tuples
I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.