def manualReverse(list):
return list[::-1]
def reverse(list):
return list(reversed(list))
list = [2,3,5,7,9]
print manualReverse(list)
print reverse(list)
I just started learning Python. Can anyone help me with the below questions?
1.How come list[::-1] returns the reversed list?
2.Why does the second function throw me NameError: name 'reverse' is not defined?
reverseis a local name insidemanualReverse. Un-indent it.def reverseso it lines up withdef ManualReverse- otherwise, you're defining a function inside a function.... indentation is important in Pythonlist. That will override the builtin namelist, and indeed will cause an error in your code above once you fix the indentation.