title: How to set a voltage sweep effectively in just one command? Python
body: <p>I am trying to do a Current-Voltage sweep using PyVisa with a Keithley 6430 doing a complete loop of voltages going from zero to both positive and negative values: from 0 to +5, back to 0 and from 0 to -5 and back to 0 again.</p>
  
  <p>To do that I basically used 4 different ranges and 4 different <em>for</em> loops like so:</p>
  
  <pre><code> #Voltage values
   low = -5
   middle = 0
   high = 5
   step = 1
  
   voltage_range1 = np.arange(middle,high,step)
   voltage_range2 = np.arange(high,middle,-step)
   voltage_range3 = np.arange(middle,low,-step)
   voltage_range4 = np.arange(low,middle+1,step)
  
   data = [] 
  
   for voltage in voltage_range1:
   keithley.write('source:voltage:level {}'.format(voltage)) 
   data.append(keithley.query_ascii_values('read?'))
  
   for voltage in voltage_range2:
   keithley.write('source:voltage:level {}'.format(voltage)) 
   data.append(keithley.query_ascii_values('read?'))
  
   for voltage in voltage_range3:
   keithley.write('source:voltage:level {}'.format(voltage)) 
   data.append(keithley.query_ascii_values('read?'))
  
   for voltage in voltage_range4:
   keithley.write('source:voltage:level {}'.format(voltage)) 
   data.append(keithley.query_ascii_values('read?'))
  </code></pre>
  
  <p>The program works properly, but I couldn't find a more functional way to do this, like setting the whole process in just one <em>voltage_range</em> and avoid using 4 loops. Do you have any ideas? </p>