---
jupytext:
  formats: md:myst
  text_representation:
    extension: .md
    format_name: myst
    format_version: 0.13
kernelspec:
  display_name: Python 3
  language: python
  name: python3
---

# Different ways to use Python

## Python: a language and some interpreters

Python is a programming language.

The most common way to execute Python code is to **interpret** it. Through misuse of
language, one says that Python is an **interpreted** language (like Bash, Matlab, and in
contrast with Fortran, C or C++). To interpret code, we need an interpreter, i.e. a
program parsing Python code and running computer instructions.

Most of the time, users of Python software don't need to know where is the interpreter.
However, as a Python developer, it is usually good to have an idea about which
interpreter is used.

:::{exercise}
Open a terminal. Depending on your OS, run the following commands:

```sh
# on Unix
which python
which python3
```

or

```powershell
# on Windows (PowerShell)
Get-Command python
Get-Command py
```

The results depend on your computer state...

On Linux, you should have the system Python (a command `python3` corresponding to a file
`/usr/bin/python3`). On Linux, when no virtual environment is activated, there is usually
no `python` command.
:::

## Execute a script with the command `python3`

::::{exercise}
:label: exercise-exec-script

Run a script with `python3` (`helloworld.py` is *just* a text file):

```bash
python3 common/examples/helloworld.py
```

:::{note}
`cat` is a POSIX command that prints the content of a text file. You can use it to read
the content of the file:

```sh
cat common/examples/helloworld.py
```
:::
::::

## Work interactively with [IPython](https://ipython.org/)

The command `ipython` launches the program [IPython](https://ipython.org/), which is used
for interactive python.

```{exercise-start}
---
label: ex-ipython
---
```

- In IPython, you can execute a first interactive instruction:

```{code-cell}
2 + 2
```

```{code-cell}
3 / 4
```

- Run the script from IPython.

```{code-cell}
%run ../common/examples/helloworld.py
```

In the file `helloworld.py`, a variable called `name` is defined.

```{code-cell}
print(name)
```

- Help on an object can be displayed with the question mark (try it):

```ipython
name?
```

- Try to type `name.` and to press on tab... The tab key is used to tell you how what you
  typed can be completed.

- Try to use the top and bottom arrows...

```{exercise-end}
```

## Python in an IDE ([Spyder](https://pythonhosted.org/spyder/))

Launch the application Spyder, a Python IDE (Integrated Development Environment).

- a good code editor with:
  - syntax coloring,
  - code analysis powered by pyflakes and pylint,
  - introspection capabilities such as code completion.
- IPython console
- variable inspector
- ...

```{important}
It is very important to use a good editor to code in Python (for example Spyder,
emacs or vi (**with a good setup!**), or
[Visual Studio Code](https://code.visualstudio.com/docs/languages/python)).
```

## Python in the browser ([JupyterLab](https://jupyter.readthedocs.io))

The presentations of this python training are made with Jupyter (demonstration).

This is a very powerful tool to present results (see these
[examples](http://nbviewer.jupyter.org/)).

```bash
jupyter-lab
```

```{note}

Jupyter is a web application. It works in a server-client mode. The command `jupyter-lab`
opens a server which can be used locally in your browser. One can also use a distant
Jupyter server running on another computer.

The code of the notebooks runs on the machine hosting the server.

```

```{admonition} JupyterLite

[JupyterLite](https://jupyterlite.readthedocs.io) is a JupyterLab
distribution that runs entirely in the browser.

You can use
[the JupyterLite of the training](https://python-cnrs.netlify.app/edu/lite/lab/index.html).
In this case, the code of the notebook runs on your computer (but through your web
browser).

```
