Posts: 2
Threads: 1
Joined: Mar 2020
Mar-17-2020, 07:10 PM
(This post was last modified: Mar-17-2020, 07:11 PM by thinwheats.)
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
Posts: 8,160
Threads: 160
Joined: Sep 2016
Mar-17-2020, 07:24 PM
(This post was last modified: Mar-17-2020, 07:24 PM by buran.)
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)
Posts: 2
Threads: 1
Joined: Mar 2020
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... ?
Posts: 8,160
Threads: 160
Joined: Sep 2016
Mar-17-2020, 07:45 PM
(This post was last modified: Mar-17-2020, 07:45 PM by buran.)
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?
Posts: 25
Threads: 7
Joined: Oct 2019
(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
Posts: 8,160
Threads: 160
Joined: Sep 2016
Posts: 1,358
Threads: 2
Joined: May 2019
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.
Posts: 8,160
Threads: 160
Joined: Sep 2016
Mar-18-2020, 07:33 PM
(This post was last modified: Mar-18-2020, 07:33 PM by buran.)
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
|