+ 7

why does all these code lines print the same?

help me understand pls. https://code.sololearn.com/cRNoy5C1NaJV/?ref=app

23rd Sep 2020, 7:47 AM
Twilight🌅
Twilight🌅 - avatar
3 Respostas
+ 8
Please keep in mind that slicing can be a bit weired: # 2. slice has descending order of start / stop and a therefor a negative step print(I[1:3] + I[5:1:-1]) # res = [2, 3, 5, 4, 3] # 2. slice has ascending order of start / stop and a positive step print(I[1:3] + I[1:5:1]) # res = [2, 3, 2, 3, 4, 5]
23rd Sep 2020, 10:59 AM
Lothar
Lothar - avatar
+ 8
Try running this variant to your code, I think it will answer your question # why does all these code print the same? #Run the program I = list((1,2,3,4,5)) print(I[5:1]) I = list((1,2,3,4,5)) print(I[4:2]) I = list((1,2,3,4,5)) print(I[3:2]) I = list((1,2,3,4,5)) print(I[5:2]) # but, if the numbers(list indentations) go from low to high it works in normal known way. I = list((1,2,3,4,5)) print(I[1:3] + I[3:5])
23rd Sep 2020, 7:52 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 6
If you want to go from higher to lower you have to specify the step in negative like that I[5:3:-1]
23rd Sep 2020, 7:50 AM
Ruba Kh
Ruba Kh - avatar