- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check if element is present in tuple in Python
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
- A tuple is defined, and is displayed on the console.
- The value of 'N' is initialized.
- The loop is iterated over, and if the element 'N' is present in the tuple, a value is assigned 'True'.
- This value is assigned to a result.
- It is displayed as output on the console.
- Check if element is present in tuple of tuples in Python
- Python – Check if any list element is present in Tuple
- Check if variable is tuple in Python
- Check if substring present in string in Python
- Check if one tuple is subset of other in Python
- How do you check if an element is present in a list in Java?
- Test if element is present using Selenium WebDriver?
- Python Program to check if a substring is present in a given string.
- Test if tuple is distinct in Python
- Check if tuple has any None value in Python
- Check if tuple and list are identical in Python
- Check if a value is present in an Array in Java
- Python – Check if particular value is present corresponding to K key
- Check if elements of Linked List are present in pair in Python
- Check if a key is present in a C++ map or unordered_map