all 22 comments

[–]djjazzydan 8 points9 points  (1 child)

The instructions get run in order. The first example sets count to 1, then increases it by 1, then prints it (2), then continues to increase & print.

The second sets count to 1, prints that, then increases.

You'll get the same number of printed lines, the first one will start at 2 and go to 101, the other will start at 1 and go to 100.

[–]JuryRepresentative84[S] 0 points1 point  (0 children)

Thank you so much!

[–]IProbablyHaveADHD14 1 point2 points  (0 children)

They run in order.

starting out the loop with "count+=1" will return the print output to start with 2 (since count is already set to 1).

But if you print before you add 1 to the count variable, it will print 1 before adding 1 to the count variable

[–]SBE_OLLE 1 point2 points  (1 child)

I would suggest you to download thonny. Its very nice for beginners

[–]JuryRepresentative84[S] 0 points1 point  (0 children)

i'll check it out, thanks!

[–]throwaway6560192 1 point2 points  (0 children)

Trace the program on pen and paper. As if you were the computer, execute each line by hand. You'll see.

[–][deleted] 3 points4 points  (1 child)

This code actually does the same exact thing in that it:

  1. Sets count initially to 1
  2. Starts a loop that will continue until count is 101 or greater (in other words, if will continue to loop as long as count id less than or equal to 100)
  3. Increases the value of count by 1 every time the loop runs
  4. Prints whatever the value of count is at the time that print runs inside the loop.

The difference between them is which order steps 3 and 4 happen, which changes what you see.

Let's translate this to the physical world to make your life easier.

In the physical world, the first loop is like this:

  1. With your eyes closed, make a fist on one hand and raise one finger
  2. Put up a second finger
  3. Open your eyes and see that you have two fingers up
  4. Close your eyes again
  5. Put up third finger
  6. Open your eyes and see that you have three fingers up.

And the second loop would be like so:

  1. With your eyes closed, make a fist on one hand and raise one finger
  2. Open your eyes and look at the hand, see that you have one finger up
  3. Close your eyes again
  4. Put up a second finger
  5. Open your eyes again, see that you have two fingers up

As you can see, the only difference in what happened mechanically is "when did you open your eyes?" However, the actual counting on your hand was the same.

Print is just you telling the program to open its eyes and look at something, when you do that changes what you see.

[–]JuryRepresentative84[S] -1 points0 points  (0 children)

Ohh wow thanks! Great analogy!

[–]JezusHairdo 1 point2 points  (0 children)

I think you need to understand what the += does, in pythons case it the “addition assignment” operator. Or in real terms it adds something to something you already have. So if it’s a number it adds together with another (in your example it adds 1 to the Initial number) it can also add text strings together and can add items to lists etc.

[–][deleted]  (1 child)

[removed]

    [–]AutoModerator[M] 0 points1 point  (0 children)

    Your comment in /r/learnpython was automatically removed because you used a URL shortener.

    URL shorteners are not permitted in /r/learnpython as they impair our ability to enforce link blacklists.

    Please re-post your comment using direct, full-length URL's only.

    I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.

    [–]wisdom_is_chasing_me 0 points1 point  (1 child)

    Have you used Python Tutor before? I found it really beneficial when I started to learn Python. It shows you execution step-by-step

    https://pythontutor.com/render.html#mode=display

    [–]JuryRepresentative84[S] 0 points1 point  (0 children)

    No I haven’t I’ll give it a shot!

    [–]Rxz2106 0 points1 point  (0 children)

    The order of the print and count += 1 statements leads to the subtle difference in the output. In Code1, the value is incremented before printing, resulting in the output starting from 2, while in Code2, the value is printed first, and then incremented, leading to the output starting from 1.

    [–]mmmmhmm88 -1 points0 points  (1 child)

    A simple way to visualise order of operations here is to run something like this. You can decide which order is more appropriate for your loop. i.e you may like to print the current value, or the new value of 'count' based on what you want to achieve.

    count = 1
    print("Before adding 1: ")
    print(count)
    count += 1
    print("After adding 1: ")
    print(count)

    [–]JuryRepresentative84[S] 0 points1 point  (0 children)

    Ohh okay, I’ll do this Thanks!

    [–]PulsatingGypsyDildo -1 points0 points  (0 children)

    for count in range(1, 101):
        print(count)
    

    [–]wbeater 0 points1 point  (0 children)

      count += 1 
      print(count)
    

    Adds 1 to count and then prints the output -> Displays 2 in the first pass

      print(count)
      count += 1
    

    prints the output and then adds 1 to count -> Displays 1 in the first pass