Modules & bibliothèque standard

Python ↔ Baobab
11 correspondances

importer/depuis traduisent import/from. La bibliothèque standard francise les modules essentiels : maths pour math, aleatoire pour random.

PythonBaobabDescription
import mathimporter mathsImporte le module mathématique.
from math import sqrtimporter racine depuis mathsImporte une seule fonction du module.
math.sqrt(x)maths.racine(x)Racine carrée.
math.pimaths.piLa constante π.
math.floor(x) / math.ceil(x)maths.plancher(x) / maths.plafond(x)Arrondi à l'entier inférieur / supérieur.
import randomimporter aleatoireImporte le module de hasard.
random.randint(a, b)aleatoire.entier(a, b)Entier aléatoire entre a et b inclus.
random.choice(l)aleatoire.choix(l)Élément tiré au hasard dans une liste.
random.shuffle(l)aleatoire.melanger(l)Mélange une liste sur place.
random.random()aleatoire.nombre()Décimal aléatoire entre 0 et 1.
import utils (fichier local)importer utils depuis "./utils.bao"Importe un module de votre projet.
Exemple complet - Baobab ↔ Python
Baobab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
importer maths importer aleatoire afficher(maths.racine(144)) # → 12.0 afficher(maths.pi) # → 3.141592653589793 afficher(maths.plancher(3.9)) # → 3 afficher(maths.plafond(3.1)) # → 4 tirage = aleatoire.entier(1, 6) afficher(f"Le dé donne {tirage}") equipe = ["Amina", "Kofi", "Fatou"] capitaine = aleatoire.choix(equipe) aleatoire.melanger(equipe) afficher(f"Capitaine : {capitaine}") afficher(f"Ordre de passage : {equipe}") # Module local du projet importer utils depuis "./utils.bao" afficher(utils.additionner(2, 3))
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import math import random print(math.sqrt(144)) # → 12.0 print(math.pi) # → 3.141592653589793 print(math.floor(3.9)) # → 3 print(math.ceil(3.1)) # → 4 tirage = random.randint(1, 6) print(f"Le dé donne {tirage}") equipe = ["Amina", "Kofi", "Fatou"] capitaine = random.choice(equipe) random.shuffle(equipe) print(f"Capitaine : {capitaine}") print(f"Ordre de passage : {equipe}") # Local project module import utils print(utils.additionner(2, 3))