Anyone care to explain? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Anyone care to explain?

https://imgur.com/a/gbAd2Wd

25th Jun 2022, 7:59 PM
Hubert Huang
2 Answers
+ 2
a = [2, 4, 5] a is a list, and a[0] = 2, a[1] = 4, and a[2] = 5 lists are mutable, their elements can be accessed by their indices, and can be assigned new values. for i in range(1, 3): a[i] = a[i-1] print(a[i], end ='') means, do the indented operations as msny times as specified by range, when iteration = 1, assign a[1], which is set currently to 4, the value that a[1-1] or a[0] has. so a[1] = 2 print this new a[1], spare no space 2 gets printed so if you printed a, at this stage, you'd have: [2, 2, 5] get back to the top of the for loop, i = 2, a[2] = a[2-1] = a[1] = 2 print a[2], without sparing any space in the end 22 (gets printed next to 2, normally it would have printed on a new line, without end = '') if printed you'd see that a has become: [2, 2, 2] the loop stops here as that's the way intervals are designed in the program. (starts with, ends before) so (1, 3) just tells you that you start off with 1, you iterate by 1 unless you explicitly code otherwise, you stop after 2.
25th Jun 2022, 10:04 PM
Korkunç el Gato
Korkunç el Gato - avatar
+ 1
Please do not post image urls for codes in Discussions. Please just do what everyone else does, which is begin to learn how to use the app. My own post gave me a visual trauma so I truncated it to this. All the best.
25th Jun 2022, 10:13 PM
Korkunç el Gato
Korkunç el Gato - avatar