+ 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.

22nd Jun 2016, 6:59 PM
Meri
Meri - avatar
10 Antworten
+ 3
You just have to write X.sort()
22nd Jun 2016, 7:12 PM
Jehan Milleret
Jehan Milleret - avatar
+ 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.
22nd Jun 2016, 8:51 PM
Meri
Meri - avatar
+ 1
but when ask from second to fourth
22nd Jun 2016, 7:15 PM
Meri
Meri - avatar
+ 1
X.sort()
22nd Jun 2016, 7:43 PM
Koffi Yao
Koffi Yao - avatar
+ 1
clearefy terms for program. first element is 1, last is 6. just print it or print with for loop.
22nd Jun 2016, 7:49 PM
Olexander Gerasymenko
Olexander Gerasymenko - avatar
+ 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])
22nd Jun 2016, 10:18 PM
Koffi Yao
Koffi Yao - avatar
+ 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...
22nd Jun 2016, 11:14 PM
Koffi Yao
Koffi Yao - avatar
0
thanks very much and the last question what do we mean ,write X[1:4:1] .what is it?
22nd Jun 2016, 11:00 PM
Meri
Meri - avatar
0
thanks for explaining .I understand everything.
23rd Jun 2016, 6:47 AM
Meri
Meri - avatar
0
for i in x: print(i)
12th Jan 2017, 4:41 PM
rasoul norouzi
rasoul norouzi - avatar