+ 2
let us given arraylist X=[1,3,7,24,6] .Write a program which give us elements from first to last.Help me please.
10 ответов
+ 3
You just have to write
X.sort()
+ 2
For examle in this case :let us given arraylist X=[1,3,7,24,6] .Write a program which give us elements from second to forth.Help me please.
+ 1
but when ask from second to fourth
+ 1
X.sort()
+ 1
clearefy terms for program. first element is 1, last is 6. just print it or print with for loop.
+ 1
you can take a slice then from the second to the fourth.
write X[1:4:1] will give you the slice from second to fourth elements.
I suggest you provide what you want the final answer to look like in your question.
Try this in code playground:
X = [1,3,7,24,6]
X.sort() #remove this line if you don't want to sort
print(X)
print(X[1:4:1])
+ 1
X[1:4:1] is what is called a slice in python. this code will give you the elements in X from second to the 4th. try the code I provided you in my previous answer to see
basically a slice is written as: X[index of first element you want:index of last element you want+1: step of slice]. In X[1:4:1], the index of the first element you want (2nd element) is 1, index of last element you want is 3 (add 1 and it's 4). step is 1. you can make step = 2 to skip one element each time in your slice. You can make it 3 to skip 2 elements each time...
0
thanks very much and the last question what do we mean ,write X[1:4:1] .what is it?
0
thanks for explaining .I understand everything.
0
for i in x:
print(i)