(Oct-27-2019, 04:43 PM)bbragg311 Wrote: [ -> ]1st = [1, 2, 3]
for v in range(len(1st)):
1st.insert(1, 1st[v])
print(1st)
Hi!
Actually it seems that the code is actually:
lst = [1, 2, 3]
for v in range(len(lst)):
lst.insert(1, lst[v])
print(lst)
where all your
1st
should be
lst
. In some keyboards
'1'
and
'l'
are almost indistinguishable. And why should it be 'l' and not '1'? Well, because names of variables like the list [1, 2, 3] cannot start with a number. It is a rule while naming variables in python. (You can try with
1st
as you provided in the post, and then you will be shown a SyntaxError, because it breaks the naming rule of variables).
Naming Variables: Rules and Style
The naming of variables is quite flexible, but there are some rules you need to keep in mind:
- Variable names must only be one word (as in no spaces).
- Variable names must be made up of only letters, numbers, and underscore (_).
- Variable names cannot begin with a number.
Variables can represent any data type, not just integers:
my_string = 'Hello, World!'
my_flt = 45.06
my_bool = 5 > 9 #A Boolean value will return either True or False
my_list = ['item_1', 'item_2', 'item_3', 'item_4']
my_tuple = ('one', 'two', 'three')
my_dict = {'letter': 'g', 'number': 'seven', 'symbol': '&'}
https://www.digitalocean.com/community/t...n-python-3
for v in range(len(lst)):
lst
is the name that somebody has given to the list
[1, 2, 3]. It could have been anything else, as far as you respect the 3 rules above. In this case
lst
was short for
list
, word that shouldn't be used as a name, because
list
is a python keyword.
(len(lst))
calculates the length (len) of the list named
lst
, that is to say, 3, because the list named
lst
has 3 elements: 1, 2, 3.
range(len(lst))
means therefore, in this case,
range(3)
. The function
range()
can take for instance, the form
range(0, 3)
, that in this case, are equivalent to each other. Lists and other concepts like strings, start with a position 0, then 1, then 2, etc. (Not like normal people start counting on '1'). The function
range()
also has the peculiarity that it doesn't take into consideration the last position (3), so both
range(3)
and
range(0, 3)
means numbers 0, 1, and 2.
People use arbitrary names for all kind of things in python, although some might have a logic origin. So 'v' actually is short for 'value', but it could have been named anything else. So
for v in range(len(lst)):
means that for the values in range 0, 1, and 2 do whatever is after the colon (:). And that 'whatever' is:
lst.insert(1, lst[v])
That line will insert '1' (the first part of insert(1, lst[v])) to the list, more specifically, on the position in the list given by the value (v). Originally the list named lst =[1, 2, 3] has the next positions and values:
lst[0] = 1
lst[1] = 2
lst[2] = 3
So for the case of '0' (remember
for v in range(len(lst)):
in this case, is the same as
for v in range(3):
and
for v in (0, 1, 2):
; therefore, it takes one by one every one of those numbers: 0, 1, 2), it makes
lst.insert(1, lst[v])
to be translated as
'insert '1' in the list named lst in the position [0]'
. That is to say, that
lst
will be modified to [1, 1, 2, 3].
Similarly, for the case of '1' (remember
for v in range(len(lst)):
in this case, is the same as
for v in range(3):
and
for v in (0, 1, 2):
; therefore, it takes one by one every one of those numbers: 0, 1, 2), it makes
lst.insert(1, lst[v])
to be translated as
'insert '1' in the list named lst in the position [1]'
. That is to say, that
lst
will be modified to [1, 1, 1, 2, 3].
And lastly, for the case of '2' (remember
for v in range(len(lst)):
in this case, is the same as
for v in range(3):
and
for v in (0, 1, 2):
; therefore, it takes one by one every one of those numbers: 0, 1, 2), it makes
lst.insert(1, lst[v])
to be translated as
'insert '1' in the list named lst in the position [2]'
. That is to say, that
lst
will be modified to [1, 1, 1, 1, 2, 3].
The last line,
print(lst)
simply prints that list named
lst
after all those modifications:
Output:
[1, 1, 1, 1, 2, 3]
I hope this explanation helps to understand it a bit better.
All the best,