Try It! (x problem)


#1



https://www.codecademy.com/courses/python-beginner-en-KAgt5/3/3?curriculum_id=4f89dab3d788890003000096#

Hello,
I have done this exercise however I'm stuck with a problem. It asks us to "Use filter() and a lambda expression to print out only the squares that are between 30 and 70 (inclusive)." So aren't we supposed to do the second version I show below instead of the correct version which in the first code.


Because It asks us to "only the squares that are between 30 and 70 ". Then why does it accept the "x"s instead of "x**2" ?


1)
squares=[x**2 for x in range(1,11)]
print filter(lambda x: x>=30 and x<=70, squares) 

&
2)
squares=[x**2 for x in range(1,11)]
print filter(lambda x: x**2>=30 and x**2<=70, squares)


#2

This version doesn't have any data to filter. Consider:

>>> squares=[x**2 for x in range(1,11)]
>>> squares
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
>>> print (list(filter(lambda x: x**2>=30 and x**2<=70, squares)))
[]
>>>

Now examine the above filter. 1**2 is 1, 4**2 is 16, 9**2 is 81, &c.

None of the values is in the prescribed range.

For future reference, consider that Python recognizes an inequality expression as a range (in a lambda)...

30 < x < 70

I left out the = since neither 30 nor 70 are perfect squares.


#3

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.