Python program to check for None tuple

In this program, we are given a tuple of elements. We need to create a Python program to check if the given tuple is a None Tuple or not.
Submitted by Shivang Yadav, on January 04, 2022

While working with complex data that are being received from a server or some input sources there are cases where the values can be left empty, programs need to keep a check on empty values to avoid compilation overhead or error. So, the need for a code to check whether the given tuple is a None tuple is there. Here, we will see a Python program to check for None Tuple.

Before going further with the problem, let's recap some basic topics that will help in understanding the solution.

Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.

Tuples in Python is a collection of items similar to list with the difference that it is ordered and immutable.

Example:

tuple = ("python", "includehelp", 43, 54.23)

Checking For None Values in Python

In this program, we are given a tuple consisting of some values. And we will be creating a Python program that will check whether the given Tuple is a None tuple or not.

None Tuple is a tuple whose all values are none.

Example:
(None, None, None)

To check for None Tuple we need to traverse the tuple and check if each value is a None value or not. If all values are none, return true otherwise false.

This can be performed in Python using multiple method combinations. Let's see some of them here.

Method 1:

One method to check for none tuple is to check if all elements are None by using the all() method with a generator expression.

# Python program to check for 
# none tuple 

# Initializing and printing tuple
myTuple = (None, None, None, None, None)
print("The elements of tuple : " + str(myTuple))

# Checking for None tuple
isNone = all(val is None for val in myTuple)
print("Is the given tuple a none tuple? " + str(isNone))

Output:

The elements of tuple : (None, None, None, None, None)
Is the given tuple a none tuple? True

Method 2:

One more method to check if the tuple is a none tuple or not is by comparing the number of none values in the tuple which is the length of the tuple. If both are the same then the tuple is none tuple otherwise not.

The length of the tuple is calculated using len() method and the count of none elements is found using the count() method.

# Python program to check for 
# none tuple 

# Initializing and printing 
# tuple
myTuple = (None, None, None, None, None)
print("The elements of tuple : " + str(myTuple))

# Checking for None tuple
isNone = len(myTuple) == myTuple.count(None)
print("Is the given tuple a none tuple? " + str(isNone))

Output:

The elements of tuple : (None, None, None, None, None)
Is the given tuple a none tuple? True

Python Tuple Programs »



Related Programs



Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.