Check if Element Exists
Check if x exists in array.
IntermediatePhase 4 - Level 2: Searching & Counting LogicExample 1 of 10
check-if-element-exists.py
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
What's going on
Use 'in' operator to check membership.
Key Concepts:
'in' operator checks existence
Returns True if found
Simple membership test
🔗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