rpiboy
Posts: 86
Joined: Mon Mar 20, 2017 8:39 pm

Range in python

Tue Jul 04, 2017 7:31 pm

Code: Select all

def print_squares(start, end):
    for n in range(start, end):
        yield n ** 2

for n in print_squares(2, 5):
    print(n)
With this code, i thought it would have printed 2, 4, 9 and 16. Why does it print 4, 9 and 16?

mattmiller
Posts: 1953
Joined: Thu Feb 05, 2015 11:25 pm

Re: Range in python

Tue Jul 04, 2017 7:39 pm

because its only loops while n <5 not <=5

Often catches me out as well :)
I think they shouldn't have used the word range because in all other English language contexts the range would include the endpoints
e.g "vehicles ranging from cars to bicycles" would include cars and bicycles

But Python is what it is :)

User avatar
PeterO
Posts: 4241
Joined: Sun Jul 22, 2012 4:14 pm

Re: Range in python

Tue Jul 04, 2017 7:44 pm

mattmiller wrote:because its only loops while n <5 not <=5
Except OP didn't ask about it not printing 25 ! Mind you I think they are confused as well !

Why would you expect it to print "2" ?
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

rpiboy
Posts: 86
Joined: Mon Mar 20, 2017 8:39 pm

Re: Range in python

Tue Jul 04, 2017 7:52 pm

PeterO wrote:
mattmiller wrote:because its only loops while n <5 not <=5
Except OP didn't ask about it not printing 25 ! Mind you I think they are confused as well !

Why would you expect it to print "2" ?
PeterO
Not 2 i meant 1 because 1 ** 2 is 1
It said (2,5)
range 2 i thought was 1 (0,1) but i am guessing the code is saying start from 2 and end with range 5 which is (4) 4 ** 2 which is 16 (0, 1, 2, 3, 4)

User avatar
PeterO
Posts: 4241
Joined: Sun Jul 22, 2012 4:14 pm

Re: Range in python

Tue Jul 04, 2017 7:56 pm

rpiboy wrote:
PeterO wrote:
mattmiller wrote:because its only loops while n <5 not <=5
Except OP didn't ask about it not printing 25 ! Mind you I think they are confused as well !

Why would you expect it to print "2" ?
PeterO
Not 2 i meant 1 because 1 ** 2 is 1
It said (2,5)
range 2 i thought was 1 (0,1) but i am guessing the code is saying start from 2 and end with range 5 which is (4) 4 ** 2 which is 16 (0, 1, 2, 3, 4)
No. It's not hard to try these things to see what they produce....

Code: Select all

Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print range(2,5)
[2, 3, 4]
>>> 
PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

rpiboy
Posts: 86
Joined: Mon Mar 20, 2017 8:39 pm

Re: Range in python

Tue Jul 04, 2017 8:04 pm

Ok thanks

rpiboy
Posts: 86
Joined: Mon Mar 20, 2017 8:39 pm

Re: Range in python

Tue Jul 04, 2017 8:08 pm

No. It's not hard to try these things to see what they produce....

Code: Select all

Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print range(2,5)
[2, 3, 4]
>>> 
PeterO
I did print range(2, 5) in python 3 and i got a syntax error

User avatar
PeterO
Posts: 4241
Joined: Sun Jul 22, 2012 4:14 pm

Re: Range in python

Tue Jul 04, 2017 8:18 pm

rpiboy wrote: I did print range(2, 5) in python 3 and i got a syntax error
Ah, well in python3 print is a function so you need "print(range(2,5))"
But that won't give the result you expect either!

Code: Select all

Python 2.7.6 (default, Oct 26 2016, 20:30:19) 
[GCC 4.8.4] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print type(range(2,5))
<type 'list'>
>>> 

Code: Select all

Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(type(range(2,5)))
<class 'range'>
>>> 

See http://pythoncentral.io/pythons-range-f ... explained/

PeterO
Discoverer of the PI2 XENON DEATH FLASH!
Interests: C,Python,PIC,Electronics,Ham Radio (G0DZB),Aeromodelling,1960s British Computers.
"The primary requirement (as we've always seen in your examples) is that the code is readable. " Dougie Lawson

rpiboy
Posts: 86
Joined: Mon Mar 20, 2017 8:39 pm

Re: Range in python

Tue Jul 04, 2017 8:36 pm

I got it print(list(range(2, 5)))

Return to “Python”

Who is online

Users browsing this forum: No registered users and 21 guests