Coding Exercise for Beginners in Python |Exercise 16 | Python Tutorials for Beginners #lec47
Python Programming: Summing Even Numbers
Introduction to the Exercise
- The video introduces a coding exercise focused on summing all even numbers from 1 to 100, using the
rangefunction in Python.
- It emphasizes that this task is simpler than previous exercises, which involved summing all numbers from 1 to 100.
Two Approaches to Solve the Problem
- The first method involves initializing a total variable and using
range(2, 101, 2)to iterate through even numbers directly.
- In this approach, the loop adds each even number (2, 4, 6,... up to 100) to the total variable. The expected result is a sum of 2550.
Alternative Method Using Conditional Checks
- The second method does not specify step size in
range, iterating through all numbers from 1 to 100.
- A conditional check (
if i % 2 == 0) determines if a number is even before adding it to the total. This also results in a sum of 2550.
Conclusion and Next Steps
- The instructor encourages viewers that they should now be familiar with using the
rangefunction effectively for such exercises and invites them to proceed with further learning.