title: Specify a range (upper and lower bound) for a variable in Python
body: <p>Is there a way to specify bounds for a float variable in Python? For example, when I import data, I would like to either check if it within certain range such that <code>min &lt;= variable &lt;=max</code>. Further, if possible, I would like assign a specific value to such variable if the imported value is outside of these bounds (or even missing). </p>
  
  <p>I can perhaps do the first part if the variable was a float.</p>
  
  <pre><code>def var_check(x,lower_bound=3,upper_bound=30):
      rng = range(lower_bound,upper_bound+1)
      if x not in rng:
          return (upper_bound-lower_bound)/2
      else:
          return x
  </code></pre>
  
  <p><code>x = var_check(5)</code> returns 5, while <code>x = var_check(50)</code> returns 13.</p>
  
  <p>Any ideas on how to do this for a float variable?</p>