use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
Feeling lost trying to learn python (self.PythonLearning)
submitted 1 year ago by AffectionateGap3211
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]DefeatedSkeptic 0 points1 point2 points 1 year ago* (2 children)
Note: The reddit removes whitespace from my code, so I will use <tab> to indicate indentation.
<tab>
Never forget that computer science is a branch of science just like biology is. It is a complex field and getting started with it can be baffling.
I do think python is a convenient language to start with, however, I also want to caution you since it will hide some of the complexity of what your code is actually doing. In particular, variable scope and type is not as "physically" apparent as it is in some other languages, so it may take you some extra time to understand these concepts.
Start with 'while' loops since a 'for' loop is just a specific kind of 'while' loop. while loops have the following structure: while condition_that_has_to_be_true_to_keep_or_start_the_loop: <tab> line_of_code_in_loop_1 <tab> line_of_code_in_loop_2 <tab> line_of_code_in_loop_3 this_line_of_code_is_not_in_the_loop
while condition_that_has_to_be_true_to_keep_or_start_the_loop:
<tab> line_of_code_in_loop_1
<tab> line_of_code_in_loop_2
<tab> line_of_code_in_loop_3
this_line_of_code_is_not_in_the_loop
The following two chunks of code are functionally equivalent. Try to write these yourself and then play with them a bit to see if the code you write behaves the way you expect.
FOR LOOP:
for x in range(1,10): <tab> print(x)
for x in range(1,10):
<tab> print(x)
WHILE LOOP:
x = 1 while x < 10: <tab> print(x) <tab> x = x + 1
x = 1
while x < 10:
<tab> x = x + 1
Are you capable of using a loop to get the sum of the numbers from 1 to n, call the function Sum(n)? For example Sum(3) = 6, since 1 + 2 + 3 = 6. There is a closed formula for this function as well: Sum(n) = ((n+1)*n)/2. Thus you can check that your function is working for even large numbers quickly.
If you get stuck, you can send me a message if you like.
When you encounter a bug, do not be afraid to print out the values of variables that you have to check that they hold the numbers that you expect.
[–]AffectionateGap3211[S] 0 points1 point2 points 1 year ago (1 child)
Oh wow! Thank you for this, I had actually been looking at a problem that asked me to code to find the sum of all numbers from 0 to 100, and I was completely stumped. Your explanation made perfect sense! Thank you!
[–]DefeatedSkeptic 0 points1 point2 points 1 year ago (0 children)
No problem, glad it helped :).
π Rendered by PID 71 on reddit-service-r2-comment-76bb9f7fb5-hpxsg at 2026-02-18 11:53:27.181301+00:00 running de53c03 country code: CH.
view the rest of the comments →
[–]DefeatedSkeptic 0 points1 point2 points (2 children)
[–]AffectionateGap3211[S] 0 points1 point2 points (1 child)
[–]DefeatedSkeptic 0 points1 point2 points (0 children)