# Python

The version of Python available on your NumWorks calculator is MicroPython 1.9.4, compatible with Python 3.4.

## Scripts

### The script list

When you enter the application, you will see the list of saved scripts. When you first use the application, three scripts are defined as examples: factorial.py, mandelbrot.py and polynomial.py.

### Adding and removing a script from the list

You can add up to 8 scripts in the list.

1. Select the Add a script cell at the bottom of the list.
2. Confirm by pressing OK.

A new script appears in the list. You can then enter a name for this script.

To delete a script, select the settings icon next to the script name and press OK. Choose Delete script and press OK.

### Renaming a script

To change a script name, select the settings icon next to the script name and press OK. Choose Rename script and press OK to confirm. You can now change the name of the script.

### Editing a script

To write to a script, simply select the script name and press OK. The editor opens and you can write your algorithms inside.

To help you write, press Toolbox. A menu will open and show some shortcuts to make the editing easier. The menu Loops and Tests offers pre-filled blocks for for and while loops, if tests and a series of conditions. The menu Catalog lists the functions present in Python and gives a short description of them. You can also use the var key to display the list of functions defined in your scripts as well as the global variables.

### Disabling automatic import into the shell

Automatic import is automatically enabled for your scripts. This means that the command from script_name import * is systematically entered when the shell is opened so that you can use the functions you defined in the scripts inside the console.

To disable automatic import of a script, select the settings icon next to the script name and press OK. Choose Auto import in shell and press OK to toggle the switch. The switch turns grey and the script will no longer be activated automatically.

## The shell

At the bottom of the list of scripts is a button Python shell which allows access to the interactive shell of Python.

The triple arrows >>> prompts you to enter a command.

You can use the shortcuts on the Toolbox menu to make text entry easier. You will also find in the menu of the var key the list of functions you have defined in your scripts.

## Modules

The modules present in this version of Python are the math, cmath, random and kandinsky modules.

### The math module

Here is the complete description of the math module. You can get this list on your calculator by pressing Toolbox and going to Modules then math.

Function Description
e The constant e=2.718281828459045
pi The constant pi=3.141592653589793
sqrt(x) Square root, type sqrt(x) for $\sqrt{x}$
pow(x,y) Power, type pow(x,y) for $x^y$
exp(x) Exponential, type exp(x) for $e^x$
expm1(x) Exponential minus 1, type expm1(x) for $e^x-1$
log(x) Natural logarithm: log(x) calculates $ln(x)$
log2(x) Base-2 logarithm, type ln2(x) for $\frac{ln(x)}{ln(2)}$
log10(x) Base-10 logarithm, type ln10(x) for $\frac{ln(x)}{ln(10)}=log(x)$
cosh(x) Hyperbolic cosine
sinh(x) Hyperbolic sine
tanh(x) Hyperbolic tangent
acosh(x) Inverse hyperbolic cosine
asinh(x) Inverse hyperbolic sine
atanh(x) Inverse hyperbolic tangent
cos(x) Cosine in radians
sin(x) Sine in radians
tan(x) Tangent in radians
acos(x) Arc cosine
asin(x) Arc sine
atan(x) Arc tangent
atan2(y,x) Type atan2(y,x) to calculate $atan(\frac{y}{x})$
ceil(x) Ceiling
copysign(x,y) Returns xwith the sign of y, for instance copysign(3,-1)=-3
fabs(x) Absolute value, fabs(x) returns $\mid x\mid$
floor(x) Floor, type floor(x) to calculate $\lfloor x \rfloor$
fmod(a,b) fmod(a,b) returns $a$ modulo $b$
frexp(x) Mantissa and exponent of x: for instance, frexp(10) returns (0.625,4) because $10=0.625\times2^4$
ldexp(x,i) Inverse of frexp(x), that is $x\times2^i$
modf(x) Fractional and integer parts, for instance modf(5.1)=(0.1,5.0)
isfinite(x) Checks if xis finite
isinf(x) Checks if xis infinity
isnan(x) Checks if x is NaN
trunc(x) Returns x truncated to an integer, for instance trunc(6.7)=6
radians(x) Converts x from degrees to radians, for instance radians(180) returns 3.141592653589793
degrees(x) Converts x from radians to degrees, for instance degrees(pi) returns 180
erf(x)  Error function, $erf(x) = \frac{2}{\pi}\int_0^x e^{-t^2} \, \mathrm dt$
erfc(x) Complementary error function, $erfc(x) = 1-erf(x)$
gamma(x)  Gamma function
lgamma(x) Log-gamma, $lgamma(x)=ln(gamma(x))$

### The cmath module

Here is the complete description of the cmath module. You can get this list on your calculator by pressing Toolbox and going to Modules then cmath.

Function Description
e The constant e=2.718281828459045
pi The constant pi=3.141592653589793
phase(z) Phase of z in radians, for instance phase(1j)=1.570796326794897
polar(z) Representation of z in polar coordinates: polar(1j) returns (1.0, 1.570796326794897)
rect(z) Representation of z in cartesian coordinates: rect(1,pi/4) returns 0.70710+0.70710j
exp(x) Exponential function, for instance exp(i*pi/4) returns 0.70710+0.70710j
log(x) Natural logarithm, for instance log(1j) returns 1.570796326794897j
sqrt(x) Square root
cos(x) Cosine
sin(x) Sine

### The random module

Here is the complete description of the random module. You can get this list on your calculator by pressing Toolbox and going to Modules then random.

Function Description
getrandbits(k) Returns an integer with k random bits
seed(x) Initialize the random number generator
randrange(start,stop) Returns a random number in range(start,stop)
randint(a,b) Returns an integer in [a,b]
choice(list) Returns a random number in the list
random() Returns a random floating point number in [0,1[
uniform(a,b) Returns a random floating point number in [a,b]

### The kandinsky module

Here is the complete description of the kandinsky module. You can get this list on your calculator by pressing Toolbox and going to Modules then kandinky.

Function Description
color(r,g,b) Defines the color from the values of r,g,b
get_pixel(x,y) Returns the pixel x,y color
set_pixel(x,y,color) Colors the pixel x,y of the color color
draw_string(text,x,y) Displays text from the pixel x,y

## Toolbox and var keys

### The var key

The menu of the var key lists all the functions defined in your scripts (containing no errors) as well as the global variables.

### The Toolbox key

The Toolbox key menu contains four sections for the faster editing of your scripts.

Section Description
Loops and tests Contains instructions for for and while loops as well as if tests.
Modules Contains the functions available in the math, cmath, random and kandinsky modules.
Catalog Contains the functions that can be used in Python, especially those of the modules but also functions like print() and input(). An alphabetical search with the letters of the keyboard is possible.
Functions Contains instructions for defining a function: def function(argument): and return.