Updated April 3, 2023

## Introduction to NumPy Vector

The tuple of one or more than one scalars is called a vector, and the ordinary numbers are the components that are used to build the vectors; and this vectors can be thought of as a list of numbers and just like how we perform the operation on numbers in the list, vector algebra is also performed, and the small case letter v is used to represent a vector as v = (v1,v2,v3) where v1,v2, and v3 represent the values that are scalar and vectors can also be represented in a vertical column and a NumPy array represents a vector in python and a list of numbers can be used to create a NumPy array and the arithmetic operations like addition, subtraction, multiplication, division, dot product, and vector scalar multiplication can be performed on vectors.

**Syntax:**

`vectorname = numpy.array([arrayelement1, arrayelement2..])`

Where vectorname is the name of the vector created by using the array function in Numpy with the elements of the array being arrayname1, arrayname2, and so on.

### Working of NumPy vector

- The tuple of one or more scalar is called a vector, and the ordinary numbers are the components used to build the vectors.
- The vectors can be thought of as a list of numbers, and just like how we perform the operation on numbers in the list, vector algebra is also performed, and the small case letter v is used to represent a vector as v = (v1,v2,v3) where v1,v2, and v3 represent the values that are scalar and vectors can also be represented in a vertical column.
- A NumPy array represents a vector in python, and a list of numbers can be used to create a NumPy array.
- The arithmetic operations like addition, subtraction, multiplication, division, dot product, and vector scalar multiplication can be performed on vectors.

### Examples

Different examples are mentioned below:

#### Example #1

Python program to demonstrate NumPy vector to create two arrays using array function in NumPy and perform vector addition on the created two arrays:

**Code:**

```
#importing the package numpy
import numpy as nu
#creating an array using the array function and storing it in the variable nameofarray1
nameofarray1 = nu.array([[10,20,30,40]])
#displaying the elements of the newly created array
print 'The elements of the first array are:'
print nameofarray1
print '\n'
#creating an array using the array function and storing it in the variable nameofarray2
nameofarray2 = nu.array([[10,20,30,40]])
#displaying the elements of the newly created array
print 'The elements of the second array are:'
print nameofarray2
print '\n'
#using vector arithmetic to add the elements of the first array with the elements of the second array
vectorarray = nameofarray1 + nameofarray2
#displaying the elements of the vector array
print 'The elements of the vector array after adding the elements of the first array by the elements of the second array:'
print vectorarray
print '\n'
```

**Output:**

In the above program, a package in python called numpy is imported to make use of the array function to create new arrays. First, Nameofarray1 is the new array that is created using the array function in NumPy, whose elements are then displayed. Then Nameofarray2 is the new array that is created using the array function in NumPy, whose elements are then displayed. Then vector addition is performed on the two arrays nameofarray1 and nameofarray2 to add the elements of the two arrays.

#### Example #2

Python program to demonstrate NumPy vector to create two arrays using array function in NumPy and perform vector subtraction on the created two arrays:

**Code:**

```
#importing the package numpy
import numpy as nu
#creating an array using the array function and storing it in the variable nameofarray1
nameofarray1 = nu.array([[40,30,20,10]])
#displaying the elements of the newly created array
print 'The elements of the first array are:'
print nameofarray1
print '\n'
#creating an array using the array function and storing it in the variable nameofarray2
nameofarray2 = nu.array([[10,20,30,40]])
#displaying the elements of the newly created array
print 'The elements of the second array are:'
print nameofarray2
print '\n'
#using vector arithmetic to add the elements of the first array with the elements of the second array
vectorarray = nameofarray1 - nameofarray2
#displaying the elements of the vector array
print 'The elements of the vector array after subtracting the elements of the first array by the elements of the second array:'
print vectorarray
print '\n'
```

**Output:**

In the above program, a package in python called numpy is imported to make use of the array function to create new arrays. First, Nameofarray1 is the new array that is created using the array function in NumPy, whose elements are then displayed. Then Nameofarray2 is the new array that is created using the array function in NumPy, whose elements are then displayed. Then vector subtraction is performed on the two arrays nameofarray1 and nameofarray2 to subtract the first array elements from the second array.

#### Example #3

Python program to demonstrate NumPy vector to create two arrays using array function in NumPy and perform vector multiplication on the created two arrays:

**Code:**

```
#importing the package numpy
import numpy as nu
#creating an array using the array function and storing it in the variable nameofarray1
nameofarray1 = nu.array([[4,3,2,1]])
#displaying the elements of the newly created array
print 'The elements of the first array are:'
print nameofarray1
print '\n'
#creating an array using the array function and storing it in the variable nameofarray2
nameofarray2 = nu.array([[10,10,10,10]])
#displaying the elements of the newly created array
print 'The elements of the second array are:'
print nameofarray2
print '\n'
#using vector arithmetic to add the elements of the first array with the elements of the second array
vectorarray = nameofarray1 * nameofarray2
#displaying the elements of the vector array
print 'The elements of the vector array after multiplying the elements of the first array by the elements of the second array:'
print vectorarray
print '\n'
```

**Output:**

In the above program, a package in python called numpy is imported to make use of the array function to create new arrays. First, Nameofarray1 is the new array that is created using the array function in NumPy, whose elements are then displayed. Then Nameofarray2 is the new array that is created using the array function in NumPy, whose elements are then displayed. Then vector subtraction is performed on the two arrays nameofarray1 and nameofarray2 to multiply the elements of the first array and the second array.

### Conclusion

In this tutorial, we understand the concept of the NumPy vector in Python through definition, syntax, and working of the vector in python through programming examples and their outputs.

### Recommended Articles

This is a guide to NumPy Vector. Here we discuss the Working of the NumPy vector and Examples along with the codes and outputs. You may also have a look at the following articles to learn more –