0

a,b,*c?

What is the output? a,b,*c=[x for x in range (0,12,3)] print (*c) Solution: 6 9 I know were asking Python to go from 0 to 12 by 3. Then take every third number *3? So the index is: 0-1-2-3-4-5-6-7-8-9-10-11-12 Every third number is (2*3=6), (3*3=9), so far so good. Why doesnt Python also print (8*3=24) and (11*3=33)? Any help appreciated:)

27th Mar 2019, 2:59 AM
tristach605
tristach605 - avatar
8 odpowiedzi
+ 4
The range function works a little differently than how you described it. What it does is start by 0, then move up by 3 until it reaches 12 (excluding it though). This leaves the array to be 0, 3, 6, and 9. Because variables a and b took the values 0 and 3 and c takes the rest (because of the *), the value of c becomes 6 and 9.
27th Mar 2019, 3:21 AM
Faisal
Faisal - avatar
+ 3
No problem at all d:
27th Mar 2019, 5:33 AM
Faisal
Faisal - avatar
+ 2
No need to apologize haha, programming can definitely be confusing at times :) The range function takes 3 parameters, a lower range, upper range, and step. As I mentioned in my previous reply, because the range is set from 0 to 12 and the step is 3, the first element will be 0 with each element following it be 3 steps up from the previous one. This leaves with an array of [0,3,6,9] (upper range is always left out). Then, the variables a, b, and c are set to different values of the list. a is set to the first element (0), b is set to the second element (3), and because c has the asterisk * before it, it is set to be a list of all remaining elements (although * can be used for multiplication, in this case it's used to indicate the rest of the element within the list). This leaves c to be [6,9] which is what ends up getting printed
27th Mar 2019, 5:05 AM
Faisal
Faisal - avatar
+ 2
Faisal! Thanks so much! Now I get it (I will review the unit on ranges and lists)👍🏻
27th Mar 2019, 5:31 AM
tristach605
tristach605 - avatar
+ 1
Sorry! One more question! Why would it be "6" and "9"? Shouldnt it be "6" (3*3 is 9) and "15 (5*3 is 15)? Sorry, Im totally lost:(.
27th Mar 2019, 4:57 AM
tristach605
tristach605 - avatar
+ 1
Also, I thught the * was a multiplication symbol? So 2 times 3, which is 6?
27th Mar 2019, 5:03 AM
tristach605
tristach605 - avatar
0
Thanks for the help, Faisal:).
27th Mar 2019, 3:33 AM
tristach605
tristach605 - avatar
0
Salut
6th Apr 2019, 4:01 AM
Amara Sangare
Amara Sangare - avatar