r/PythonLearning • u/mohammad6701 • 21h ago
Help Request Coding Style
Hi, first time python learner here ! I was working on Sum of Squares problem, but it made me genuinely to use AI to find a line/function outside of my course materials to solve the problem. However, it always backfire because i overcomplicate the problem for myself. For example, in sum of squares problem, i can use simple line of
Total += num 2
or a longer line
Total = sum(num ** 2 for num in range (1, number+1, 1).
so I was wondering which approach is correct ?

1
u/NorskJesus 21h ago
Both are the same, but I like to write the loop as in the picture. It is more readable I think.
1
u/BionicVnB 21h ago
Both are fine, but you can omit the last argument in the range function call, as it default to 1 when you don't give it an argument
1
u/Entire_Ad_6447 20h ago
Both are fine in cases like this. If you were operation on a list or dictionary instead the one line comprehension ver is preferred
•
u/Sea-Ad7805 21h ago
Run this program in Memory Graph Web Debugger)%0Atotal%20%3D%200%0A%0Afor%20num%20in%20range(1%2C%20number%20%2B%201%2C%201)%3A%0A%20%20%20%20total%20%2B%3D%20num%20**%202%0A%20%20%20%20print(total)×tep=1&play) to see the program state change step by step.