Element-wise Sum
Add corresponding entries of two equal-sized lists. Students use a common index and compare element-wise addition with concatenating the lists themselves.
1# Take arrays2n = int(input("Enter array size: "))3arr1 = []4for i in range(n):5 arr1.append(int(input(f"Array1 element {i+1}: ")))67arr2 = []8for i in range(n):9 arr2.append(int(input(f"Array2 element {i+1}: ")))1011# Element-wise sum12sum_arr = []13for i in range(n):14 sum_arr.append(arr1[i] + arr2[i])1516print(f"Sum array: {sum_arr}")
Output
Enter array size: 3 Array1 element 1: 1 Array1 element 2: 2 Array1 element 3: 3 Array2 element 1: 4 Array2 element 2: 5 Array2 element 3: 6 Sum array: [5, 7, 9]
In this one
- Pair equal indexes
- Add values rather than lists
- Build a result container
How it works
- 1
Read both lists using the same n.
- 2
Append arr1[i] + arr2[i] for each index.
- 3
Print sum_arr after all pairs are processed.
Edge cases
What does n = 0 produce?
What does n = 0 produce?
An empty result list.
Are all pairs of values added?
Are all pairs of values added?
No. Only values at the same index are paired.
What's going on
Both lists are read with the same count n. For every index, arr1[i] + arr2[i] is appended to a new sum_arr.
Thus [1, 2] and [10, 20] produce [11, 22]. Using arr1 + arr2 instead would concatenate lists, not add positions. The source lists stay unchanged, and the result contains one value per shared index.
What does n = 0 produce? An empty result list.
Are all pairs of values added? No. Only values at the same index are paired.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Which expression adds corresponding values?
Pick an answer to check it.
Question 2
What does n = 0 produce?
Pick an answer to check it.
Questions people ask
What does n = 0 produce?
An empty result list.
Are all pairs of values added?
No. Only values at the same index are paired.
Which expression adds corresponding values?
arr1[i] + arr2[i].
๐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