In [1]:
from __future__ import print_function
from IPython.html.widgets import interact, interactive, fixed
from IPython.html import widgets

In [4]:
def f(a, b, c, d, **kwargs):
    x=np.linspace(-10, 10, 20)
    y = a*(x**3) + b*(x**2) + c*x + d
    
    title="$f(x) = (%s)x^{3} + (%s)x^{2} + (%s)x + (%s)$" % (a,b,c,d)
    
    plt_arrays(x,y, title=title, **kwargs)

In [6]:
#This function plot x, y and adds a title
def plt_arrays(x, y, title="", color="red", linestyle="dashed", linewidth=2):
    fig = plt.figure()
    axes = fig.add_subplot(111)
    axes.plot(x,y, color=color, linestyle=linestyle, linewidth=linewidth)
    axes.set_title(title)
    axes.grid()
    plt.show()

In [7]:
#Define Constants
a=0.25
b=2
c=-4
d=0

f(a, b, c, d)



In [8]:
i = interact(f,
             a=(-10.,10),
             b=(-10.,10),
             c=(-10.,10),
             d=(-10.,10),
             color = ["red", "blue", "green"],
             linestyle=["solid", "dashed"],
             linewidth=(1,5)
             )



In [10]:
def f2(p, **kwargs):
    a = p[0]
    b = p[1]
    c = p[2]
    d = p[3]
    x=np.linspace(-10, 10, 20)
    y = a*(x**3) + b*(x**2) + c*x + d
    
    title="$f(x) = (%s)x^{3} + (%s)x^{2} + (%s)x + (%s)$" % (a,b,c,d)
    
    plt_arrays(x,y, title=title, **kwargs)

In [11]:
i2 = interact(f2,
             p=(-10.,10),
             color = ["red", "blue", "green"],
             linestyle=["solid", "dashed"],
             linewidth=(1,5)
             )


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-10-dd358c677d32> in f2(p, **kwargs)
      1 def f2(p, **kwargs):
----> 2     a = p[0]
      3     b = p[1]
      4     c = p[2]
      5     d = p[3]

TypeError: 'float' object has no attribute '__getitem__'

In [12]:
@interact(x=True, y=1.0)
def g(x, y):
    print(x, y)


True 1.4

In [13]:
x = [1,2,3,4]

In [19]:
for n, val in enumerate(x):
    globals()["var%d"%n] = val

In [27]:
def manyArgs(*arg):
    print("I was called with the following arguments:")
    for i,val in enumerate(arg):
        print(val)

In [28]:
manyArgs(1,3,'dog',[1,2])


I was called with the following arguments:
1
3
dog
[1, 2]

In [26]:
help(enumerate)


Help on class enumerate in module __builtin__:

class enumerate(object)
 |  enumerate(iterable[, start]) -> iterator for index, value of iterable
 |  
 |  Return an enumerate object.  iterable must be another object that supports
 |  iteration.  The enumerate object yields pairs containing a count (from
 |  start, which defaults to zero) and a value yielded by the iterable argument.
 |  enumerate is useful for obtaining an indexed list:
 |      (0, seq[0]), (1, seq[1]), (2, seq[2]), ...
 |  
 |  Methods defined here:
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |  
 |  __iter__(...)
 |      x.__iter__() <==> iter(x)
 |  
 |  next(...)
 |      x.next() -> the next value, or raise StopIteration
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T


In [ ]: