-4
b=input("Enter number : ")
for n in range(2, b+1):
     for x in range(2, n):
         if n % x == 0:
              break
     else:
          print n

This program prints prime numbers up to n Out put is

Enter number : 10
2
3
5
7

and

b=input("Enter number : ")
for n in range(2, b+1):
     for x in range(2, n):
         if n % x == 0:
              break
         else:
              print n

out put is

Enter number : 10
3
5
5
5
7
7
7
7
7
9

kloe
  • 21
  • 10
  • 1
    The version of Python used to execute them. – Burhan Khalid Oct 21 '16 at 15:15
  • @BurhanKhalid nice answer :) – MMF Oct 21 '16 at 15:17
  • @MMF - I know it's weird, but that indentation difference actually changes how this code works. http://stackoverflow.com/questions/9979970/why-does-python-use-else-after-for-and-while-loops – thegrinner Oct 21 '16 at 15:17
  • 2
    Before the editted, the second program had a `for else` statement, and now I'm not sure since it's the same.... But if it was true to have a for else, then the one with for else will print `n` once the for loop ends. Edited: Guys please stop editing it, is it a `for else` or not... – MooingRawr Oct 21 '16 at 15:17
  • the version is python 2.7.10 – kloe Oct 21 '16 at 15:17
  • @kloe the indentation in your first block is important. Are the `for` and `else` at the same level of indentation? – thegrinner Oct 21 '16 at 15:21

2 Answers2

5
b=input("Enter number : ")
for n in range(2, b+1):
     for x in range(2, n):
         if n % x == 0:
              break
     else:
          print n

prints out n if n % x has no remainder and only the first correct value since it breaks out.


b=input("Enter number : ")
for n in range(2, b+1):
     for x in range(2, n):
         if n % x == 0:
              break
         else:
              print n

prints out every n that has a non zero remainder until the first zero remainder appears.


More on how for else works:

for else will run the for loop and then run the else right after the for loop finishes. In your case the for loop ends at a certain point and then prints the resulting value where it ended at.

for x in range(10):
    print x
else:
    print "hello world"

Take this for example. It prints out :

0
1
2
3
4
5
6
7
8
9
hello world 

Why is this useful? Well your program gives a very nice example on why it is. We want to exit the for loop for a certain condition that passed, and then do something after we've found that condition (if we've found the passing condition else we just run it anyways). So if condition is met in for loop run this, or run this at the end of the for loop always.


Back to your question, basically the first one finds the factors of the number given, and the second one finds the non factors of the number given.

MooingRawr
  • 4,901
  • 3
  • 24
  • 31
-1

They are identical

XXXX:tmp anthony$ cat > one.txt
b=input("Enter number : ")
for n in range(2, b+1):
     for x in range(2, n):
         if n % x == 0:
              break
         else:
              print n
XXXX:tmp anthony$ cat > two.txt
b=input("Enter number : ")
for n in range(2, b+1):
     for x in range(2, n):
         if n % x == 0:
              break
         else:
              print n
XXXX:tmp anthony$ diff one.txt two.txt
XXXX:tmp anthony$
Anthony Oteri
  • 402
  • 4
  • 14