Python Forum

Full Version: Super newbie trying to understand certain concepts
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I am trying to understand the concept of what output this bit of code produces and why...

1st = [1, 2, 3]
for v in range(len(1st)):
    1st.insert(1, 1st[v])
print(1st)

I know this is child's play to most of you and I'm kind of embarrassed to even be asking this but I just don't understand why, if letters like "i" or "v" aren't predefined keywords...and they haven't been assigned values in this piece of code... how am I supposed to be able to know the answer to what will be the printed result?
There is no i in the code.
The v is defined by the for loop.
I found it hard to understand first as well
My definition of a for loop where it's for a in array the for loop will go through every object in array and each time, the object can be identified as a.

Here's an example
array = ['start', 1, 3, 2, 'finish']
for item in array:
    print(item)
Output:
start 1 3 2 finish
array = range(1, 26) #Returns all the number 1-25 (It doesn't include the 26)
for number in array:
    print(number)
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
I hope this helps
Thank you to all who took the time to reply. I will keep this in mind going forward.
(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,
In addition to newbieAuggie2019 thorough answer - print() is cheap way to understand what is going on:

In [1]: lst = [1, 2, 3] 
   ...: for v in range(len(lst)): 
   ...:     print(lst)                 # lst before insert
   ...:     lst.insert(1, lst[v]) 
   ...:     print(lst)                 # lst after insert
   ...:                                                                                         
[1, 2, 3]             
[1, 1, 2, 3]
[1, 1, 2, 3]
[1, 1, 1, 2, 3]
[1, 1, 1, 2, 3]
[1, 1, 1, 1, 2, 3]