Nov-16-2019, 06:59 PM
(This post was last modified: Nov-16-2019, 06:59 PM by allusernametaken.)
I'm a beginner in computer science field, and Python is my first programming language I'm studying. I started reading Python documentation and would like to clarify my understanding of range function and for loop.
In 4.4. break and continue Statements, and else Clauses on Loops of Python doc,
There is this code, to find a prime number, below.
1) n will be 2, 3, 4, 5, 6, 7, 8 & 9 because of range function and for loop.
2) because of the first step,
for x in range(2, 2)
...
for x in range(2, 9) will happen.
So here's my question.
If I run the codes below separately,
In 4.4. break and continue Statements, and else Clauses on Loops of Python doc,
There is this code, to find a prime number, below.
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')How I'm understanding is,
1) n will be 2, 3, 4, 5, 6, 7, 8 & 9 because of range function and for loop.
2) because of the first step,
for x in range(2, 2)
...
for x in range(2, 9) will happen.
So here's my question.
If I run the codes below separately,
if 2 % 2 == 0: print(2, 'equals', 2, '*', 2//2) else: print(n, 'is a prime number')the result is different. What, where, how am I getting this wrong?