r/PythonLearning • • 5d ago

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

1 Upvotes

5 comments sorted by

2

u/Money_Ad_5277 5d ago edited 5d 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 5d 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 5d 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 5d ago
progress_list = []  # This is a Python list
progress_set = {}  # This is a Python set

1

u/Educational-Paper-75 2d ago

progress_set has 10 elements with index 0 through 9, thus there is no element 10, so operation needs to be below 10. You can use integer division // and get rid of mapping to int instead of using real division / on the last line. You probably also want operation to be positive.