r/pythonhelp • • Aug 31 '21

HOMEWORK Help with Python exponential function

So, I started my coding adventure yesterday and started from "4 hours full course for beginners of python". I had problems understanding how below code works, it's exponential function and I can't really understand It.After defining a function, we make container called "result" where we store below list of numbers and I can't understand how its gonna make 2^3=8? How can list of numbers be multiplied by 2? Someone can help?

3 Upvotes

8 comments sorted by

View all comments

5

u/sentles Aug 31 '21

The variable result is an integer, not a list. That integer starts at 1.

The loop that is run afterwards iterates through range(pow_num). This is similar to a list, which contains every integer starting with 0 and ending with pow_num-1. You showcased this with the code below, where you run a loop iterating through range(3). In the first iteration, index contains 0, in the second it contains 1 and in the third, 2, thus the output.

In the example posted, range(pow_num) is only used to run the loop exactly pow_num times. The values that index contains each iteration are never used.

The variable result starts at 1. Each iteration, result is multiplied by base_num (and the result is saved in result again). Since there will be a total of pow_num iterations, in total, result will be multiplied by base_num exactly pow_num times. Because it starts at 1, the final result will be 1*base_num*...*base_num = base_numpow_num.

The below table showcases the value of result after each iteration, if we run raise_to_power(2, 3):

Current IterationOperationresult
0result = 11
1result = result*base_num = 1*2 = 22
2result = result*base_num = 2*2 = 44
3result = result*base_num = 4*2 = 88

Remember that this stops after the third iteration because it is iterating through range(3), which is similar to [0, 1, 2], so it will run once for each number, for a total of three iterations.

2

u/Neat_Chip4684 Aug 31 '21

After each iteration integer "result" gets multiplied and after each multiplication it changes from 1 to 2, from 2 to 4 and from 4 to 8?

3

u/sentles Aug 31 '21

Yes, in this example.

4

u/Neat_Chip4684 Aug 31 '21

Thank you so much. I got a headache from so much information about programming and writing first code that barely works. You help me sleep better that night😀

1

u/[deleted] Nov 15 '22

so if i were to say for index in range(5): print(index)

would the “current iteration” start at 0 and run all the way to 5.

and the Operation would result = 1 then on to 2,4,8,32 ? i’m starting out to and i’m trying to understand

2

u/sentles Nov 15 '22

range(5) is a range object containing [0, 1, 2, 3, 4], a total of 5 integers. If you were to run raise_to_power(2, 5), the result would be 32 because on each iteration, result is multiplied by 2. Since it starts at 1 and we have a total of 5 iterations (from i=0 up to and including i=4), the final value saved in result is going to be 1*2*2*2*2*2, or 32.

1

u/[deleted] Nov 15 '22

oh okay! so that was the correct format ? yes

0

u/[deleted] Apr 07 '23

[deleted]

1

u/[deleted] Jun 29 '23

you should’ve been swallowed