Hi, can anyone help me with this exercise? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 [ ])

9th Jun 2023, 8:50 PM
Rossella Solaro
Rossella Solaro - avatar
18 Answers
+ 7
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?
9th Jun 2023, 8:57 PM
Lisa
Lisa - avatar
+ 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
10th Jun 2023, 10:57 AM
Lothar
Lothar - avatar
+ 5
Refrain from giving solution-only comments and from copying already given answers. Read the previous comments BEFORE POSTING IN THE THREAD.
11th Jun 2023, 3:15 PM
Lisa
Lisa - avatar
+ 4
Furkan Yarız , since your *post* has no relation to the initial question, it can be seen as spam.
11th Jun 2023, 12:05 PM
Lothar
Lothar - avatar
+ 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!
11th Jun 2023, 12:09 PM
Lothar
Lothar - avatar
+ 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.
15th Jun 2023, 9:33 AM
Emerson Prado
Emerson Prado - avatar
+ 3
Orin Cook "Oh, you can’t help that, we’re all mad here." ;)
10th Jun 2023, 7:19 AM
Lisa
Lisa - avatar
+ 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.
11th Jun 2023, 12:14 PM
Lothar
Lothar - avatar
+ 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.
12th Jun 2023, 11:35 AM
Lisa
Lisa - avatar
+ 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.
9th Jun 2023, 10:10 PM
Danish Zubair
Danish Zubair - avatar
+ 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)
10th Jun 2023, 7:36 AM
Rossella Solaro
Rossella Solaro - avatar
+ 2
Belu mom Your answer has nothing to do with the question. Pls remove it or edit so it does.
12th Jun 2023, 12:10 AM
Emerson Prado
Emerson Prado - avatar
+ 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.
12th Jun 2023, 11:36 AM
Emerson Prado
Emerson Prado - avatar
+ 2
Belu mom BTW, your last answer has the same problem. Pls do not do it.
12th Jun 2023, 11:37 AM
Emerson Prado
Emerson Prado - avatar
+ 2
Belu mom This thread is about a particular Python task. Do not post unrelated codes here.
13th Jun 2023, 5:00 PM
Lisa
Lisa - avatar
+ 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
11th Jun 2023, 5:35 PM
Aquiles Moyón
Aquiles Moyón - avatar
0
Y'all count weird, where I come from the third number is 3. Which makes it index 2.
10th Jun 2023, 5:17 AM
Orin Cook
Orin Cook - avatar
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).
11th Jun 2023, 1:38 PM
Satyam Xtha
Satyam Xtha - avatar