[EdLUG] Learning python
Matthew Rankine
matthew at mrankine.com
Sun Apr 11 16:52:33 UTC 2021
*Why does .reverse(0 not work as a list method on line 5*
Your code assumes the list.reverse() method returns a reversed list, but in
fact the method reverses the list variable in-place and does not return
anything. So, to print a reversed list, instead do:
list1.reverse()
print(list1)
*and then [not work] as a stand alone function on line 7 ?*There is no
built-in Python function called reverse(). There is the list.reverse()
method set out above, and there is the reversed() builtin function,
which returns a reversed iterator. You could use reversed() like so:
print(list(reversed(list1)))
More info: https://dbader.org/blog/python-reverse-list
Have fun with Python!
On Sun, 11 Apr 2021 at 16:40, Andrew Smith <asmith9983 at gmail.com> wrote:
> Guys
> I have a Raspberry Pi and after some thought, decided I should learn
> Python as it's the language most used in projects around it,and also in
> data science.
> Knowing C++, and Haskell, I thought it would be simple. *Hello World*
> was no problem, but lists have been less so.
>
> I've attached my test code. I Had to comment out lines 5 & 6 to get any
> output.
> Why does .reverse(0 not work as a list method on line 5, and then as a
> stand alone function on line 7 ?
> Andrew
> --
> EdLUG mailing list
> EdLUG at mailman.lug.org.uk
> https://lists.edlug.org.uk/mailman/listinfo/edlug
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.edlug.org.uk/pipermail/edlug/attachments/20210411/6d4d3659/attachment-0001.html>
More information about the EdLUG
mailing list