# Interact Basics

## Import


```python
from ipywidgets import interact, interactive, fixed
from IPython.display import display
```

## Adding numbers

Write a `print_sum` function that `prints` the sum of its arguments `a` and `b`.


```python
def print_sum(a, b):
    """Print the sum of the arguments a and b."""
    print(a+b)
```

Use the `interact` function to interact with the `print_sum` function.

* `a` should be a floating point slider over the interval `[-10., 10.]` with step sizes of `0.1`
* `b` should be an integer slider the interval [-8, 8] with step sizes of `2`.


```python
interact(print_sum, a=(-10.0,10.0,0.1), b=(-8,8,2));
```

## Optional flags
Write a function named `print_string` that prints a string and additionally prints the length of that string if a boolean parameter is `True`.


```python
def print_string(s, length=False):
    """Print the string s and optionally its length."""
    print(s)
    if length:
        print(len(s))
```

Use the `interact` function to interact with the `print_string` function.

* `s` should be a textbox with the initial value `"Hello World!"`.
* `length` should be a checkbox with an initial value of `True`.


```python
interact(print_string, s='Hello World', length=True);
```

# Using SymPy to play with Polynomials

We now show that IPython's `interact` capabilities are not limited to basic Python types, with an exercise that uses [SymPy](http://sympy.org/en/index.html) to factor polynomials.  

We begin by initializing SymPy to display its results with proper mathematical formatting in the browser:


```python
from sympy import Symbol, Eq, factor, init_printing
init_printing(use_latex='mathjax')
```

Now, we create a symbolic variable, $x$, to build symbolic polynomials with:


```python
x = Symbol('x')
```

We can then, for example, factor a polynomial:


```python
factor(x**5-1)
```

## Exercise
Write a function that takes an argument $n$, and will display both the polynomial $x^n-1$ and its factorization.

Then, use IPython's `interact` to provide a control to visualize how the result varies with a slider control that allows $n$ to vary over the range from 2 to 40.

*Hint:* Look up the help for the sympy function `Eq`.


```python
def factorit(n):
    display(Eq(x**n-1, factor(x**n-1)))
```

Notice how the output of the `factorit` function is properly formatted LaTeX.


```python
factorit(12)
```


```python
interact(factorit, n=(2,40));
```

d84b7278c5dd9ce4c2e9326b7e9947249b11bced
ipynb
Jupyter Notebook
intro_python/python_tutorials/jupyter-notebook_intro/exercises/InteractBasics-solution.ipynb
cgentemann/tutorials
04f04ed43f6e35d70c9037e54624b5b476ed9666
Apache-2.0
intro_python/python_tutorials/jupyter-notebook_intro/exercises/InteractBasics-solution.ipynb
cgentemann/tutorials
04f04ed43f6e35d70c9037e54624b5b476ed9666
Apache-2.0
intro_python/python_tutorials/jupyter-notebook_intro/exercises/InteractBasics-solution.ipynb
cgentemann/tutorials
04f04ed43f6e35d70c9037e54624b5b476ed9666
Apache-2.0
Qwen/Qwen-72B
1. YES
2. YES