Vector programming is a programming paradigm that focuses on using vector operations to manipulate data. In vector programming, operations are applied to entire data vectors at once, rather than to individual elements of the vector. This allows for more efficient and parallelizable code and easier and more expressive data manipulation.
Vector programming languages, such as MATLAB and R, provide built-in support for vector operations and data structures and high-level functions for common vector operations such as element-wise addition and multiplication. This makes it easier for developers to write vectorized code, which can be more efficient and expressive than using loops to manipulate vectors.
Let’s understand what is a vector in Python and how can we create and use it.
What is a Vector in Python?
A Vector in Python is a data structure representing a sequence of elements. It can store numeric data, but it can also store other data types, such as strings. Python does not have a built-in data structure like Vector. However, several libraries, such as the numpy library, provide vector data structures.
The elements in a vector are accessed by their index, which is a numerical value that indicates their position in the sequence. For example, if you have a vector called v, you can access the first element in the vector using the syntax v, the second element using v, and so on.
Implementation of Vector in Python
Use the numpy library to create vectors in Python.
import numpy as np # Define two vectors as arrays of numbers vector_1 = np.array([11, 21, 31]) vector_2 = np.array([41, 51, 61]) # printing two vectors using print() function print(vector_1) print(vector_2)
[11 21 31] [41 51 61]
We used the np.array() function to create two vectors and the print() function to print those two vectors.
A vector can be as simple as a single-dimensional array. It is a one-dimensional array of lists. It contains elements similar to that of a Python list.