01 / 21

Using Programs
with Data

Learn how programs collect, transform, and visualize data to discover patterns and solve real-world problems.

AP Computer Science Principles

What is Data?

Data is a collection of facts, statistics, or values stored in a structured format. Programs process data to extract meaningful information and generate knowledge.

Data
Raw values: numbers, text, booleans. No context yet.
Information
Data with context: "average temperature rose 2°F."
Knowledge
Actionable insight: "warming trend may cause drought."

How Programs Use Data

Collect Input / Import Store Lists / Tables Transform Filter / Sort Visualize Charts

Data Abstraction and Storage

Data abstraction lets us manage complexity by organizing data into structures. Instead of tracking hundreds of individual variables, we group related values together.

Lists
Ordered collection of values accessed by index. Example: [85, 92, 78, 95]
Tables
Rows and columns like a spreadsheet. Each row is a record, each column is a field.

The ETQ Pattern

Extract Read data from source Transform Clean, filter, compute Query Search, aggregate, report CSV, API, sensor Sort, filter, calculate Min, max, average

Filtering vs. Sorting

Filtering Sorting
Purpose Remove rows that don't match a condition Reorder rows by a column's values
Data size Shrinks (fewer rows) Same (same rows, new order)
Example Show only scores \( > 80 \) Arrange scores highest to lowest
AP Pseudocode IF condition, APPEND to new list Compare and swap elements

Step-by-Step: Working with Data

1
Define your question. What do you want to learn from the data? Example: "Which city has the highest average temperature?"
2
Collect the data. Import from a file (CSV), user input, or an API. Store it in a list or table.
3
Clean and filter. Remove incomplete or irrelevant entries. Check for duplicates and invalid values.
4
Process and compute. Use loops to calculate sums, averages, minimums, or maximums across the dataset.
5
Visualize and interpret. Display results as a chart or summary. Answer your original question.

Example 1: Computing the Average

Given a list of test scores, compute the average.

scores ← [85, 92, 78, 95, 88] total ← 0 FOR EACH score IN scores { total ← total + score } average ← total / LENGTH(scores) DISPLAY(average)

Example 2: Filtering a List

From the same scores, keep only those above 85.

scores ← [85, 92, 78, 95, 88] high ← [] FOR EACH s IN scores { IF (s > 85) { APPEND(high, s) } } DISPLAY(high)

Example 3: Finding the Maximum

Find the highest score in a dataset using a linear scan.

scores ← [85, 92, 78, 95, 88] maxVal ← scores[1] FOR EACH s IN scores { IF (s > maxVal) { maxVal ← s } } DISPLAY(maxVal)

Visualizing Data

Visualization turns processed data into visual representations that make patterns easier to identify.

0 25 50 75 100 85 Test 1 92 Test 2 78 Test 3 95 Test 4 88 Test 5

Why Cleaning Data Matters

Real-world data is messy. Before processing, you must handle missing values, duplicates, and incorrect formats. Dirty data leads to wrong conclusions.

Missing Values
A temperature reading of "" or null. Skip it or replace with a reasonable default.
Duplicates
Same student entered twice inflates totals. Remove duplicates before aggregation.
Bad Format
A date stored as "Jan 5" vs "01/05/2024". Standardize before comparing.

Metadata and Privacy

Metadata is data about data. A photo's metadata includes the date, location, and camera model. Understanding metadata is critical for both analysis and privacy.

Metadata Examples
File size, creation date, author, GPS coordinates, device type
Privacy Concern
Metadata can reveal location, habits, and identity even when content is hidden

Iterating with Loops

Programs use loops to process every element in a dataset. Two key patterns appear on the AP exam:

FOR EACH Loop
Visits every element in a list automatically. Best for processing all items: sums, counts, searches.
Index-Based Loop
Uses a counter from 1 to LENGTH(list). Best when you need the position of each element.
// Count how many scores are above 90 count ← 0 FOR EACH s IN scores { IF (s > 90) { count ← count + 1 } } DISPLAY(count)

Common Mistakes

Off-By-One Indexing
AP pseudocode lists start at index 1, not 0. Accessing \(list[0]\) is an error.
Forgetting to Initialize
Setting total or count to 0 before the loop is essential. Uninitialized variables produce garbage results.
Modifying List While Iterating
Removing elements from a list inside a FOR EACH loop can skip items or cause errors.

Summary of Key Concepts

Concept What It Does Example
Data Abstraction Groups related values into structures List of scores
Filtering Keeps only matching items Scores \( > 85 \)
Sorting Reorders without removing Highest to lowest
Aggregation Computes a single summary value Average, max, min
Visualization Displays data as charts/graphs Bar chart
Metadata Describes data about data File date, GPS tag

Question 1 of 5

A program uses a list of 100 temperatures. It loops through the list and keeps only values above 80. What operation is this?

Question 2 of 5

In AP pseudocode, what is the index of the first element in a list?

Question 3 of 5

What value does this program display?

Question 4 of 5

A school collects student names, grades, and home addresses. Which of the following is metadata about this dataset?

Question 5 of 5

What value does this program display?