Hi everyone I am learning python, I came across a sample program which checks whether the given number is prime number or is not a prime number using for loop and range() function. I can't understand that program, can someone please help me understand it? Here is the program and the output. So initially what will the variable 'n' hold?

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’
...
#output
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

Dani AI

Generated

Brief, practical clarification tied to the thread

@nVarunkrishna: in that snippet n takes each value produced by range(2, 10) (2 through 9). For each n, the inner loop assigns x each value from 2 up to n-1. If the inner loop finds a divisor it breaks; if it never breaks the inner for's else block runs — that else belongs to the for, not to the if. Indentation matters here (as pointed out): the else must line up with the for it pairs with.

Corrections and version notes

: the divisibility test should be n % x == 0 (remainder equals zero). Also be aware of Python 2 vs Python 3 differences: print is a function in Py3, range is a lazy sequence in Py3 (not a full list), and n/x yields different results across versions — use n//x for integer division if you want integer factors consistently.

A clearer, faster Python 3 friendly approach (skip checking even numbers and stop at sqrt):

def is_prime(n):
    if n < 2:
        return False
    if n % 2 == 0:
        return n == 2
    limit = int(n**0.5) + 1
    for i in range(3, limit, 2):
        if n % i == 0:
            return False
    return True

Notes: this reduces work to O(sqrt(n)) checks and handles 0, 1 and negatives safely. Use a visual stepper (as suggested) to watch loop variables change if the flow is still confusing.

Recommended Answers

All 5 Replies

The program is using nested for loops -

It is also usin modulo which is "%" this works out the remainder therefore if:

n / x = 0

the if statement will be true.

Therefore it will run:

print n, ’equals’, x, ’*’, n/x

Eitherwise if n / x is not = 0, it will run the following line of code:

print n, ’is a prime number’


so what value will the variable n takes? Initially after applying the range(2,10), n will hold (2-9). then what will the variable x holds? That first two for loops are confusing me...

range(2,10) generates a list, [2,3,4,5,6,7,8,9]
for n in range(2,10) assigns n to the next element in the result from range(2,10) and then executes the code, repeat
range(2,n) takes n and generates [2,3,4....,n-1]

In Python code blocks need to be properly indented.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.