Tutorials  /  Python

Vectors in Python - A Quick Introduction!

Ccentron Redaktion · January 2024 ·5 min read ·Python, Tutorial

Today, we will be having a look at one of the most unaddressed topics in Python that is, Vectors in Python. So, let us begin!

VM

Matching infrastructure at centron

Scripts and services eventually need an environment that keeps running: ccloud³ VMs from €3.12 per month, billed by the hour. Rent a cloud server →

First, what is a Vector?

A vector in a simple term can be considered as a single-dimensional array. With respect to Python, a vector is a one-dimensional array of lists. It occupies the elements in a similar manner as that of a Python list.

Let us now understand the Creation of a vector in Python.

Creation of a Vector in Python

Python NumPy module is used to create a vector. We use numpy.array() method to create a one-dimensional array i.e. a vector.

Syntax:

Code
numpy.array(list)

Example 1: Horizontal Vector

Python
import numpy as np 

lst = [10,20,30,40,50] 

vctr = np.array(lst) 

print("Vector created from a list:") 
print(vctr) 

Output:

Code
Vector created from a list:
[10 20 30 40 50]

Example 2: Vertical Vector

Python
import numpy as np 

lst = [[2], 
        [4], 
        [6],
          [10]]  

vctr = np.array(lst) 

print("Vector created from a list:") 
print(vctr) 

Output:

Code
Vector created from a list:
[[ 2]
 [ 4]
 [ 6]
 [10]]

Basic Operations on a Python Vector

Having created a Vector, now let us perform some basic operations on these Vectors now!

Here is a list of the basic operations that can be performed on a Vector–

  • Addition
  • Subtraction
  • Multiplication
  • Division
  • Dot Product, etc.

Let us begin!

1. Performing addition operation on a Python Vector

Below, we have performed Vector addition operation on the vectors.

The addition operation would take place in an element-wise manner i.e. element by element and further the resultant vector would have the same length as of the two additive vectors.

Syntax:

Code
vector + vector

Example:

Python
import numpy as np 

lst1 = [10,20,30,40,50] 
lst2 = [1,2,3,4,5]

vctr1 = np.array(lst1) 

vctr2= np.array(lst2) 

print("Vector created from a list 1:") 
print(vctr1) 
print("Vector created from a list 2:") 
print(vctr2) 

vctr_add = vctr1+vctr2
print("Addition of two vectors: ",vctr_add)

Output:

Code
Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[1 2 3 4 5]
Addition of two vectors:  [11 22 33 44 55]

2. Performing Subtraction of two vectors

On similar lines, in subtraction as well, the element-wise fashion would be followed and further the elements of vector 2 will get subtracted from vector 1.

Let us have a look at it’s implementation!

Python
import numpy as np 

lst1 = [10,20,30,40,50] 
lst2 = [1,2,3,4,5]

vctr1 = np.array(lst1) 

vctr2= np.array(lst2) 

print("Vector created from a list 1:") 
print(vctr1) 
print("Vector created from a list 2:") 
print(vctr2) 

vctr_sub = vctr1-vctr2
print("Subtraction of two vectors: ",vctr_sub)

Output:

Code
Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[1 2 3 4 5]
Subtraction of two vectors:  [ 9 18 27 36 45]

3. Performing multiplication of two vectors

In a Vector multiplication, the elements of vector 1 get multiplied by the elements of vector 2 and the product vector is of the same length as of the multiplying vectors.

Let us try to visualize the multiplication operation:

x = [10,20] and y = [1,2] are two vectors. So the product vector would be v[ ],

v[0] = x[0] * y[0]

v[1] = x[1] * y[1]

Have a look at the below code!

Python
import numpy as np 

lst1 = [10,20,30,40,50] 
lst2 = [1,2,3,4,5]

vctr1 = np.array(lst1) 

vctr2= np.array(lst2) 

print("Vector created from a list 1:") 
print(vctr1) 
print("Vector created from a list 2:") 
print(vctr2) 

vctr_mul = vctr1*vctr2
print("Multiplication of two vectors: ",vctr_mul)

Output:

Code
Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[1 2 3 4 5]
Multiplication of two vectors:  [ 10  40  90 160 250]

4. Performing Vector division operation

In vector division, the resultant vector is the quotient values after carrying out division operation on the two vectors.

Consider the below example for a better understanding.

x = [10,20] and y = [1,2] are two vectors. So the resultant vector v would be,

v[0] = x[0] / y[0]

v[1] = x[1] / y[1]

Let us now implement the above concept.

Example:

Python
import numpy as np 
 
lst1 = [10,20,30,40,50] 
lst2 = [10,20,30,40,50]
 
vctr1 = np.array(lst1) 
 
vctr2= np.array(lst2) 
 
print("Vector created from a list 1:") 
print(vctr1) 
print("Vector created from a list 2:") 
print(vctr2) 
 
vctr_div = vctr1/vctr2
print("Division of two vectors: ",vctr_div)

Output:

Code
Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[10 20 30 40 50]
Multiplication of two vectors:  [ 1 1 1 1 1 ]

5. Vector Dot Product

In a vector dot product, we perform the summation of the product of the two vectors in an element-wise fashion.

Let us have a look at the below.

vector c = x . y = (x1 * y1 + x2 * y2)

Example:

Python
import numpy as np 

lst1 = [10,20,30,40,50] 
lst2 = [1,1,1,1,1]

vctr1 = np.array(lst1) 

vctr2= np.array(lst2) 

print("Vector created from a list 1:") 
print(vctr1) 
print("Vector created from a list 2:") 
print(vctr2) 

vctr_dot = vctr1.dot(vctr2)
print("Dot product of two vectors: ",vctr_dot)

Output:

Code
Vector created from a list 1:
[10 20 30 40 50]
Vector created from a list 2:
[1 1 1 1 1]
Dot product of two vectors: 150
Jetzt 200 € Guthaben sichern

Testen Sie Ihr Setup auf ccloud³

Registrieren Sie sich in der ccloud³ und erhalten Sie 200 € Startguthaben für Ihr Projekt – z. B. für eine PostgreSQL-VM mit automatischen Backups.

centron Redaktion Technische Redaktion

Das Redaktionsteam von centron schreibt Anleitungen aus dem Betriebsalltag: getestet auf unserer eigenen Plattform, betrieben im Rechenzentrum in Hallstadt bei Bamberg.

Kategorie Python
Teilen
Noch offene Fragen?

Unser Team hilft Ihnen bei Ihrem konkreten Setup weiter – von Menschen, die die Plattform selbst betreiben.

War dieses Tutorial hilfreich?

Ihre Antwort wird anonym gespeichert und hilft uns, die Tutorials zu verbessern.

Kommentare

Noch keine Kommentare – stellen Sie die erste Frage zu diesem Tutorial.

Zum Kommentieren anmelden

Kommentare stehen centron-Kunden offen. Melden Sie sich in Ihrem Konto an, um eine Frage zu diesem Tutorial zu stellen.

Weiterlesen

Das könnte Sie auch interessieren

Jetzt kostenlos anfangen

Melden Sie sich an und erhalten Sie in den ersten 60 Tagen ein Guthaben von 200 € bei centron.

Dieses Werbeangebot gilt nur für neue Konten. Angebot ausschließlich für Gewerbetreibende.

Jetzt loslegen Sales kontaktieren