Les bases
Le type de variable
Pour connaitre le type d'une variable il existe la fonction type(). Exemple:
>>> x = 1>>> type(x)<type 'int'>>>> x = 1.0>>> type(x)<type 'float'>>>> x = 1.0 + 2.0j>>> type(x)<type 'complex'>
Tester le type d'une variable
>>> x = 1>>> type(x) is floatFalse>>> type(x) is intTrue
L'addition
nombres entiers
>>> x = 1>>> y = 3>>> z = x + y>>> z4
nombres reels
>>> x = 1.0>>> y = 3.0>>> z = x + y>>> z4.0
nombres complexes
>>> z1 = 1.0 + 1.0j>>> z2 = 3.0 + 2.0j>>> z3 = z1 + z2>>> z3(4+3j)
La soustraction
nombres entiers
>>> x = 1>>> y = 3>>> z = x - y>>> z-2
nombres reels
>>> x = 1.0>>> y = 3.0>>> z = x - y>>> z-2.0
nombres complexes
>>> z1 = 1.0 + 1.0j>>> z2 = 3.0 + 2.0j>>> z3 = z2 - z1>>> z3(2+1j)
La division
nombres entiers
>>> x = 2>>> y = 3>>> z = y / x>>> z1>>> type(z)<type 'int'>
nombres reels
>>> x = 2.0>>> y = 3.0>>> z = y / x>>> z1.5>>> type(z)<type 'float'>
nombres complexes
>>> z1 = 1.0 + 1.0j>>> z2 = 3.0 + 2.0j>>> z3 = z2 / z1>>> z3(2.5-0.5j)>>> type(z3)<type 'complex'>
La multiplication
nombres entiers
>>> x = 2>>> y = 3>>> z = x * y>>> z6
nombres reels
>>> x = 1.7>>> y = 3.2>>> z = x * y>>> z5.44
nombres complexes
Multiplication d'un complexe avec un nombre réel
>>> z1 = 3.0 + 2.0j>>> z2 = 2.0 * z1>>> z2(6+4j)
Multiplication de deux complexes
>>> z1 = 3.0 + 2.0j>>> z2 = 2.0 + 1.0j>>> z3 = z1 * z2>>> z3(4+7j)
La puissance
nombres entiers
>>> x = 2>>> y = 3>>> z = x ** y>>> z8
nombres reels
>>> x = 2.0>>> y = 0.5>>> z = x ** y>>> z1.4142135623730951
nombres complexes
>>> z1 = 3.0 + 2.0j>>> z2 = z1 ** 2.0>>> z2(5+12j)>>> z1 = 3.0 + 2.0j>>> z2 = 2.0 + 1.0j>>> z3 = z1 ** z2>>> z3(-5.600430345839602+4.557757310292927j)
Arrondir un nombre réel
>>> round(0.8660254037844387,2)0.87>>> round(0.8660254037844387,3)0.866>>> round(0.8660254037844387,1)0.9>>> round(0.8660254037844387,5)0.86603
Avec les modules math et cmath
>>> import math>>> import cmath
Constantes mathématiques
Le nombre $\pi$
>>> import math>>> math.pi3.141592653589793
Le nombre d'Euler
>>> math.e2.718281828459045
Les fonctions trigonométriques
Conversion degrees-radian
Le cosinus
>>> math.cos(math.pi/3)0.5000000000000001>>> math.cos(math.pi/2)6.123233995736766e-17>>> math.cos(math.pi/6)0.8660254037844387
Le sinus
>>> math.sin(math.pi/3)0.8660254037844386>>> math.sin(math.pi/2)1.0>>> math.sin(math.pi/6)0.49999999999999994
La fonction exponentielle et logarithme
La fonction exponentielle
>>> math.exp(1)2.718281828459045>>> math.exp(2)7.38905609893065>>> math.exp(0)1.0
La fonction logarithme népérien
>>> math.log(math.e)1.0>>> math.log(4)1.3862943611198906
La fonction logarithme en base 10
>>> math.log10(10)1.0>>> math.log10(100)2.0
Recherches associées
| Liens | Site |
|---|---|
| python docs | python |
| python docs | python |
| scipy | scipy |
