Python Forum
I experimented with a loop, and I don't understand how I got my output
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I experimented with a loop, and I don't understand how I got my output
#1
I am self-studying python while on the job. I have "Python Programming: An Introduction to Computer Science" by John Zelle, and I'm on page 44 learning about the "for" loop. I experimented with some code not in the book, and I don't know how I got my output. Can someone explain? I'm very curious:
>>> #book example
>>> for x in [1,2,3,4,5]:
	print (x+1)

	
2
3
4
5
6
>>> #my experiment
>>> for x in [1,2,3,4,5]:
	x + 1
	print(x)

	
2
1
3
2
4
3
5
4
6
5
Reply
#2
first of all you output does not correspond to your code:
for x in [1,2,3,4,5]:
    x + 1
    print(x)
Output:
1 2 3 4 5
the reason is that you don't store the result of x+1 in a variable. It is simply lost. You could use x to store the new value

[python]for x in [1,2,3,4,5]:
    x = x + 1 # now x has value increased by 1
    #x += 1 # this is alternative
    print(x)
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Thank you!

However, I don't think I asked my question well:

1. Why did the "x + 1" double my list length?
2. Why does the "x + 1" result in an out put of 2,1,3,2... ?
Reply
#4
Neither of these happens with the code you show - first part of my post show what your code will output. You can see for yourself here:
https://repl.it/repls/GrimEducatedObservation

There is something else in your code that produce that output (i thought it's just error when posting on forum)

How/where do you run the code. Can you share a screenshot?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
(Mar-17-2020, 07:39 PM)thinwheats Wrote: Thank you!

However, I don't think I asked my question well:

1. Why did the "x + 1" double my list length?
2. Why does the "x + 1" result in an out put of 2,1,3,2... ?

Hey,

This would be the solution for your output.
Maybe you tested something, and delete something when you psotet in here :)

for x in [1,2,3,4,5]:
    print(x+1)
    print(x)
Output:
2 1 3 2 4 3 5 4 6 5
Reply
#6
@Fre3k, good catch
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
I reproduced your result by copying and pasting into IDLE. Did not get those results in Colab.

What appears to be going on is that the loop executes the first line (x+1) and in IDLE it prints the result (2) It then executes the print statement and you get the value 1. The next time through the loop the x+1 prints 3, then print(x) prints 2. Keep the loop going and you get your exact result.

So the answer is that the x+1 line results in a value being printed, doubling the length of your output.
Reply
#8
OK, I must admit I was surprised. After @jefsummers reproduced the result in IDLE, I tried in terminal in interactive mode and reproduced the output too. I admit, your code was clearly in interactive mode, but I test in IDE.
In interactive mode each line is evaluated immediately and result is displayed, but I was confused because it was in the loop body.
Basically the explanation is that this is because you are in interactive mode. If you put this code in a python file and execute it from terminal - the output will be different (i.e. no output for x + 1 line)

>>> for x in [1,2,3,4,5]:
...     x+1
...     print(x)
... 
2
1
3
2
4
3
5
4
6
5
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Storing data/output from a loop Jason613 6 7,456 Sep-29-2019, 02:58 PM
Last Post: ichabod801
  Help to understand output Pippi 1 1,858 Jan-20-2019, 02:23 PM
Last Post: stullis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020