ADVERTISEMENT
ADVERTISEMENT

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

In this program, we are given a tuple. We need to create a Python program to check if the given tuple is a True Record or not.
Submitted by Shivang Yadav, on November 19, 2021

Python programming language has found multiple usages in the programming world. A check for valid data is required in order to perform the given tasks efficiently.

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 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.

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

Python Tuple Programs »



ADVERTISEMENT




ADVERTISEMENT


Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.