+ 1

Python question

What is the result of this code? nums = [9, 8, 7, 6, 5] nums.append(4) nums.insert(2, 11) print(len(nums)) Hello SOLOLEARN community, First of all thank you for taking the time to read my question and for replying. I am a little confused on this code right here. The answer is it will print 7 number, but what I am confused on is when it inserts (2, 11) it did not use the index= to assign it to any specific spot within the nums list. So why is the answer 7 if it is inserting (2, 11) and it appends 4 with those 3 numbers + 5 numbers already in the in the list the answer should of been 8. Any help explaining this would greatly appreciated. Thank you all once again. Sincerly, Noobie Coder

21st Nov 2018, 2:56 PM
Issac Wiggins
Issac Wiggins - avatar
6 Answers
+ 2
Ah I see. I thought when you said "index=" in your question, you meant "nums.insert(index=2, 11)". If you haven't learned about keyword arguments yet, you can ignore the first answer. ------------- When you say `index = 1`, Python creates a variable called 'index', which evaluates to '1' anywhere it is used. So if I say: print(index) The variable first gets evaluated, then gets passed into the function. So what we're really saying is: print(1) ------------- The same applies when you call the insert method. This: words.insert(index, "is") Evaluates to this: words.insert(1, "is") Which is why it inserts "is" before index 1. ------------- There's nothing special about using the variable 'index' or really any variable at all. What matters is that the first argument evaluates to 1. >>> words.insert(1, "is") >>> my_cats = 1 >>> words.insert(my_cats, "is") >>> words.insert((5**2 - 4**2)//3**2, "is") >>> import math >>> words.insert(int(-(math.e**(math.pi*(1j))).real), "is") All of the above examples evaluate to the same thing. ------------- Python doesn't automatically know "is" isn't an index number because it's a string, it knows so because it's in the second position and not the first. Whatever argument you give in the first position (expressed using variables or not) is what represents the index number, and whatever you give in the second position is the object that is put into the list. (Hence the name 'positional arguments') ------------- Let me know whether or not this helped :)
21st Nov 2018, 4:13 PM
Just A Rather Ridiculously Long Username
+ 3
Hi Issac, I think you might have confused between positional arguments and keyword arguments. Here's how the two are different ------------- When you define a function like so: def add(a, b): ... The function takes in two *positional* arguments. That means you would call the function like `add(1, 2)` and NOT like `add(a=1, b=2)`. If you wanted to use the function in the second way, you would have to define the function to take *keyword* arguments: def add(**kwargs): ... ------------- The 'insert` method is defined in the first way, so whatever number you give as your first argument is the index before which it will insert your object. >>> nums = [0,1,2,3,4,5] >>> nums.insert(2, 'cat') >>> nums [0, 1, 'cat', 2, 3, 4, 5] >>> nums.insert(0, 9001) >>> nums [9001, 0, 1, 'cat', 2, 3, 4, 5] ------------- Hope this helped, let me know if i lost you anywhere :)
21st Nov 2018, 3:15 PM
Just A Rather Ridiculously Long Username
+ 1
@Just a Rather Ridculously Long Username It definitely helped a ton thank you. Faisal thank you for your answer as well.
21st Nov 2018, 6:51 PM
Issac Wiggins
Issac Wiggins - avatar
+ 1
No problem, cheers
21st Nov 2018, 7:05 PM
Just A Rather Ridiculously Long Username
0
The insert function will insert a new value in a list at the index specified (the first number in the brackets). In your code, it's set to insert the int 11 at the index of 2 (being placed right after 8), and having 4 being appended to the list will result in the array having a length of 7.
21st Nov 2018, 3:12 PM
Faisal
Faisal - avatar
0
@Just A Rather Ridiculously Long Username Thank you for taking the time explain that to me. I did not get lost at all during your explanation it answered my question really well. I guess what I am lost on is why is in the previous question it had words = ["Python", "fun"] index = 1 words.insert(index, "is") print(words) which uses the index = 1 then uses word.insert(index."is") to insert the word "is" into the list. Does Python automatically know "is" is not an index number since its in a String format?
21st Nov 2018, 3:23 PM
Issac Wiggins
Issac Wiggins - avatar