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?
2 Respostas
+ 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]
+ 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.