Q:

Python program to check if the given tuple is a true record or not

belongs to collection: Python Tuple Programs

0

Example:

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

Checking if Given Tuple is True Record or Not

We need to check whether a tuple is a true record or not.

For this, we need to iterate over every element for the record and check if it is a valid record or not. If all are valid print 'true' otherwise 'false'.

Input:
tuple = (true, true, 4 , 1)

Output:
true

In python, we can perform the task in multiple ways using one of the multiple methods that are present in the function.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Method 1:

One method to check if the tuples are True record is by checking if any element is not valid using lambda on map() with  not() method.

Method 1:

One method to perform AND operation on each element of the tuple is by using generator expression for performing the AND operation and zipping together both the tuples using the zip() method.

# Python Program to check if the given Tuple 
# is a True Record or not

# initializing and printing tuple 
myTuple = (True, 1, 'includehelp', True)
print("The elements of the tuple are " + str(myTuple))
  
# Checking if the given tuple is a true record or not
isTrueRecord = not any(map(lambda ele: not ele, myTuple))
  
# printing result
print("Is the given Tuple True record? " + str(isTrueRecord))

Output:

The elements of the tuple are (True, 1, 'includehelp', True)
Is the given Tuple True record? True

Method 2:

Another method to check if the tuple is a true record or not is by using the all() method. This will check if each element of the tuple is a true element and return a truthy value based on the result.

# Python Program to check if the given Tuple 
# is a True Record or not using all() method 

# initializing and printing tuple 
myTuple = (True, 1, 'includehelp', True)
print("The elements of the tuple are " + str(myTuple))
  
# Checking if the given tuple is a true record or not
isTrueRecord = all(myTuple)
  
# printing result
print("Is the given Tuple True record? " + str(isTrueRecord))

Output:

The elements of the tuple are (True, 1, 'includehelp', True)
Is the given Tuple True record? True

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Tuple Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python program to perform AND operation on tuples... >>
<< Python | Elementwise AND in Tuples...