Code explanation1


#1



def power(x, y=2):

    r = 1
    
    for i in range(y):
       r = r * x
       
    return r

print power(3)
print power(3, 3)
print power(5, 5)

kindly explain how this code works am unable to get what the iteration is doing in this code.this is example of implicit value in a function.kindly explain in simple lucid way.


#2

the loop iterates y times (depending on the value of y)

we only use the loop here to get the desired number of iterations


#3

but in our function why we have then defined y=2, if later on when we are calling function we have increased the number i.e more than 2. AM unable to get this logic. could you further elaborate the logic behind it.


#4

that logic, its basicly a fall back.

it means that y=2 will be two if no second argument is supplied like here:

print power(3)

if you do supply a second argument like here:

print power(5, 5)

y will be 5


#5

the y=2 at the start means that the y argument is optional, and if it is not used then 2 is used as the default value.
e.g

>>> print power(3) # Although the y arguement has not been entered, 2 is used by default.
9
>>> print power(3,2)
9

#6

ok that solves my one of the doubts. but then why we are fixing value of r here(r=1).what does r is contributing to this function.


#7

Before we continue, why do you need to know? I am not helping you cheating, right?

well, we want to get the x to the power of y, so for example if we want print power(3, 3) we need to multiply x*x*x, but we need a starting point, in this case r, we give it a value of 1, because anything multiplied with one will be the same value

this function is a bit pointless by the way, we could simply shorten this:

def power(x, y=2):
     return x ** y

#8

sir am not cheating or anything. am just following the course at codeacademy.com
am just asking for help/explanation to clear my doubt.this code is on the article recommended to read in functions(15 min) heading.


#9

thanks for your kind explanation. your point is valid (that here we could have used power function).But am just started learning python.it is always good to expand your horizon by knowing various ways to solve a particular problem.


#10

I just wanted to be sure, sometimes students cheat by asking questions here in the hope there teacher doesn't find out :wink: Just wanted to be sure i wasn't someone helping to cheat, i hope you can understand

absolutely true :slight_smile: Do you now understand the loop?

Which exercise is this then? Named arguments (y=2) is not part of python course so far i know


#11

i understand sir , but still little confusion is there.Am not able to understand how does loop is calculating def power(3,3).how does the iteration is going , that am not getting.how it is adding the result.


#12

range() allows us to loop an y amount of times

then we multiply x with r ytimes, each iteration of the loop we store the result ofr * xinr, so that r will increase:

def power(x, y=2):

    r = 1
    
    for i in range(y):
       r = r * x
       print r
       
    return r
print power(5, 5)

best way to see, is simply to print r every iteration of the loop

even better would be to run your code here:

http://www.pythontutor.com/visualize.html#mode=edit

it will run you through your code step for step


#13

thanks sir for yur kind guidance.


#14

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.