0

I cant understand the difference between those two code blocks, first the correct one is:

number_list=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
prime_list=[]
for i in number_list:
    for j in range(2,i):
        if i%j==0:
            break
    else: prime_list.append(i)
print('Primes are:',prime_list)

Output is:

Primes are: [1, 2, 3, 5, 7, 11, 13]

but moving the else statement forward inside the block below the if statement (which i thought was the right thing to do) results in a different and wrong output:

number_list=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
prime_list=[]
for i in number_list:
    for j in range(2,i):
        if i%j==0:
            break
        else: prime_list.append(i)
print('Primes are:',prime_list)

Output is:

Primes are: [3, 5, 5, 5, 7, 7, 7, 7, 7, 9, 11, 11, 11, 11, 11, 11, 11, 11, 11, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13]

Why is this so? how does the code behave with the else statement indented?

Guillaume Jacquenot
  • 11,217
  • 6
  • 43
  • 49
ErrorWhile
  • 11
  • 3
  • Suppose I is 6 ok and you will check with 2,3,4,5 and it will give 0,0,2,1 as mod value now if you put else inside it will append each time when it will not be 0 so for 4,5 it will add in the prime list!! Change your condition in which you are appending do it if i%j is not 0 then you add it else do nothing – Raman Mishra May 08 '19 at 06:52
  • In the first example, the else-statement belongs to the inner for-loop. In this case, `i` is appended to `prime_list` after the inner for-loop has finished iterating through range `(2,i)`. In the second case, the else-statement belongs to the if-statement. In this case, `i` is appended to `prime_list` every time `i%j != 0`. – Heike May 08 '19 at 06:52

3 Answers3

1

The else in different scope has difference means:

  1. else with the same scope of your for-loop is the else condition. It will be execute when not-break in your for loop.
  2. else in the for-loop is used to exit the for loop in your second case. The else is related on your if statement
cmj
  • 106
  • 1
  • 9
1

In the first example else is used for: for loop. It means that the code inside else statement is executed only if for loop ends without break. So in your case only of any number from the range (2, i) is not a divider of i.

In the second case else is used for: if statement. It means that: if if is not true, then execute else statement. In this scenario let's assume i=5. For:

j = 2 => i%j = 1 => else statement is executed:  prime_list.append(5)
j = 3 => i%j = 2 => else statement is executed:  prime_list.append(5)
j = 4 => i%j = 1 => else statement is executed:  prime_list.append(5)

And thats why you have 3 times 5 in your list.

Relandom
  • 1,029
  • 2
  • 9
  • 16
0

Because when moving in, it will loop and most of the case exit the first if statement, then since the else says to add the value, it would keep adding to it.

There is other ways to do this and more efficiently, like below:

prime_list=[i for i in number_list if all(i % x for x in range(2, i))][1:]
print('Primes are:',prime_list)
U13-Forward
  • 69,221
  • 14
  • 89
  • 114