Replace Evens with 1, Odds with 0
Encode even list values as one and odd values as zero in Python. Students trace in-place index assignments and avoid confusing this rule with remainder itself.
1# Take array2n = int(input("Enter array size: "))3arr = []4for i in range(n):5 arr.append(int(input(f"Element {i+1}: ")))67# Replace8for i in range(len(arr)):9 if arr[i] % 2 == 0:10 arr[i] = 111 else:12 arr[i] = 01314print(f"Modified array: {arr}")
Output
Enter array size: 5 Element 1: 2 Element 2: 3 Element 3: 4 Element 4: 5 Element 5: 6 Modified array: [1, 0, 1, 0, 1]
In this one
- Encode a boolean property
- Distinguish indicators from remainders
- Overwrite original magnitudes
How it works
- 1
Visit every index.
- 2
Assign one for an even current value.
- 3
Assign zero for an odd one and print arr.
Edge cases
What does zero become?
What does zero become?
One, because zero is even.
What does -3 become?
What does -3 become?
Zero, because it is odd.
What's going on
Each stored value is tested before its position is overwritten. Even values become one; the else branch writes zero for odd values.
This is the opposite of simply storing value % 2, which gives zero for evens. Every list position receives an indicator, so the original numeric magnitudes are lost. The length is unchanged and empty input performs no updates.
What does zero become? One, because zero is even.
What does -3 become? Zero, because it is odd.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Why is this not just value % 2?
Pick an answer to check it.
Question 2
What does zero become?
Pick an answer to check it.
Questions people ask
What does zero become?
One, because zero is even.
What does -3 become?
Zero, because it is odd.
Why is this not just value % 2?
The desired indicator reverses that remainder mapping.
๐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