 |
BOOK: Python Projects
 | This is the forum to discuss the Wrox book Python Projects Laura Cassell, Alan Gauld; ISBN: 978-1-118-90866-2 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the BOOK: Python Projects section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

October 17th, 2015, 10:53 AM
|
|
Registered User
|
|
Join Date: Oct 2015
Posts: 5
Thanks: 2
Thanked 2 Times in 2 Posts
|
|
P19, "even squares", code error
On page 19, generator expressions are introduced. The following code from that page produces the expexted result in the form of a list of even squares:
Code:
>>> [n*n for n in range(1,11) if not n*n % 2]
[4, 16, 36, 64, 100]
At the bottom of the page there is an example of how to generate an identical result with a conventional for loop. However, the sample code produces odd integers, instead of even squares, because not and n*n are missing:
Code:
>>> result = []
>>> for n in range(1,11):
... if n*n % 2: # Missing not
... result.append(n) # n instead of n*n
...
>>> print(result)
[1, 3, 5, 7, 9]
This code produces the expected result:
Code:
>>> result = []
>>> for n in range(1,11):
... if not n*n % 2:
... result.append(n*n)
...
>>> print(result)
[4, 16, 36, 64, 100]
|
|
The Following User Says Thank You to Egalth For This Useful Post:
|
|
|

October 17th, 2015, 11:02 AM
|
|
Registered User
|
|
Join Date: Oct 2015
Posts: 5
Thanks: 2
Thanked 2 Times in 2 Posts
|
|
Sorry folks, forgot to mention that this has already been addressed in the Errata section. However, the correction in the Errata section only mentions the missing not so the result is even integers, instead of even squares:
Code:
>>> result = []
>>> for n in range(1,11):
... if not n*n % 2: # "not" now included.
... result.append(n) # n*n still missing.
...
>>> print(result)
[2, 4, 6, 8, 10] # Even integers instead of even squares.
Last edited by Egalth; October 17th, 2015 at 11:13 AM..
|
|
The Following User Says Thank You to Egalth For This Useful Post:
|
|
|

October 17th, 2015, 12:21 PM
|
|
Wrox Author
|
|
Join Date: Feb 2015
Posts: 25
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
That's for spotting that. I can only apologize for the error, not sure how that got
through all the reviews but it obviously did. Your changes are absolutely correct.
Apologies again,
Alan G.
|
|
The Following User Says Thank You to Alan G For This Useful Post:
|
|
|

June 26th, 2017, 06:18 PM
|
|
Registered User
|
|
Join Date: Jun 2017
Posts: 1
Thanks: 2
Thanked 0 Times in 0 Posts
|
|
Not real bugs but...
I tried to find in the index 'list comprehension' and 'conditional expression'. Not available...
|
|

June 26th, 2017, 08:40 PM
|
|
Wrox Author
|
|
Join Date: Feb 2015
Posts: 25
Thanks: 0
Thanked 3 Times in 3 Posts
|
|
You are quite right, I'm not sure why they got missed out. I could say it's because the first chapter is supposed top be revision rather than anything new, but they have included loops so i suspect it is just a simple omission. Index entries are always a bit subjective because you can't index everything but those two probably should be there.
I can only apologize.
|
|
 |
|