DoAssignment study guide
C3.2 · Modify data-analysis code and explain the result
Learn to modify data-analysis code and explain the result through clear examples and targeted practice.
Ontario Grade 8 Mathematics
Algebra
Ontario Grade 8 Mathematics — C3.2
A list of numbers can describe information, such as the number of pages read on several days. Code gives a computer instructions for working with that information. In this lesson, the code uses a small set of clearly defined commands. You will change an instruction, work out what the code will display, and explain the result in everyday language. The examples use hypothetical data made for learning.
What you will learn
- Read simple data-analysis code and identify what it is meant to do.
- Modify an instruction or value to answer a new question about data.
- Explain what the modified code displays and why.
- Check a result by comparing it with the data and the instructions.
1. Read a small set of code
A data set is a collection of information. A value is one item in that collection. For example, four daily page counts make a data set with four values.
A variable is a name that stores information for the code to use. In `pages = [6, 8, 5, 9]`, the variable `pages` stores a list of four values. The square brackets mark the beginning and end of the list.
For this lesson, read the code using these command meanings: `show(x)` displays the result called `x`; `count(list)` gives the number of values in a list; and `sum(list)` adds the values in a list. The command names are explained here so you can predict the result without needing a particular programming app.
For example, `show(count(pages))` displays how many values are in `pages`. It does not add the values. Always read the command before describing its result.
- A variable is a named place that stores data.
- `count` gives the number of values; `sum` adds their values.
- `show` displays the result of the command inside its brackets.
2. Modify code to answer a question
To modify code is to change an instruction or value so the code answers a question. First, decide what the question asks for. Then find the part of the code that controls that result.
If the question asks for a total instead of the number of entries, change `count` to `sum`. Keep the data list as it is. This makes it clear that the result changed because the instruction changed, not because the data changed.
A condition is a rule that decides which values are included. “At least 10” means 10 or any number greater than 10. In the example command `keep_at_least(list, 10)`, the code keeps only values that meet this rule. Changing the cutoff changes which values are kept.
Change only what is needed, then work out the result from the data. This makes your explanation precise: name the changed instruction, state the output, and say how the instruction produces that output.
- Match the code change to the question.
- Keep the original data unchanged unless the question asks you to change it.
- A condition determines which values are included.
3. Explain and check the output
Output is the information that the code displays. An output needs context. For example, say “the code displays 4 values,” not just “the answer is 4.”
To check an output, apply the instruction to the values yourself. For a count, identify which values qualify and count them. For a total, add the listed values. If your check and the code's output do not match, reread the command and the data.
Do not claim more than the code shows. A count tells how many values meet a rule. It does not give the total of those values. A total tells what the values add to. It does not tell how many entries there are.
- Explain what the output represents, not only its number.
- Check a count by identifying qualifying values.
- Check a total by adding the values included.
4. Practice the explain-and-check routine
Use this routine when modifying a short piece of data-analysis code: read the data, read the command, identify the question, make the smallest useful change, and explain the new output.
For a count, name the rule and the values that meet it. For a total, name the values being added. This connects the code's result to the actual data instead of leaving the result unexplained.
- Read, modify, calculate, explain, and check.
- Connect each result to the data and instruction that produced it.
Worked example
Change a count into a total
A hypothetical reading log stores pages read on four days: 6, 8, 5, and 9. The code is `pages = [6, 8, 5, 9]` and `show(count(pages))`. Modify the code to display the total pages, then explain the output.
- Read the current command`count(pages)` gives the number of entries. The list has four values, so the current command displays 4.
- Change the commandThe question asks for a total, so replace `count` with `sum`. Leave the list unchanged.
- Explain and checkThe modified code is `show(sum(pages))`. It displays 28 because the four daily page counts add to 28 pages.
Answer: Use `show(sum(pages))`. The output is 28 pages, the total of the four daily counts.
Check: The list still has four entries, but `sum` adds their values instead of counting them.
Worked example
Change a condition and explain the new count
A hypothetical data set contains values 7, 9, 10, 12, and 14. The code keeps values at least 10, then counts them: `selected = keep_at_least(values, 10)` and `show(count(selected))`. Modify it to count values at least 12. Explain the output.
- Apply the original ruleAt least 10 includes 10 and greater values. The qualifying values are 10, 12, and 14, so the original count is 3.
- Change the cutoffReplace the cutoff 10 with 12. The data and count command stay the same.
- Find and explain the outputThe values that are at least 12 are 12 and 14. There are two such values, so the modified code displays 2.
Answer: Use `selected = keep_at_least(values, 12)` and keep `show(count(selected))`. The output is 2 because 12 and 14 meet the new rule.
Check: The value 10 no longer qualifies. The value 12 does qualify because the condition includes 12 itself.
Worked example
Change both the data and the summary
A hypothetical list stores the number of books read by three students: `books = [2, 1, 4]`. The code displays the count: `show(count(books))`. A fourth student reports reading 3 books. Modify the code so it displays the total number of books reported by all four students. Explain what the output means.
- Update the dataThe question includes a fourth student's report, so add 3 as a new value in the list. The updated list is `books = [2, 1, 4, 3]`.
- Choose the requested summaryThe question asks for the total, not the number of students. Replace `count` with `sum`.
- Explain the resultThe modified code is `show(sum(books))`. It displays 10, meaning the four reported book counts add to 10 books.
Answer: Update the list to `books = [2, 1, 4, 3]` and use `show(sum(books))`. The output is 10 books in total.
Check: There are four listed values, but the requested output is their total. Adding them gives 10.
Common mistakes and how to avoid them
Changing the data when only the summary instruction needs to change.
Correction: Keep the list as it is when the question asks for a different summary.
Describing a count as a total.
Correction: `count` gives the number of entries. `sum` adds their values.
Using the old cutoff after changing a condition.
Correction: Apply the new rule to each value, then count the values that qualify.
Giving only a number with no meaning.
Correction: State what the output counts or adds and which data it uses.
Lesson summary
- Read the data and the command before changing code.
- Modify the part that controls the result the question asks for.
- Explain what the output means and how the code produced it.
- Check the output against the values and the instructions.
Check your understanding
Question 1
The list is `scores = [4, 6, 8]`. Change `show(count(scores))` so it displays the total. What does the modified code display, and why?
- It displays 18, because 4, 6, and 8 add to 18.
- It displays 3, because the list contains three values.
- It displays 8, because 8 is the largest value.
- It displays 4, because 4 is the first value.
Show answer and explanation
It displays 18, because 4, 6, and 8 add to 18.
Replace `count` with `sum`. The code adds the three listed values, giving 18.
Question 2
A list is `times = [3, 5, 6, 8]`. Code keeps values at least 6 and displays their count. What does it display, and what does that output mean?
- 2; two values, 6 and 8, meet the at-least-6 rule.
- 4; all four values meet the at-least-6 rule.
- 14; the qualifying values add to 14, so this is the count.
- 6; the cutoff itself is the count.
Show answer and explanation
2; two values, 6 and 8, meet the at-least-6 rule.
The qualifying values are 6 and 8. The code counts them, so it displays 2, meaning two list values are at least 6.
Key terms
- Data set
- A collection of information or values.
- Value
- One item of information in a data set.
- Code
- Instructions that tell a computer what to do.
- Variable
- A name used to store information for code.
- Command
- An instruction in code that performs a task.
- Condition
- A rule that decides which values are included.
- Output
- The information displayed after code follows its instructions.
Continue through Grade 8 Math
View the complete Grade 8 Math Ontario Grade 8 Mathematics curriculum and lessons
- C3.1 · Write code to analyse data and communicate decisions
- D1.1 · Choose one-variable or two-variable data for a question
- C1.1 · Compare growing and shrinking patterns by rate and initial value
- C1.2 · Represent rational-number patterns with tables, graphs, and equations
- C1.3 · Extend patterns and predict unknown terms using linear rules
- C1.4 · Create patterns showing relationships among rational numbers
About this lesson and its review
Published by DoAssignment. This reviewed lesson follows Ontario Grade 8 Mathematics, expectation C3.2. It is a study resource, not an official curriculum publication.
Before publication, content is checked for structure, mathematical or chemical notation, calculations, course boundaries, and readability. Errors can still occur, so corrections are welcomed.