title: when to use if vs elif in python
body: <p>If I have a function with multiple conditional statements where every branch gets executed returns from the function. Should I use multiple if statements, or if/elif/else? For example, say I have a function:</p>
  
  <pre><code>def example(x):
      if x &gt; 0:
          return 'positive'
      if x &lt; 0:
          return 'negative'
      return 'zero'
  </code></pre>
  
  <p>Is it better to write:</p>
  
  <pre><code>def example(x):
      if x &gt; 0:
          return 'positive'
      elif x &lt; 0:
          return 'negative'
      else:
          return 'zero'
  </code></pre>
  
  <p>Both have the same outcome, but is one more efficient or considered more idiomatic than the other?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>A couple of people have said that in the first example both if statements are always evaluated, which doesn't seem to be the case to me</p>
  
  <p>for example if I run the code:</p>
  
  <pre><code>l = [1,2,3]
  
  def test(a):
      if a &gt; 0:
          return a
      if a &gt; 2:
          l.append(4)
  
  test(5)
  </code></pre>
  
  <p>l will still equal [1,2,3]</p>