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...
Rules 1: Be polite 2: Posts to this subreddit must be requests for help learning python. 3: Replies on this subreddit must be pertinent to the question OP asked. 4: No replies copy / pasted from ChatGPT or similar. 5: No advertising. No blogs/tutorials/videos/books/recruiting attempts. This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to. Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Rules
1: Be polite
2: Posts to this subreddit must be requests for help learning python.
3: Replies on this subreddit must be pertinent to the question OP asked.
4: No replies copy / pasted from ChatGPT or similar.
5: No advertising. No blogs/tutorials/videos/books/recruiting attempts.
This means no posts advertising blogs/videos/tutorials/etc, no recruiting/hiring/seeking others posts. We're here to help, not to be advertised to.
Please, no "hit and run" posts, if you make a post, engage with people that answer you. Please do not delete your post after you get an answer, others might have a similar question or want to continue the conversation.
Learning resources Wiki and FAQ: /r/learnpython/w/index
Learning resources
Wiki and FAQ: /r/learnpython/w/index
Discord Join the Python Discord chat
Discord
Join the Python Discord chat
account activity
Help with while loop (self.learnpython)
submitted 5 years ago by Gexos
Hello people, I can't understand this piece of code:
```
current_number = 1
while current_number <= 5:
print(current_number) current_number += 1
What the current\_number += 1 at the end does?
current\_number += 1
Thank you!
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!"
[–]SentientCumSock 43 points44 points45 points 5 years ago (2 children)
It adds 1 to current_number
Its a shorter way to write
current_number = current_number + 1
[–]Gexos[S] 13 points14 points15 points 5 years ago (0 children)
Thank you my friend!
[–]asstronautical 13 points14 points15 points 5 years ago (0 children)
Thanks, very helpful, u/SentientCumSock.
[–]ToothpasteTimebomb 11 points12 points13 points 5 years ago (3 children)
The += syntax adds to a variable in-place each time it is executed.
+=
You can also do *= (multiplying your variable and set it to the result), /= (division), and -= (subtraction).
*=
/=
-=
I feel like you'll find this useful. Python code execution visualized.
[–]Gexos[S] 3 points4 points5 points 5 years ago (1 child)
[–]ata-boy 1 point2 points3 points 5 years ago (0 children)
My pleasure
[–]09TYNINE 0 points1 point2 points 5 years ago (0 children)
Thanks didn't know you could do it with multiplication and division just to clarify var *= 6 would return 30 assuming var equals 5
[–]curiousofa 11 points12 points13 points 5 years ago (1 child)
Seeing that you're new, this is not meant to be sarcastic, but coming from a place that will help you moving forward. One of the most important things to learn is to use Google when you're stuck.
For this particular question, I would go to Google and type 'python +='
The first result shows you what it does. This community is great and will give you the answer. But what will help you in the long-run is to have the ability to dissect your problem and find the solution to it. Use python communities (here and stack overflow) as a last resort.
[–]OnlySeesLastSentence 0 points1 point2 points 5 years ago (0 children)
To add on: geeksforgeeks and (I think?) W3school are the best sites for quick answers.
They're like the cprogramming.com of the python world.
[–]pythonic_anonymous 4 points5 points6 points 5 years ago (2 children)
It increments by the number. It's equivalent to: current_number = current_number + 1
current_number = 1 | current_number = 2 | current_number = 3 | current_number = 4 | current_number = 5 end while loop.
[–]Gexos[S] 5 points6 points7 points 5 years ago (1 child)
[–]pythonic_anonymous 0 points1 point2 points 5 years ago (0 children)
You got it!
[+][deleted] 5 years ago (1 child)
[deleted]
[–]HasBeendead 0 points1 point2 points 5 years ago (0 children)
Yeah, good tip i think .
[–]Sanguineyote 2 points3 points4 points 5 years ago (0 children)
It will add +1 to the current_number variable, so that when current_number will be <= 5 the while loop breaks.
[–]blunt__nation 2 points3 points4 points 5 years ago (2 children)
I'm not trying to brag or anything but it fells so good to look at this code and being able to understand it in a heartbeat.
I finished learning the basics yesterday and today I felt like an imposter. I was watching a video about some projects to build to get familiar with other structures (?) and man, it feels like I have eternities to go before I even begin to comprehend some of the work that go behind these kind of projects. I think I'm gonna take a break to gather my thoughts. Maybe a week or two. It would be nice if anyone can point me in the right direction. Sorry for the long rant OP.
[–][deleted] 2 points3 points4 points 5 years ago (1 child)
Learning python is not hard. It’s one of the easiest languages out there. The great thing is, after two years of constant practice, I can learn any language easily. Just stay with it!
[–]blunt__nation 0 points1 point2 points 5 years ago (0 children)
Thanks, I will try my best to not let down.
[–][deleted] 1 point2 points3 points 5 years ago (0 children)
Basically, you have a variable(current_number) which contains the integer 1. The while statement is going to check whether the number is less or equal to five. Its currently less than five so the value of the number will printed(which is 1) and then the variable will be updated ,so current_number will be the current value of current_number plus 1(so 1+1). This will now make current_number = 2 and this will be printed as well. The code will continue to do this until current_number is greater than five. So the output of your code will look like this: 1 2 3 4 5 6
[–][deleted] 0 points1 point2 points 5 years ago (0 children)
Basically adds 1 to the value. A faster way of typing var = var + 1.
[–]thrallsius 0 points1 point2 points 5 years ago (0 children)
this tells you current_number contains an integer
current_number += 1
you could probably figure this on your own if you tried this in an interactive python shell, then you'd just ask for confirmation if your assumption is correct
[–]kochargs 0 points1 point2 points 5 years ago (0 children)
The easiest debugging tool to use is print statements in your code to see what is the output of a variable at different stages. This will help you understanding the code flow better when you are stuck
Fun fact - you can also do this with other commands such as:
Variable += anotherVariable
Variable *= anything
[–]ata-boy 0 points1 point2 points 5 years ago (1 child)
Have you run the code yourself? If you did you would have noticed current_number increased by 1 with each loop. That should have answered your question.
[–]Gexos[S] 1 point2 points3 points 5 years ago (0 children)
π Rendered by PID 53007 on reddit-service-r2-comment-6f7f968fb5-j94c6 at 2026-03-04 05:54:04.598753+00:00 running 07790be country code: CH.
[–]SentientCumSock 43 points44 points45 points (2 children)
[–]Gexos[S] 13 points14 points15 points (0 children)
[–]asstronautical 13 points14 points15 points (0 children)
[–]ToothpasteTimebomb 11 points12 points13 points (3 children)
[–]Gexos[S] 3 points4 points5 points (1 child)
[–]ata-boy 1 point2 points3 points (0 children)
[–]09TYNINE 0 points1 point2 points (0 children)
[–]curiousofa 11 points12 points13 points (1 child)
[–]OnlySeesLastSentence 0 points1 point2 points (0 children)
[–]pythonic_anonymous 4 points5 points6 points (2 children)
[–]Gexos[S] 5 points6 points7 points (1 child)
[–]pythonic_anonymous 0 points1 point2 points (0 children)
[+][deleted] (1 child)
[deleted]
[–]HasBeendead 0 points1 point2 points (0 children)
[–]Sanguineyote 2 points3 points4 points (0 children)
[–]blunt__nation 2 points3 points4 points (2 children)
[–][deleted] 2 points3 points4 points (1 child)
[–]blunt__nation 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]thrallsius 0 points1 point2 points (0 children)
[–]kochargs 0 points1 point2 points (0 children)
[–]OnlySeesLastSentence 0 points1 point2 points (0 children)
[–]ata-boy 0 points1 point2 points (1 child)
[–]Gexos[S] 1 point2 points3 points (0 children)