Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Learning Loops
#1
Here is a simple piece of code with two loops.

It prints out a long string of numbers.

I'm trying to wrap my head around this, but I
don't understand exactly what the code is asking
or what it's doing.


Thanks for any help.



for x in range (0,10):
    for y in range (1,21):
        print(x)
        print(y)
print('\n')
Reply
#2
Hello,

Here is your code with comments. Don't try to run it, it will fail
The comments explain what takes place.

1. for x in range (0,10):     # x will = 0, 1, 2, 3, 4, 5, 6, 7 ,8, 9 
2.     for y in range (1,21): #y will = 0, 1, 2, ... 20                                     |
3.         print(x)           # 1st inner loop, x = 0 and stays 0 until inner loop finishes |
4.         print(y)           # y = 0 ... 20 after y = 20, exits inner loop, x increments,  |- Inner loop
                              # and inner loop executes again                               |
5.     print('\n')
     
The inner loop will execute 10 timmes x = 0 to 9
after each inner loop, one more iteration of outer loop begins (x += 1), then
at step 2 Y starts again at 0 and continues to 20
It would help you to understand if you changed your two print statement to one as follows

            print('x: {}, y: {}'.format(x,y)
Another way to look at is:
X = 0
  y = 0
  Y += 1
  X still = 0, y = 1
  ... until y = 20
x += 1
... until x = 9
Reply
#3
Thank you for your reply. I'm still a little bit confused, but I'll study your answer and play around with it to see if I can get a better grasp of things. 

I'll also try practicing with different types of loops, and maybe that will help.
Reply
#4
BobA-

If you're still confused on this, perhaps I can help clear things up a bit more for you.
I'm not sure how much of this code you do or don't understand, so I apologize if I go over things you already know.

First of all, Larz60+ offered a great recommendation on the formatted print statement - definitely give it a shot.
This guy is a legend around here... Listen to him.

Now... On to the explanation...

You have two loops - or really, a loop nested inside of another loop.
In this instance, your inner loop (y) will run in its entirety FOR EACH iteration of the outer loop (x).

The "range" you are giving to x and y is the "range" of numbers to span - or, the number of times to loop.
The x loop will run 10 times (starting at 0 and stopping at (not including) 10).
The y loop will run 20 times (starting at 1 and stopping at (not including) 21).

So if we back up and remind ourselves that the inner loop will run in its entirety for each iteration of the outer loop, then that must mean:
For every time x runs, y will run 20 times, until x has run 10 times.

To give you an idea of the expected outcome:

First Loop -
x = 0
y = 1, y = 2, y = 3, y = 4, y = 5........ up to y = 20.
Then on to the next loop -
x = 1
y = 1, y = 2, y = 3, y = 4, y = 5........ up to y = 20.
Then on to the third loop - 
x = 2
y = 1, y = 2, y = 3, y = 4, y = 5........ up to y = 20.
All the way until the last loop -
x = 9
y = 1, y = 2, y = 3, y = 4, y = 5........ up to y = 20.


Hope that was helpful!
Reply
#5
Yes,  that was very helpful, and now I understand it save for two things.  

1. My output doesn't make any sense according to what you say, it starts with 12/2, 13/2, 14/2...
as shown below and

2. I don't understand what x+=0 means.  That formula doesn't make any sense to me.
    x + what??

Thanks
Reply
#6
Please post the code that was run to produce the output shown because the code in the first post would not produce the output shown (unless you have just not copied all of the output and cut the beginning off)
x += 0
is the same as 
x = x + 0
which adds nothing to the value of x and saves the result in x. This does not appear in any of the code shown so far.
Reply
#7
BobA -
Is the complete code just the nested for loop you posted in your original post?
The output you are getting seems like your code is different than what youve supplied.

Post your full code.
We can better help you learn whats going on that way.
Are you following a tutorial?

Also, as Yoriz mentioned, y+=0 shows up no where in your code, so im not sure in what instance it is in question for you.
Look up arithmatic operators for python, and study how they work - you will run into them in every python script.
Reply
#8
(Oct-05-2016, 05:01 PM)Yoriz Wrote: Please post the code that was run to produce the output shown because the code in the first post would not produce the output shown (unless you have just not copied all of the output and cut the beginning off)

I copied the code from post 1 in this thread and ran it in Visual Studio.  I've run it several times with the same result.

Here is the code again.

for x in range (0,10):
    for y in range (1,21):
        print(x)
        print(y)
print('\n')
Reply
#9
Hello,

I take responsibility for that. I made a mistake. code should be:

for x in range (10):
    for y in range (21):
        print(x)
        print(y)
print('\n')
Reply
#10
(Oct-06-2016, 11:24 AM)Larz60+ Wrote: Hello,

I take responsibility for that. I made a mistake. code should be:

for x in range (10):
    for y in range (21):
        print(x)
        print(y)
print('\n')

I don't know what's  going on.  If I run the above code it looks like this:
Reply


Forum Jump:

User Panel Messages

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