
上QQ阅读APP看书,第一时间看更新
Vector
A vector is a single dimension structure to store data. They are one of the most popular data structures in Python. There are two ways of creating vectors in Python as follows:
- Using a Python list: The simplest way of creating a vector is by using a Python list, as follows:
>>> myVector = [22,33,44,55]
>>> print(myVector)
[22 33 44 55]
>>> print(type(myVector))
<class 'list'>
Note that this code will create a list with four elements.
- Using a numpy array: Another popular way of creating a vector is by using NumPy arrays, as follows:
>>> myVector = np.array([22,33,44,55])
>>> print(myVector)
[22 33 44 55]
>>> print(type(myVector))
<class 'numpy.ndarray'>
Note that we created myVector using np.array in this code.
In Python, we can represent integers using underscores to separate parts. It makes them more readable and less error-prone. This is especially useful when dealing with large numbers. So, one billion can be represented as a=1