Python: Data Aggregation (Numbers)
Another type of task that needs loops is ones that involve aggregating data. These tasks include: finding the maximum or minimum value, finding the sum of something, or finding the mean. With these tasks, the result depends on the whole data set.
To calculate the sum, you have to add up all the numbers, and to calculate the maximum, you have to compare them. Accountants and marketers will be familiar with such tasks. They work in Microsoft Excel or Google Tables.
In this lesson, we'll look at how aggregation applies to numbers and strings.
Suppose we need to find the sum of a set of numbers. Let's implement a function that adds numbers in a specified range, including boundaries. A range is a set of numbers with an upper and lower limit. For example, the range [1, 10] includes the integers from one to ten.
sum_numbers_from_range(5, 7) # 5 + 6 + 7 = 18 sum_numbers_from_range(1, 2) # 1 + 2 = 3 # [1, 1] - a range with the same beginning and end is also a range # It includes one number, which is the range boundary itself sum_numbers_from_range(1, 1) # 1 sum_numbers_from_range(100, 100) # 100
To implement this code, you need a loop, because adding numbers is an iterative process, i.e., it's repeated for every number. The number of iterations depends on the size of the range.
Check out the code below:
def sum_numbers_from_range(start, finish): # It is technically possible to change the start # But the input arguments must be left at their original value # This will make the code easier to analyze i = start sum = 0 # Initializing the amount while i <= finish: # Moving to the end of the range sum = sum + i # Calculate the sum for each number i = i + 1 # Move to the next number in the range # Return the result return sum
The structure of the loop here is standard: there's a counter, which is initialized with the initial value of the range, a loop which has a condition requiring you to stop at the end of the range, and the counter changes at the end of the loop body. The number of iterations in such a loop is equal to
finish - start + 1. For the range [5, 7], it's 7 - 5 + 1, which is three iterations.
The main difference from the usual way of processing is the logic of calculating the result. In aggregation tasks, there's always a variable that stores the result of the loop. In the code above, it's
sum. It changes at each iteration of the loop: the next number in the range is added:
sum = sum + i.
The process looks like this:
# To call sum_numbers_from_range(2, 5) sum = 0 sum = sum + 2 # 2 sum = sum + 3 # 5 sum = sum + 4 # 9 sum = sum + 5 # 14 # 14 is the result of adding numbers in the range [2, 5]
sum variable has an initial value, any repeating operation will begin with it. In the example above, it's
In math, we have the concept of a neutral element, and each operation has a different one. An operation with this element doesn't change the value it's working on. For example, in addition, any number plus zero gives the number itself. It's the same for subtraction. Concatenation also has a neutral element, which is an empty string:
'one' will be
Implement a function called
multiply_numbers_from_range() that multiplies numbers in a specified range, including range boundaries. Call example:
multiply_numbers_from_range(1, 5) # 1 * 2 * 3 * 4 * 5 = 120 multiply_numbers_from_range(2, 3) # 2 * 3 = 6 multiply_numbers_from_range(6, 6) # 6
The exercise doesn't pass checking. What to do? 😶
If you've reached a deadlock it's time to ask your question in the «Discussions». How ask a question correctly:
- Be sure to attach the test output, without it it's almost impossible to figure out what went wrong, even if you show your code. It's complicated for developers to execute code in their heads, but having a mistake before their eyes most probably will be helpful.
In my environment the code works, but not here 🤨
Tests are designed so that they test the solution in different ways and against different data. Often the solution works with one kind of input data but doesn't work with others. Check the «Tests» tab to figure this out, you can find hints at the error output.
My code is different from the teacher's one 🤔
It's fine. 🙆 One task in programming can be solved in many different ways. If your code passed all tests, it complies with the task conditions.
In some rare cases, the solution may be adjusted to the tests, but this can be seen immediately.
I've read the lessons but nothing is clear 🙄
It's hard to make educational materials that will suit everyone. We do our best but there is always something to improve. If you see a material that is not clear to you, describe the problem in “Discussions”. It will be great if you'll write unclear points in the question form. Usually, we need a few days for corrections.
By the way, you can participate in courses improvement. There is a link below to the lessons course code which you can edit right in your browser.