-2

I don't understand what the code does it works im happy but i wanna understand what is it doing , im total beginner ...

def raise_to_power(base_num, pow_num):
    result = 1
    for ind in range(pow_num):
        result = result * base_num
    return result

I don't understand this part of code python

for ind in range(pow_num):
    result = result * base_num
nathancy
  • 42,661
  • 14
  • 115
  • 137

5 Answers5

1

This part of the code:

for ind in range(pow_num):
    result = result * base_num

is just a for loop where ind is not actually used inside the loop.

Often such a loop would be written:

for _ in range(pow_num):
    result = result * base_num

using an underscore to mean a variable that is not used.

The range(pow_num) means that the loop will go round pow_num times.

The calculation: result = result * base_num just keeps multiplying by base_num, so eventually the power is calculated as others have pointed out.

For example, calling raise_to_power(2, 3) would mean that result would start at 1 and be successively multiplied by 2 a total of 3 times:

result = 1
result = 2
result = 4
result = 8
quamrana
  • 37,849
  • 12
  • 53
  • 71
0

It is a power function raise_to_power(a,b) it will give you a to power b (a^b). It will multiply a , b times. raise_to_power(a,b) = a*a......*a

Sina
  • 270
  • 1
  • 23
0

The best way to understand a code is to Particularize it.

def raise_to_power(4, 3):   # base_num = 4 , power_num = 3   
result = 1                  
for ind in range(3):
    result = result * 4
return result

So result = 1 initially. Then a loop is run 3 times (power_num times). In that loop, the value of result is updated. So result becomes its previous value (here 1) multiplied by the base_num i.e. 4. So result=4

In second iteration, again result is updated to result = 4 x 4 = 16 In third and final (here 3 iterations), result becomes result = 16 x 4 = 64

So overall this function is calculating the pow_num power of base_num

MNK
  • 634
  • 4
  • 18
0

The first line states the function raise_to_power with two variables, base_num and pow_num.

The next line is a starting point (1). The function should only return integers (numbers that are not decimals) and not a float (numbers that contain decimals). Any number times 1 is itself which is important to know for this code.

The next line proposes a qualified statement (when this happens... do that). Here when you have a pow_num (exponent). It will multiply the base_num by itself for pow_num of times.

This function calculates the base_num ^ pow_num.

Alex Pandar
  • 31
  • 1
  • 7
0

well, all it is really doing is taking the base number and putting it to the power of something then returning the result so for example

say the base_num is 5 and pow_num is 3. You have to set the result to 1 to start off because obviously if you set it to 0 since its multiplication your answer would always be 0.

So, then it takes your base_num loops through and multiplies it the number of times equal to your pow_num.

In this case it would take your 5 multiply it by the result which is 1 so you would be left with 5 which is what you want the base_num to be then it takes that 5 and multiplies it the number of times you selected for your power_num which was 3 so it goes 5*5*5 to get your a result of 125 which is then returned so you can do whatever you want with

Brent
  • 740
  • 1
  • 5
  • 11