title: Conditional list comprehension
body: <p>I am trying to gain a better understanding of how list comprehensions work.
  I have the following function that returns true or false if a number is prime (which I found somewhere on the forum but cant remember where):</p>
  
  <pre><code>import math
  
  def is_prime(n):
      if n % 2 == 0 and n &gt; 2: 
          return False
      for i in range(3, int(math.sqrt(n)) + 1, 2):
          if n % i == 0:
              return False
      return True
  </code></pre>
  
  <p>if I run:</p>
  
  <pre><code>[x for x in range(2, num) if is_prime(x)]
  </code></pre>
  
  <p>i get the desired result</p>
  
  <pre><code>[2, 3, 5, 7, 11, 13, 17, 19]
  </code></pre>
  
  <p>in trying to convert the whole function to a list comprehension I came up with:</p>
  
  <pre><code>[x if not (x%2==0 and x &gt; 2) else x for x in range (2, num) 
       for i in range(3, int(x**(1/2))+1, 2) if not (x%i==0)]
  </code></pre>
  
  <p>which produces:</p>
  
  <pre><code>[10, 11, 13, 14, 16, 17, 19]
  </code></pre>
  
  <p>not sure where I am going wrong and would appreciate some help.  In truth I think it is better to use the function in this case, but like I said I am trying to understand the list comprehension and its capabilities.</p>