def pop(n):
result,counter = 0,0
while counter<=n:
result=(2**counter)
counter=counter+1
return result
example:
>>>pop(4)
16
How do i return all the results? like:
1
2
4
8
16
You can store the result in an list:
def pop(n):
result,counter = [],0
while counter<=n:
result.append(2**counter)
counter=counter+1
return result
Now the result will be a list of all powers.
Alternatively, you can create a generator to yield multiple results
def pop(n):
result,counter = 0,0
while counter<=n:
yield 2**counter
counter=counter+1
Now if you do a list(pop(4)), then you will get a list of all results
yield instead of yeild :)The pythonic way would be something like this:
def pop(n): return [2**x for x in range(n)]
mylist = [1,2,3,4],mylist.pop()will work? What are you getting at? Am I using the wrong words? cause it is a valid python function... :Spopis not a "builtin" function. it is a method to thelistclass, but defining your ownpopfunction will not shadow that, and so is not a problem.