In your Python code, you can write your own functions, but you can also import functions from one of the available Python modules. All available Python modules can be found in the authoring manual.

**Jump to: **

In this article, you learned how to create and edit variables. It was mentioned that variables are written in PHP, but this programming language doesn’t suffice for things like complex mathematical operations. Instead, we want to use the programming language Python. It is also possible to use Maxima (see this article) or R (see this article) for variable definitions.

❗ Note that this article does not explain how to write code using Python. For an introduction to Python, we refer to any tutorial that you can find using your search engine. Moreover, note that this article should not be confused with the article Exercise type: Python. In the exercise type Python, the student should write Python code in the answer field, whereas this article is just about defining variables using Python.

To explain how defining variables using Python works, we start with an easy example. Say we want to make an exercise where students have to add two random integers together. Using only PHP, the Variables tab of this exercise would look something like this:

**Step 1**

If instead, we want to use Python, we need to follow some steps. First, we create a variable that will be a Python code block. For the example where we add two integers, this can look as follows:

The function

`sw_Python`

is a SOWISO-made PHP function that evaluates its argument as Python code.Here we import all packages that we need in our Python code. The packages that are mentioned in the code above should always be imported, but we can import more if necessary.

This part defines a function called

`random_exclude(n,m,list)`

. When called, this function generates a random integer in the closed interval [n,m] excluding all integers in list. When no integers should be excluded, the list should be an empty list`[].`

This is the most important part, namely where we generate our variables (

`number_1`

and`number_2`

) and the solution (`sum`

).The function

`output[list]`

returns all elements in its list. We need to write all elements in the list twice: once in a normal way (like`sum`

) and once with the function`latex()`

around it (`like latex(sum)`

). The elements that are written in a normal way will be outputted in ‘Python language’ and the elements with the`latex()`

function around it will be outputted in ‘LaTeX language’.In the preview of the variable

`$code`

, we see the array that was returned by the function`output[list]`

.

Note that in the example above, the output of, for instance, `sum`

and `latex(sum)`

looks the same. This is because an integer in ‘Python language’ looks the same as an integer in ‘LaTeX language’. In general, however, the output looks different, as can be seen in the following picture:

**Step 2**

After writing our Python code block, we need to export the elements from the output list of `$code`

to SOWISO variables that we can use in the exercise. To do this, we make new variables that have Definition `$code[i]`

for all i in the array, as in the image below:

The variables you just created can be used in, for instance, the “Texts”, “Positive feedback”, “Negative feedback” and “Solution” tabs of the exercise.

**Using function variables in your Python code**

Let’s say we want to define the function **f(x)=3*x+5** in our Python code. To do this, we need to make sure that the letter x is seen as a function variable and not a Python variable in the code. This can be done by using the function `symbols`

from the Python package Sympy. This looks as follows:

More information and documentation on the symbols function can be found here.

#### Summary

In short, we need to do the following steps to create variables using Python code:

1. Write a SOWISO variable named `$code`

that contains all variables you need. Make sure to use the function `symbols`

to define any function variables if applicable.

2. Write all variables of your code in the output list. Once in a normal way (like `variable`

) and once with the function `latex()`

around it (like `latex(variable)`

).

3. Export the elements in the output array to SOWISO variables by using the variable Definitions `$code[i]`

for all i.

Below we provide some code that you can copy-paste into your `$code`

variable:

sw_Python("

from sympy import latex, symbols

import numpy as np

#Function to create random integers in the interval [n,m] excl all integers in list.

#If no integers should be excluded, list should be an empty list: []

def random_exclude(n,m,list):

listnp = np.array(list)

listnp = listnp[ ((listnp>=n) & (listnp<=m)) ]

numb = np.random.choice(np.setdiff1d(np.arange(n,m+1),list))

return numb.item()

#ASSIGN THE LETTERS THAT WILL BE VARIABLES IN A FUNCTION,

#FOR EXAMPLE x,y = symbols('x y') (look at sympy package for documentation)

var = 42

output = [

var, latex(var)

]

for i in output:

print(i)

")