r/PythonLearning • • 3d ago

i am stuck with the range function

sorry but i am gonna crash out because of the range function i cant understand it well to use it this is driving me crazy any one could give me a video explaining everything i dont mind it being an hour long

8 Upvotes

32 comments sorted by

View all comments

Show parent comment

1

u/Repulsive-Song-1594 3d ago
ni = [2, 4, 6, 9, 12]

running_total = ni[0]
print(f"start: {running_total}")

for i in range(1, len(ni)):
    prev = running_total
    running_total = running_total + ni[i]
    print(f"{ni[i]} + {prev} = {running_total}")

the ai i just asked now showed me this too but after i told him again he showed me the right code
thats the part of range combining it with other things thats its hard for me to understand

1

u/arivictor 3d ago
start = 2
stop = 20
step = 2
for i in range(start, stop, step):
    print(i)

range uses start, stop, step. start is the number you want to start with in your first loop. stop is when you want the loop to exit when i is equal to or greater. step is how many to increment each time.

  • loop 1: 2, then it adds step, and i becomes 4
  • loop 2: 4, then it adds step, and i becomes 6
  • ...
  • loop 9: 18, i becomes 20
  • exits: stop says 20, i is 20, so the loop does not run.

i

1

u/Repulsive-Song-1594 3d ago

i understand the start stop step the thing is i dont know how to use functions inside of it despite me knowing they play by the row its hard t o understand even a simple multiplication is hard for me to do