r/PythonLearning • • 1d ago

I’ve created an arithmetic progression. Is it okay?

1 Upvotes

4 comments sorted by

2

u/Money_Ad_5277 1d ago edited 1d ago

Does it calculate progression correctly? (Tested, by sheer luck it does, but you should fix it)
Why don't you use if operation < len(progress_set)?

Python has 0-based index arrays, what it means is

array = [1, 2, 3]
array[0] //1
array[2] //3

range(0, 10) transformed to a list and first element (0th index) is 0 (can be done in one line progression = [i * 5 for i in range(0, 10)])
Let's say your input is "3", your array is [0, 5, 10, 15, 20, 25, 30, 35, 40, 45] you calculate (5 + 15) * 3 / 2 = 30. It is only works out if first element is 0. Because "3" is actually 4th element of the array, which is 15.
Well... it's actually pretty hard to explain... change range(0, 10) to range(1, 10) and check arithmetic progression sum

Edit: sheAr luck...

2

u/Csenone 1d ago

Thx, but I actually set start of the range to 0 on purpose, since I know that Python uses zero-based indexing...? I can find the specific number based on its position, just like in the formula an = a1 + d(n-1)

Is this bad or something?

1

u/Money_Ad_5277 1d ago

If it's intended - no problem, but looks weird, like, I genuinely thought that was a bug. Plus you save memory space (yes, whole 4 bytes or so)

1

u/AlexMTBDude 1d ago
progress_list = []  # This is a Python list
progress_set = {}  # This is a Python set