Sum of Squares of Even Numbers
Sum squares of even numbers through a limit with Python. Students select terms with a stepped range, square each value, and build the running total.
1# Take n2n = int(input("Enter n: "))34# Sum squares of evens5sum_sq_even = 06for i in range(2, n + 1, 2):7 sum_sq_even += i * i89print(f"Sum of squares of evens: {sum_sq_even}")
Output
Enter n: 10 Sum of squares of evens: 220
In this one
- Filter through a range step
- Square selected terms
- Accumulate without a list
How it works
- 1
Initialize sum_sq_even to zero.
- 2
Visit even values through n and add their squares.
- 3
Print the resulting total.
Edge cases
What happens for n = 1?
What happens for n = 1?
No terms are added, so the total is zero.
Is an odd n included?
Is an odd n included?
No. The range only lands on even values.
What's going on
The range supplies two, four, six, and later even values through n. Each supplied value is squared before being added.
For n equal to six, the total is 4 + 16 + 36 = 56. This differs from squaring the sum of the even values. Starting at two makes a separate parity condition unnecessary and leaves zero for limits below two.
What happens for n = 1? No terms are added, so the total is zero.
Is an odd n included? No. The range only lands on even values.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
What is added for i = 4?
Pick an answer to check it.
Question 2
What happens for n = 1?
Pick an answer to check it.
Questions people ask
What happens for n = 1?
No terms are added, so the total is zero.
Is an odd n included?
No. The range only lands on even values.
What is added for i = 4?
16.
๐Related Content
- ๐
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- ๐
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- ๐ป
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum