On 4/11/2014 1:13 PM, Andoni Gorostiza wrote:
Hi tutor. I need your help with something I don't understand. In the tutorial, it mentions an example of a range within a range. I'll keep it simplified. How exactly does this work? I'll provide a few examples.

>>> for x in range(0,5):
...for n in range(0,5):
...      print(x)

>>> for x in range(0,5):
...for n in range(0,5)
...      print(n)

This one comes from the tutorial:

>>>for  n  in  range(2,  10):
...     for  x  in  range(2,  n):
...         if  n  %  x  ==  0:
...             print(n,  'equals',  x,  '*',  n//x)
...             break
...     else:
...         # loop fell through without finding a factor
...         print(n,  'is a prime number')
...
2 is a prime number
3 is a prime number
4 equals 2 * 2
5 is a prime number
6 equals 2 * 3
7 is a prime number
8 equals 2 * 4
9 equals 3 * 3
Can you explain what is going on?
We could but that will not help you learn. Instead I recommend with pencil and paper you play computer - "execute" (write) the code one step at a time and write down what happens. Very simple example:
execute:                       change:
for x in range(0,2):      x == 0
  for n in range(0,2):   n == 0
    print(x)                     output == 0
  next for n                   n == 1
    print(x)                     output == 0
next for x                     x == 1
  for n in range(0,2):   n == 0
    print(x)                     output == 1
keep going - at each step write what executes and what changes or happens

When you run into an operation you don't understand stop and either
- look it up
- ask this list

_______________________________________________
Tutor maillist  -  [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to