In the Python language, as in many other programming languages, the index in the vectors starts at zero. However, it is unusual for you to give the user a list of options that starts at zero, usually starting at 1:
1 - Opção A
2 - Opção B
3 - Opção C
This is what happens in this case. Notice that the option variable has initial value 1 and the options are printed as follows:
1. DONUT # índice 0 de itens
2. LATE # índice 1 de itens
3. FILTER # índice 2 de itens
4. MUFFIN # índice 3 de itens
Notice that for choice X, its index is X-1? This is why -1 is next to the variable escolha
, within the index of the precos
variable. If the user chooses option 1, access index 0, if option 2, index 1, and so on.
Related question: Why is the index of arrays and other sequences starting at zero?
Terminology
By vector, the general concept of vectors in programming is applied: a list of variables of the same type, accessible through a single name, differentiated by an index (roughly, not to get into many details). That is, in Python, the simplest vector is implemented by data type: list
:
v = ["maçã", "banana", "laranja"]
We have a list of string
, accessed by the same name v
, differentiated by index, number referring to position: 0, for apple, 1 for banana and 2 for orange. At this point I mentioned earlier that the count does not start at 1, but at 0.
In addition, a string
by itself can be considered a vector of characters and indexing works in exactly the same way.
But what are these indexes?
Let's imagine that in our operating system, when we define a vector, its values are stored in sequence in memory:
| ... | <- Memória continua para cima
+-----------------+
| maçã |
+-----------------+
| banana |
+-----------------+
| laranja |
+-----------------+
| ... | <- Memória continua para baixo
When we create the instance of our vector, we store the memory address of the beginning of the vector in the variable that defines it. If we assume that our vector starts to be stored at address 120, we would have the following structure:
| ... | 119
+-----------------+
| maçã | 120
+-----------------+
| banana | 121
+-----------------+
| laranja | 122
+-----------------+
| ... | 123
When we do:
v = ["maçã", "banana", "laranja"]
The memory address of the v
variable would be 120, as it is where the vector starts. To access a vector value, the system simply adds the index value to the starting address. For example, to access the maçã
value, which is at index 0, the system would access memory at 120+0 = 120
position. For the banana
value, which is index 1, the system would access 120+1 = 121
. Finally, for the value laranja
, index 2, the system would access 120+2 = 122
.
This is how vector indexing works, explaining why numbering does not start at 1, but rather at 0, because to access the first position, the system sooner or later would have to subtract at 1 in the address to get the start of the vector and would be one more operation for the system to execute.
For string
, the logic is the same:
s = "Stack Overflow"
In memory, it would look like:
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
| S | t | a | c | k | | O | v | e | r | f | l | o | w |
+---+---+---+---+---+---+---+---+---+---+---+---+---+---+
a a+1 a+2 a+3 a+4 a+5 a+6 a+7 a+8 a+9 ...
If string
is stored in memory address a
, the first character will be in the address a
( s[0]
), the second in address a+1
( s[1]
), third in a+2
( s[2]
), and so on.