The shortest answer is: it doesn’t define. Python is a high-level programming language and dynamic typing, you do not need to limit the size of your vector.
In fact, the type in Python that most resembles the vector of C is the list
. For you to set a new list
, just do:
>>> meuVetor = []
You can confirm the type of the variable by doing:
>>> print(type(meuVetor))
<class 'list'>
At this point it is interesting to remember that all in Python is object, so same as type list
is native, it is a class. You can check all vector methods using the function dir
:
>>> print(dir(meuVetor))
[..., 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
You can enter new values in the vector by the method append
:
>>> meuVetor.append(1)
>>> print(meuVetor)
[1]
>>> meuVetor.append(2)
>>> print(meuVetor)
[1, 2]
Theoretically you can do this indefinitely. The size of the vector will vary depending on its use. It grows and decreases by demand.
To access the elements, it is very similar to C:
>>> print(meuVetor[0])
1
>>> print(meuVetor[1])
2
You can initialize the vector with a predefined number of elements:
>>> meuVetor = [0]*5
>>> print(meuVetor)
[0, 0, 0, 0, 0]
But this is usually unnecessary in more basic applications.
To go through all elements of the vector, just use the for
:
>>> for(valor in meuVetor):
... print(valor)
0
0
0
0
0
This will work regardless of vector size.
If you need to at some point check what is the size of your vector, you can use the function len
:
>>> print(len(meuVetor))
5
>>> print(len([0] * 10))
10
For future references, while reading the documentation, keep in mind that type list
is mutable.
Got it, thanks a lot for your help Anderson
– Betinho