Write a Python program to Elementwise AND in tuples


This program defines two tuples tup1 and tup2, each containing 4 elements. The tuples are then printed to the console. Next, the program uses a list comprehension with the zip function to iterate over the elements of both tuples tup1 and tup2 simultaneously. For each pair of elements e1 and e2 from the two tuples, the bitwise AND (&) operation is performed and the result is added to a list.

Finally, the list comprehension is passed to the tuple function to convert the list to a tuple, which is then stored in the variable res. The string representation of res is then printed to the console with the message "The AND tuple :".

Source Code

tup1 = (10, 4, 6, 9)
tup2 = (5, 2, 3, 3)
 
print("Tuple 1 : ",tup1)
print("Tuple 2 : ",tup2)
 
res = tuple(e1 & e2 for e1, e2 in zip(tup1, tup2))
 
print("The AND tuple : ",res)

Output

Tuple 1 :  (10, 4, 6, 9)
Tuple 2 :  (5, 2, 3, 3)
The AND tuple :  (0, 0, 2, 1)

Example Programs