简体   繁体   中英

Can anyone explain this in detail?

num = 5

result = 1

for iterator in range(1, num+1):

 result = result * num

print(result)

I am new to python and don't understand what is going on with this problem. What does (1,num+1) represent and how is the result being calculated?

range(start, stop, step) you are starting your iteration at 1, and stopping it at (num + 1) = 6 run this code and you will see how it is working. You are calculating one result, and then multiplying that result by num again, 5 times.

num = 5

result = 1

for iterator in range(1, num+1):
 result = result * num
 print(result)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM