+ 2
Hi, can anyone help me with this exercise?
nums = [1, 5, 4, 8] Complete the code to output the sum of the first and third values in the list print (nums[ ] + nums [ ])
18 ответов
+ 8
The first value would be 1, the third would be 4. But we need to remember that indexing starts at 0. Start counting from 0. What index do you get for 1 and 4?
+ 5
danish zubair,
you mentioned *dictionairies* in the context of indexing.
> indexing dictionaries is not posible in the way like using a list or a tuple.
additionally to this, your code is not running correctly: ( i assume that you did not run the provided code?)
> index [4] and [5] are out of range
> num[4] is an undefined variable name
+ 5
Refrain from giving solution-only comments and from copying already given answers.
Read the previous comments BEFORE POSTING IN THE THREAD.
+ 4
Furkan Yarız ,
since your *post* has no relation to the initial question, it can be seen as spam.
+ 4
Amit ,
since you are active in sololearn since a long time, you should be aware that saying *hi* in q&a section is not the right place to do this.
> please do not spam here!
+ 4
تاريخ الانضمام Pls do not post unrelated questions in other people's post. This is called thread hijacking. If you have a question, open a new post instead.
+ 3
Orin Cook "Oh, you can’t help that, we’re all mad here." ;)
+ 3
Meenuga Usharani ,
> if you have a coding related question, this is the right place.
> all other stuff can be posted in the communuty section.
+ 3
Belu mom Read the initial post of the thread before posting. Do not post codes or comments that are not related to the OP's question. Do not copy other user's replies.
+ 2
As Lisa mentioned, in list, and tuples indexes starts at 0, so the correct code would be:
nums=[1,5,4,8]
print(nums[0] + nums[5] + num[4])
I know there is an efficient way to do this, but I will not mention, as I don't want to confuse you.
+ 2
Thanks to all of you. The first number is zero as Zanish said while the second number is 2 because it asks what value the third number has ( 4)
+ 2
Belu mom Your answer has nothing to do with the question. Pls remove it or edit so it does.
+ 2
Belu mom Just read the question: it ask for the sum of first and third numbers from a list. Your code in question doesn't take a list nor calculates the sum.
+ 2
Belu mom BTW, your last answer has the same problem. Pls do not do it.
+ 1
So, the code means that nums is a list, and contains 4 numbers; 1, 5, 4 and 8.
The ubication of the numbers in the list is this:
Ubication Number
0 = 1
1 = 5
2 = 4
8 = 3
When you use the function print for a list, you need specifically the ubication of the valor that you want to print.
Then if the exercise wants print the sum of num 1 in the list and num 3 in the list the code is:
nums = [1,5,4,8]
Print(nums[0] + nums[2])
Output: 5
0
Y'all count weird, where I come from the third number is 3. Which makes it index 2.
0
So lemme keep it simple in order to print 1st and 3rd just use the code given below:
num = [1,5,4,8]
print(num[0]+num[2])
Explanation: as 1 is in the first index and we do not call the first index with 1, rather we start indexing from 0 so num[0], here, is the first value and continuing from it we add it with another value which is 4 and it lies in 3rd place so the index is 2. Hence, num[0]+num[2] prints out the value 5 which is the addition of first value(1) and third value(4).