Python Forum
Super newbie trying to understand certain concepts
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Super newbie trying to understand certain concepts
#1
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?
Reply
#2
There is no i in the code.
The v is defined by the for loop.
Reply
#3
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
Reply
#4
Thank you to all who took the time to reply. I will keep this in mind going forward.
Reply
#5
(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,
newbieAuggie2019

"That's been one of my mantras - focus and simplicity. Simple can be harder than complex: You have to work hard to get your thinking clean to make it simple. But it's worth it in the end because once you get there, you can move mountains."
Steve Jobs
Reply
#6
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]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  super() in class akbarza 1 403 Dec-19-2023, 12:55 PM
Last Post: menator01
  super newbie question: escape character tsavoSG 3 2,403 Jan-13-2021, 04:31 AM
Last Post: tsavoSG
  superclass and super() grkiran2011 1 1,693 Jun-20-2020, 04:37 AM
Last Post: deanhystad
  MRO About super() catlessness 1 1,998 Jan-12-2020, 07:54 AM
Last Post: Gribouillis
  Super with Sublime Text - TypeError: super() takes at least 1 argument (0 given) Shafla 6 7,298 May-04-2019, 08:30 PM
Last Post: Shafla
  Is any super keyword like java rajeev1729 2 3,243 Sep-14-2017, 07:47 PM
Last Post: snippsat
  Multiple Inheritance using super() Sagar 2 7,245 Sep-08-2017, 08:58 AM
Last Post: Sagar

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020