![]() |
I experimented with a loop, and I don't understand how I got my output - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: I experimented with a loop, and I don't understand how I got my output (/thread-25062.html) |
I experimented with a loop, and I don't understand how I got my output - thinwheats - Mar-17-2020 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 RE: I experimented with a loop, and I don't understand how I got my output - buran - Mar-17-2020 first of all you output does not correspond to your code: for x in [1,2,3,4,5]: x + 1 print(x) 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) RE: I experimented with a loop, and I don't understand how I got my output - thinwheats - Mar-17-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... ? RE: I experimented with a loop, and I don't understand how I got my output - buran - Mar-17-2020 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? RE: I experimented with a loop, and I don't understand how I got my output - Fre3k - Mar-18-2020 (Mar-17-2020, 07:39 PM)thinwheats Wrote: Thank you! 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)
RE: I experimented with a loop, and I don't understand how I got my output - buran - Mar-18-2020 @Fre3k, good catch RE: I experimented with a loop, and I don't understand how I got my output - jefsummers - Mar-18-2020 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. RE: I experimented with a loop, and I don't understand how I got my output - buran - Mar-18-2020 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 |