Python Forum
Is 2 a prime number? for loop & range fuction in python docs says yes, mine says no.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is 2 a prime number? for loop & range fuction in python docs says yes, mine says no.
#1
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.
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?
Reply
#2
Your conclusion that range(2, 10) means: 2 through 9 is correct. A range never includes the last number.
But why do you think range(2, 2) means: 2 through 2? Of course it must mean 2 through 1. It will yield nothing unless a negative step is given.
Reply
#3
(Nov-16-2019, 08:39 PM)ibreeden Wrote: But why do you think range(2, 2) means: 2 through 2? Of course it must mean 2 through 1. It will yield nothing unless a negative step is given.

I thought range(2, 2) means 2 through 2 because that seemed logical to me. Please educate me w/ why that means 2 through 1 and more examples.

According to w2schools.com, range(start, stop, sequence)
start Optional. An integer number specifying at which position to start. Default is 0
stop Required. An integer number specifying at which position to end.
step Optional. An integer number specifying the incrementation. Default is 1

so how range(2, 2) is 2 through 1?

what about range (7, 7)? does it mean 7 through 1?
Reply
#4
Hi allusernametaken.

If you use Python's interpreter you can quickly test/use range().

>>> list(range(1,10))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(7,7))
[]
>>> list(range(1,1))
[]
>>> list(range(1,-1))
[]
For range(m,n), so as you pointed out will go from m to n in steps of one up to n-1.

So, range(2,2) should have gone forward from 2 to 1 in steps of one. This is a singular case/corner case that returns an empty value. It won't compute.

Regards
Reply
#5
(Nov-17-2019, 12:11 AM)bajacri Wrote: So, range(2,2) should have gone forward from 2 to 1 in steps of one. This is a singular case/corner case that returns an empty value. It won't compute.

Nice, very cool. Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Prime number detector Mark17 5 735 Nov-27-2023, 12:53 PM
Last Post: deanhystad
  [SOLVED] [loop] Exclude ranges in… range? Winfried 2 1,367 May-14-2023, 04:29 PM
Last Post: Winfried
  Pairs of multiplied prime number--->N Frankduc 13 3,379 Jan-16-2022, 01:52 PM
Last Post: Frankduc
  matplotlib x axis range goes over the set range Pedroski55 5 3,103 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  Loop Excel Range Kristenl2784 2 3,211 Jun-18-2020, 04:49 PM
Last Post: Kristenl2784
  Return prime numbers from range krzyfigh 2 1,885 Apr-20-2020, 08:08 PM
Last Post: krzyfigh
  Number range? rusty11 5 2,598 Feb-18-2020, 09:45 PM
Last Post: rusty11
  Could These Two Threads Of Mine Be Moved To This Forum ? eddywinch82 1 1,663 Feb-16-2020, 10:44 AM
Last Post: buran
  Define a range, return all numbers of range that are NOT in csv data KiNeMs 18 6,875 Jan-24-2020, 06:19 AM
Last Post: KiNeMs
  How to terminate the caller fuction through callee? 100k 2 2,101 Nov-27-2019, 06:49 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020