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 4
Open a terminal. Depending on your OS, run the following commands:
# on Unix
which python
which python3
or
# 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 5
Run a script with python3 (helloworld.py is just a text file):
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:
cat common/examples/helloworld.py
Work interactively with IPython#
The command ipython launches the program IPython, which is used
for interactive python.
Exercise 6
In IPython, you can execute a first interactive instruction:
2 + 2
4
3 / 4
0.75
Run the script from IPython.
%run ../common/examples/helloworld.py
Hello world
My name is Pierre
In the file helloworld.py, a variable called name is defined.
print(name)
Pierre
Help on an object can be displayed with the question mark (try it):
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…
Python in an IDE (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).
Python in the browser (JupyterLab)#
The presentations of this python training are made with Jupyter (demonstration).
This is a very powerful tool to present results (see these examples).
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.
JupyterLite
JupyterLite is a JupyterLab distribution that runs entirely in the browser.
You can use the JupyterLite of the training. In this case, the code of the notebook runs on your computer (but through your web browser).