0

I can't understand reverse method. Help please

x.reverse() x = [1, 2, 6.06, 9] print(x.reverse(x)) Why it's not right? How it to do?

20th May 2020, 10:19 PM
Blokhin Roman
Blokhin Roman - avatar
2 Respuestas
+ 4
Reverse is actually changing the array and returning nothing, so you get None. You can call it upfront and print(x). But I guess what you are looking for is: print(x[::-1]) It returns all elements in reversed order. [from:to:iterationOffset]
20th May 2020, 10:32 PM
Manu_1-9-8-5
Manu_1-9-8-5 - avatar
+ 4
Ok, let me discribe it. both x.reverse() and reversed(x) works for strings and lists. first remember, x.reverse() is a method but reversed(x) is a function. So, reverse does not return while reversed returns! x = "Hello Roman!" x.reverse() #It has no argument. print(x) output: !namoR olleH x = "Hello Roman!" print(reversed(x)) output: !namoR olleH Hope it helped. You can also check docs.python.org/3/ they have it all for you.
20th May 2020, 10:55 PM
M Tamim
M Tamim - avatar