Note Avant toute chose, veuillez choisir votre langue dans le menu Langs
(attention à ne pas avoir les 2 langues en même temps).
Note First of all, please choose your language from the Langs
menu (be careful not to have both languages at the same time).
Python as a calculator¶
The Python programming language can be used interactively to know that when you type a command, Python executes it immediately and displays the answer:
1+1
2
Notes on the environment Jupyter¶
- A cell can have 3 states:
- not selected (no frame around the cell) * selected (blue frame)
- selected and in edit mode (green frame)
- Click to select a cell (or sometimes passes it directly into edit mode for calculation cells)
- Double click selects and goes into edit mode (on a text box, the Markdown syntax appears)
- Enter like clicking allows to go down in the states when Esc allows to go up
- To have Python recalculate the result of a cell, do Shift + Enter (display the pretty display for text cells)
Exercise: modify the calculation cell above to calculate 1 + 2
print(1 + 7*2)
print(49**0.5)
print(7 % 5)
15 7.0 2
The print
command allows to display a result which is different from the result of the cell (which is the result of the last command).
Python has basic operators (including the **
operators for power and %
for the modulo).
For more advanced mathematical functions we must import libraries (we will see later what libraries are). We start with the scientific library NumPy
:
from numpy import *
print(sin(3.14))
print(sin(pi))
print(log(e))
0.0015926529164868282 1.2246467991473532e-16 1.0
log2(16)
4.0
For documentation on a function use ?
(Specific to iPython). In regular Python we do help (log)
. You have to run the cell (Shift + Enter) to see the result in a pane at the bottom of the page (click on the cross to close it or type Esc on a cell in edit mode).
?log
Complexes¶
Complexes exist but the imaginary number is j
and you have to write 1j
and not just j
:
1j**2
(-1+0j)
sqrt(1j)
(0.7071067811865476+0.7071067811865476j)
e**(pi*1j)
(-1+1.2246467991473532e-16j)
Note the rounding error in the $e^{i \pi}$ calculation for the imaginary part.
The order of priority of the operators¶
The order of priority of the operators is:
**
the power-x
,+ x
the unit operator minus or plus*
,/
,//
(//
being the entire division)%
the modulo+
,-
print( -3**2 )
print( 9/3*3 )
print( 9/(3*3) )
-9 9.0 1.0
Errors¶
Finally, we check the error messages to correct the code:
{{ PreviousNext("../lesson0 Introduction/Introduction.ipynb", "01 - Les variables.ipynb") }}