title: Checking if value in list. Returning within or outside loop?
body: <p>I'm new to Python programming. I'd like some guidance as to the reason behind the output. I was just doing some exercise online and came across a problem which I could not understand. The code goes like this: </p>
  
  <pre><code>def dataset(data,n):    
      for value in data:
          if n == value:
              return True
          else:
              return False
  
  print(dataset([1,5,8,3], 3))
  print(dataset([1,5,8,3],-1))
  </code></pre>
  
  <p>The output would be <code>False, False</code> which is the incorrect answer. </p>
  
  <p>After some tinkering, I found out the problem. The correct code is as follows: </p>
  
  <pre><code>def dataset(data,n):
      for value in data:
          if n == value:
              return True
  
      return False
  
  print(dataset([1,5,8,3], 3))
  print(dataset([1,5,8,3],-1))
  </code></pre>
  
  <p>The output is <code>True, False</code> which is the correct answer. </p>
  
  <p>So my question is why the different output with just a different putting of the <code>return False</code>and <code>else: return False</code> ? </p>