Home > Back-end >  How this nesting for loop works in Python?
How this nesting for loop works in Python?

Time:10-18

I can't understand the second for loop:

for k in range(2, n//2   1):
    print(n%k, end=" ")

How did this formula produce such result?

enter image description here

CodePudding user response:

range() has multiple parameters, first is start, second is stop, third is step (which is not present in this example, so the default value is used: 1).

So your range will generate numbers from 2 to n//2 1.

To learn more about range, and for loops:

https://www.w3schools.com/python/ref_func_range.asp https://www.w3schools.com/python/python_for_loops.asp

CodePudding user response:

The second for loop prints the reminders of dividing n by all of the numbers from 2 to n/2.

check the documentation of range

  • Related