Python Program to check if the tuple is empty


In Python, it is often necessary to check if a tuple is empty in order to determine what actions to take in a program.

A tuple in python is a pre-defined datatype that stores heterogeneous data, data of different types, in a single variable. The items can be indexed for further operations and python provides a wide array of methods to work upon them.

They are immutable by nature which means we cannot make changes after we have created a tuple. This is whenever we perform some operation on the tuple a new tuple with resulting values is created.

We can define a tuple in python using the round brackets enclosing the data that we wish to store −

Var = (1, ‘a’, 3.7)

A tuple is considered empty if the number of elements in it is zero. It is considered to be the most basic ability that every datatype should have. There are various ways for us to check whether a tuple is empty.

  • Using the len method

  • Using the comparison method

  • Using Boolean meaning of tuples

Using the len() method

Python being an easy to use and user centered language provides us with variety of methods that makes it easy for us to perform repetitive tasks. One such method is the len() method. It is a default method that is used to find the length of a variable. In other words, it returns the number of elements in each variable, preferably an iterator.

We will use the len() method to check the length of the tuple if it is zero then it will be treated as empty and not empty otherwise. The use of method is simple we write the keyword len() followed by the name of the variable we wish to check the length of in round braces.

A = [4, 5, 6]
lengthA = len(A)

The above example illustrates the use of the len() method.

Algorithm

Step 1 - Create a tuple.

Step 2 – Create a variable lenTuple and store the result of len() method given the tuple as argument.

Step 3 – Check whether the value of lenTuple is 0.

Step 4 – if yes, then the tuple is empty, print empty tuple.

Step 5 – if no, then the tuple is not empty, print not empty.

Example

A = (1, 2, 3)
B = ()
lenA = len(A)
lenB = len(B)
if lenA == 0:
   print("A is empty")
else:
   print("A is not empty")
if lenA == 0:
   print("B is empty")
else:
   print("B is not empty")

Output

A is not empty
B is not empty

Using comparison operator

Another way we can check for an empty tuple, is to make use of the comparison operator. We will compare the tuple we wish to check with an empty tuple. As the comparison operator returns true if both operands are same and false otherwise. So, if the tuple we have given is empty the expression will result in true and if it isn’t empty it will return in false.

Example

A = (1, 2, 3)
B = ()
if A == ():
   print("A is empty")
else:
   print("A is not empty")
if B == ():
   print("B is empty")
else:
   print("B is not empty")

Output

A is not empty
B is empty

Using Boolean meaning of tuples

We know that python is an easy-to-use language, this is why it provides us with a way seamlessly work across various datatypes. This functionality of python makes it possible for us to use tuple and use the binary meaning behind them for comparison.

We know that python treats anything that has some value / elements in it as true, and any empty or zero value is treated as false. So, we will just check whether a given tuple is equivalent to binary truth, we will print not empty if they are equal, else, we will print empty.

Algorithm

Step 1 − Create two tuples

Step 2 − Check whether they are true using the if clause

Step 3 − If yes, print not empty

Step 4 − If no, print empty

Example

A = (1, 2, 3)
B = ()
if A:
   print("A is not empty")
else:
   print("A is empty")
if B:
   print("B is empty")
else:
   print("B is not empty")

Output

A is not empty
B is not empty

Conclusion

In this article, we focused on three different ways to check for an empty tuple. We learnt the use of in-built python method to check the length of the tuple and using it to determine an empty tuple. We also saw the use of comparison with an empty tuple to get the result. At last, we saw the use of binary equivalents of tuples to decide whether they are empty or not.

All these methods are easy to implement because of python’s simplistic nature and programmer friendly approach.

Updated on: 17-Feb-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements