Comprehending Comprehensions


#1



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

Oops, try again. threes_and_fives should contain 3, but doesn't.


not sure where im going wrong


threes_and_fives=[x%3==0 or x%5==0 for x in range(15)]


#2

It may be a problem of precedence so grouping would fix that.

(x % 3 == 0 or x % 5 == 0)

Will check the lesson and confirm.

Okay, not that. The comprehension requires a condtional expression. How would you rewrite it to include one?

Bear in mind what the instructions ask for...

that consists only of the numbers between 1 and 15

Check your range, as well.


#3

threes_and_fives=[(x%3==0 and x%5==0) for x in range(16)]`

to me this says for all the x's in the range 1 to 15 return those with mod 3 and mod 5 equaling zero. no?


#4

Not that I can read. Your list will be populated with booleans as far as I can tell. What are you seeing in the display when you log (print) threes_and_fives?

>>> threes_and_fives=[(x%3==0 and x%5==0) for x in range(16)]
>>> threes_and_fives
[True, False, False, False, False, False, False, False, 
False, False, False, False, False, False, False, True]
>>>

>>> threes_and_fives=[(x%3==0 or x%5==0) for x in range(1,16)]
>>> threes_and_fives
[False, False, True, False, True, True, False, False, 
True, True, False, True, False, False, True]
>>>

#5

i get the result "None"


#6

threes_and_fives=[x for x in range(1,16) if x%3==0 or x%5==0]Preformatted text

i misunderstood what the meaning was of the first x. seeing the true/false thing makes it clear. this now works. thanks


#7

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