Lists as vectors: scaling and the dot product

Beginner Python for Data Science
Created by Best · 24.06.2026 at 14:03 UTC

A column of numbers — prices, scores, temperatures — is a vector, and in plain Python a vector is just a list. The most basic operation is applying something to every element. To raise every price by 10%:

prices = [10.0, 20.0, 30.0]
scaled = []
for p in prices:
    scaled.append(p * 1.1)
print(scaled)        # [11.0, 22.0, 33.0]

The pattern is: empty result list, loop, compute one value, append. Python has a tidier shorthand for exactly this, the list comprehension, which reads almost like English ("p times 1.1, for each p in prices"):

scaled = [p * 1.1 for p in prices]

The single most important vector operation in data science is the dot product: multiply two vectors element by element, then add up the results. It underlies weighted averages, similarity, and later matrix multiplication and linear models. To walk two lists in step, use zip, which hands you one pair at a time:

xs = [1.0, 2.0, 3.0]
ys = [4.0, 5.0, 6.0]
dot = 0.0
for x, y in zip(xs, ys):
    dot += x * y
print(dot)           # 1*4 + 2*5 + 3*6 = 32.0

Here dot is an accumulator: start it at 0.0 and add into it as you go. A special case you'll use constantly is a vector's dot product with itself — the sum of squares — which measures size and appears in variance, distances, and error terms.
leads into “Why loops first, and the length pitfall”.*

University approvals: 0
Related cards
Builds on Parsing rows and the bool('False') trap · Python for Data Science
Next Why loops first, and the length pitfall · Python for Data Science
Tasks
Question 1

Which is the idiomatic way to build a list of every price scaled by 1.1?

Question 2

Read a line of space-separated floats from stdin. Print their dot product with themselves (the sum of squares), as a float.

Example input:

1 2 3

Expected output:

14.0
3 test cases will be used for grading
Run checks runtime behavior only. Final correctness is evaluated when you submit.
Question 3

Select every true statement about doing vector work with plain Python lists.

Select all that apply.
Card Info
  • Topic: Python for Data Science
  • Difficulty: Beginner
  • Completed: 0 users
Creator
Best
Best
BestBuddy