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

Show parent comments

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.

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