Home »
Python
Adding two vectors | Linear Algebra with Python
Linear Algebra with Python: Here, we are going to learn how to define and add two given vectors in Python?
Submitted by Anuj Singh, on May 10, 2020
Prerequisite: Linear Algebra | Defining a Vector
In the python code, we will add two vectors. We can add two vectors only and only if the both the vectors are in the same dimensional space. For our code, we consider the vectors in 4-dimensional space.
Python code to add two vectors
#Vectors in Linear Algebra
a = [3, 5, -5, 8]
b = [4, 7, 9, -4]
print("Vector a = ", a)
print("Vector b = ", b)
# This is a 4 dimensional vector
# a list in python is a vector in linear algebra
# adding vectors
sum = []
for i in range(len(a)):
sum.append(a[i] + b[i])
print("Vector Addition = ", sum)
# This is how we can print a vector in python
Output
Vector a = [3, 5, -5, 8]
Vector b = [4, 7, 9, -4]
Vector Addition = [7, 12, 4, 4]