Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 62 ยท Getting Started

Vectors

Learn what vectors are, how to read them as arrows, points and lists, and how to add, subtract and scale them by hand and with NumPy arrays.

A vector is an ordered list of numbers. That one sentence hides three different pictures, and good linear algebra thinking means switching between them freely: a vector is a list in a program, a point on a map, and an arrow that says "go this far in this direction".

In this lesson you will learn the vocabulary (components, dimension, zero vector), the two core operations (addition and scalar multiplication), and how to write all of it in NumPy. The next lessons build on these basics: length and distance, then the dot product. If you want the bigger picture first, read What Is Linear Algebra?.

Three ways to see a vector

Take the vector v = (3, 2).

One vector, three pictures

A list Two numbers in order.
A point 3 steps right, 2 steps up from the origin.
An arrow Starts at the origin and ends at that point.

The numbers never change; only how we think about them.

1 / 2

The numbers inside a vector are its components (or entries). A vector with n components lives in n-dimensional space, written R^n. So (3, 2) is in R^2, the flat plane, and (1, 0, 5) is in R^3, ordinary space. Data scientists happily use R^100 for a row of 100 features; you can no longer draw it, but every rule stays exactly the same.

By convention vectors in math are written as columns:

TEXT
v = [ 3 ]
    [ 2 ]

In text we usually write (3, 2) to save space. NumPy has a neat simplification: a plain 1D array is just "a vector" and does not care whether it is a row or a column. We will meet the difference when matrices arrive.

Vectors in NumPy

You create a vector with np.array. Its shape tells you the dimension, and indexing starts at 0, as always in Python.

Python Creating and inspecting vectors

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The zero vector has every component equal to zero. It is the "do nothing" vector, and it will keep showing up as the identity for addition.

A Python list such as [1, 2, 3] looks like a vector but does not behave like one. [1, 2] + [3, 4] glues the lists together to give [1, 2, 3, 4]. Convert to an np.array first and + adds component by component.

Adding and subtracting vectors

To add two vectors of the same dimension, add the matching components:

TEXT
(3, 2) + (1, 4) = (3 + 1, 2 + 4) = (4, 6)

Geometrically, put the second arrow's tail at the first arrow's head; the sum is the arrow from the very first tail to the very last head. This is the "walk then walk" picture: go three steps right and two up, then one right and four up, and you have gone four right and six up in total.

Subtraction works the same way. a - b is the arrow that goes from b to a, which is why subtraction is how we measure the gap between two points. You can only add or subtract vectors with the same number of components; (1, 2) plus (1, 2, 3) has no meaning.

Python Adding and subtracting

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Addition behaves like ordinary number addition: a + b = b + a (commutative), (a + b) + c = a + (b + c) (associative), and a + 0 = a.

Scalar multiplication

In linear algebra a plain number is called a scalar. Multiplying a vector by a scalar multiplies every component, which stretches or shrinks the arrow:

TEXT
2 * (3, 2)  = (6, 4)      twice as long, same direction
0.5 * (3, 2) = (1.5, 1)   half as long
-1 * (3, 2) = (-3, -2)    same length, opposite direction

A positive scalar keeps the direction. A negative scalar flips it. Zero collapses everything to the zero vector. Notice that scaling never turns the arrow sideways: the result always lies on the same line through the origin as the original.

Python Scaling a vector

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The last line is a preview of a central idea. The vector (3, 2) is 3 copies of (1, 0) plus 2 copies of (0, 1). Building vectors out of scaled and added pieces is called forming a linear combination, and Linear Combinations and Span is entirely about it.

Rules you can rely on

Adding and scaling obey the rules of ordinary algebra. For vectors u, v and scalars c, d:

  • c * (u + v) = c*u + c*v (scaling spreads over addition)
  • (c + d) * v = c*v + d*v
  • c * (d * v) = (c*d) * v
  • 1 * v = v

These are why you can manipulate vector expressions the same way you manipulate numbers. Any collection of objects that obeys these rules is called a vector space; we study that idea properly in the Vector Spaces chapter.

Vectors as real data

Vectors are not only arrows. A person described by (age, height in meters, weight in kilograms) is a vector in R^3. Adding two daily sales vectors gives a weekly total; scaling by 0.9 applies a ten-percent discount to every product at once. The component positions must mean the same thing in every vector you combine, which is exactly the same-dimension rule at work.

Python Vectors as data

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In the last line, dividing a vector by a number is just scaling by 1/2. Averages of vectors are one of the most common operations in data analysis.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is (3, 2) + (1, 4)?

    Add matching components: 3 + 1 = 4 and 2 + 4 = 6.
  2. 2. What happens to a vector when you multiply it by -2?

    The factor 2 doubles the length and the minus sign flips the direction. Scaling never rotates a vector sideways.
  3. 3. Why does [1, 2] + [3, 4] give [1, 2, 3, 4] in plain Python?

    The + operator on lists means concatenation. Use np.array to get component-wise vector addition.
  4. 4. Which pair of vectors can NOT be added?

    Vector addition needs the same number of components, because each component is matched with the one in the same position.

Summary

  • A vector is an ordered list of numbers: a list, a point and an arrow at the same time.
  • The number of components is the dimension; np.array([...]) creates a vector and .shape reports its size.
  • Add or subtract vectors component by component, and only when their dimensions match.
  • Multiplying by a scalar stretches, shrinks or flips a vector but keeps it on the same line through the origin.
  • Adding scaled vectors gives a linear combination, the building block of the whole subject.
  • Use NumPy arrays, not Python lists, for vector arithmetic.

Next up: measure vectors in Length, Distance and Unit Vectors.