Check if Element Exists
Check whether an entered integer occurs in a list. Students use membership for a yes-or-no search and compare existence with finding a position or frequency.
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67x = int(input("Enter element to search: "))89# Check existence10if x in arr:11 print(f"{x} exists in array")12else:13 print(f"{x} does not exist in array")
Output
Enter array size: 5 Element 1: 10 Element 2: 20 Element 3: 30 Element 4: 20 Element 5: 40 Enter element to search: 20 20 exists in array
In this one
- Ask a membership question
- Search unsorted data
- Separate existence from frequency
How it works
- 1
Collect arr and read x.
- 2
Evaluate x in arr.
- 3
Print one existence or absence message.
Edge cases
What happens for an empty list?
What happens for an empty list?
The not-exists message is printed.
Does a duplicate produce extra messages?
Does a duplicate produce extra messages?
No. The membership decision is made once.
What's going on
x in arr tests membership after the values have been collected. It answers whether any element equals x.
The program prints only an existence message, not an index or count. Multiple occurrences still produce one positive message. Membership does not require sorted input and does not change the list; an empty list simply contains no candidate.
What happens for an empty list? The not-exists message is printed.
Does a duplicate produce extra messages? No. The membership decision is made once.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What does in return conceptually?
Pick an answer to check it.
Question 2
What happens for an empty list?
Pick an answer to check it.
Questions people ask
What happens for an empty list?
The not-exists message is printed.
Does a duplicate produce extra messages?
No. The membership decision is made once.
What does in return conceptually?
A boolean answer.
๐Related Content
- ๐
Phase 4 - Learn Concepts
Review Phase 4 concepts and explanations
- ๐
Phase 4 - Quiz
Test your Phase 4 understanding with quiz questions
- ๐ป
Phase 4 - All Practice Problems
Explore all practice problems for Phase 4
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum