Getting started#
Python is a very versatile programming language and can especially be used for scientific programming and image processing. Python’s syntax is very similar to Matlab’s one. Before beginning this lab, you may need to read Installing and using Python. If you use your personal computer, beware of the module version. We will use Jupyter Lab, which runs in a web browser, to write programs. These programs are saved as notebooks.
First, boot your computer on Ubuntu, then open a terminal by typing
terminal
in the main menu or typingCtrl
+Alt
+T
.Start Jupyter by typing in a terminal:
jupyter lab
or
jupyter-lab
In Jupyter Lab, open a new Python 3 notebook and rename it from
File
>Save Notebook As...
. A notebook is a file with extension .ipynb.
Now you are ready to write a Python program in the notebook.
In the first cell of the notebook, write
40 + 2
and type
Shift
+Return
. The code is executed, the result is displayed then a new cell appears below.Like any programming language, the code is written using variables and functions. A variable stores one (or more) values, whether it is numeric or not. The name of the variable can contain letters, numbers (except the first character) or underscore. Case is important (i.e.
a
andA
are two different variables). Type the instructions below in the second cell:year = 2024 course = "BIP"
and type again
Shift
+Return
. Now the value 2024 is stored in the variableyear
and the character string “BIP” is stored in the variablecourse
.Modify the previous cell by adding the following statement:
print(f"{course} {year}")
print
is a function and the string in the brackets is an argument. Here, this argument is a character string. Thef
at the beginning of the string means that it is a formatted string. TypeShift
+Return
to run again the code.
A notebook is appealing as it is also possible to add text using the markdown language.
Select an empty cell, then click on the drop-down list in the toolbar to select Markdown. Then you can write formatted text. Try to write:
New exercise
Write bold, italic or equations: \(\sqrt{2}\).
This can be useful for inserting titles or keeping comments and notes.
Verify your code in the correction below.
Correction#
Objectives#
discover and know how to write a Python program
discover and know how to use a notebook
write by using the markdown syntax
manipulate variables
Code cells#
The two cells containing codes should be:
40 + 2
42
year = 2024
course = "BIP"
print(f"{course} {year}")
BIP 2023
Markdown cell#
The markdown cell should be:
## New exercise
Write **bold**, _italic_ or equations: $\sqrt{2}$.
to get:
New exercise
Write bold, italic or equations: \(\sqrt{2}\).