When it is required to check if an element is present in the tuple or not, a simple loop can be used. A tuple is an immutable data type. It means, values once defined can't be changed by accessing their index elements. If we try to change the elements, it results in an error. They are important contains since they ensure read-only access.
Below is a demonstration of the same −
my_tuple_1 = (23, 45, 12, 56, 78, 0) print("The first tuple is : ") print(my_tuple_1) N = 12 print("The value of 'N' has been initialized") my_result = False for elem in my_tuple_1 : if N == elem : my_result = True break print("Does the tuple contain the value mentioned ?") print(my_result)
The first tuple is : (23, 45, 12, 56, 78, 0) The value of 'N' has been initialized Does the tuple contain the value mentioned ? True