Exponent function


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

print (raise_to_power(2,2))

I have a few questions:

  1. I don’t understand how this for loop statement works
  2. why is the return statement, not indented in the same line as the result statement above, l expected the code not to change but it doesn’t work, please explain

Thanks

You should leverage print statements to learn how the basic blocks work. The ability to do this effectively will help you with more advanced structures as well.

A useful example in this case:

print(f"index: {index}\nresult: {result}")
1 Like

I don’t understand how the for statement raises the power?

Did you put in the print statement?
You can craft your own formatted string print statement to ask the code specific questions like that as well.

A for-loop doesn’t do anything other than iterate through an iterable data structure (lists and tuples mainly). That’s a fancy way of saying if you have a list of length n, a for loop will run a code block n times. When you loop over a range you’re explicitly giving it the amount of times it will loop.

The code block inside the loop is what’s affecting the code. Try printing the result before and after it’s affected.

result = result * base_num

how will the above line of code raise the power? To me it is simply multiplying two numbers

This bit l don’t understand

def raise_to_power(base_num, pow_num):
    result = 1
    print(f"base: {base_num}, exponent: {pow_num}")
    for index in range(pow_num):
        print(f"index: {index}\nresult: {result}")
        print(f"result is being set to = {result} * {base_num}")
        result = result * base_num
        print(f"result is now: {result}")
    return result

print (raise_to_power(2,3)

this yields:

base: 2, exponent: 3
index: 0
result: 1
result is being set to = 1 * 2
result is now: 2
index: 1
result: 2
result is being set to = 2 * 2
result is now: 4
index: 2
result: 4
result is being set to = 4 * 2
result is now: 8

Note that to raise something to an exponent x^n means to multiply x by itself n times.
So x^4 = x * x * x *x

But please note that the most important take-away from this is not how the exponent function worked, but rather the process (methodology) in trying to decipher how it worked. This process is much more useful in terms of future problem solving and programming.

wow great explanation, l understand it now, but your code is abit complex for me you have used f strings

I had another question why is it wrong to indent the return statement instead of having it in the same level as the for statement?

Many Thanks


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

print (raise_to_power(2,2))

when indented like this it yields


C:\Users\simmy\PycharmProjects\app\venv\Scripts\python.exe C:/Users/simmy/.PyCharmCE2018.2/config/scratches/exercise.py
2

Process finished with exit code 0

I recommend getting either comfortable with f-strings or formatted strings "example {}".format(some_variable) as they are really good ways to debug clearly.

In terms of why it doesn’t work to indent the return statement, it’s because the loop will end immediately on the first iteration, so it only completes one cycles of its promised cycles.

Here’s a video on f strings: Python Quick Tip: F-Strings - How to Use Them and Advanced String Formatting - YouTube

thank you so much really appreciate, you are really helpful, thanks for the video

1 Like

does visual studio allow us to write code for the latest python version, l couldn’t download pycharm as l am using a 32 bit system?

You might want to look at this: Visual Studio Code (Windows) - Setting up a Python Development Environment and Complete Overview - YouTube

will do that

many thanks