The code given has a syntax error as list
objects do not have a property len
. It should be len(alst)
instead of len[alst]
.
To assign the value of the last element of the list alst
to the variable val
, you can use negative indexing. The last element of the list can be accessed using the index -1
. So, the corrected code would be:
main.py64 chars4 lines
Here, -1
points to the last element of the list alst
. The value of the last element is then assigned to the variable val
. The output is 6
, which is the value of the last element.
gistlibby LogSnag