Initial commit
This commit is contained in:
6
training/code/04-functions/01-base/base_01.py
Normal file
6
training/code/04-functions/01-base/base_01.py
Normal file
@ -0,0 +1,6 @@
|
||||
"""Déclaration de fonction simple."""
|
||||
def show_something():
|
||||
print("Something")
|
||||
|
||||
# Exécuter la fonction
|
||||
show_something()
|
8
training/code/04-functions/01-base/base_02.py
Normal file
8
training/code/04-functions/01-base/base_02.py
Normal file
@ -0,0 +1,8 @@
|
||||
"""Fonction simple retournant une constante."""
|
||||
def return_constant():
|
||||
print("Texte : 99")
|
||||
return 99 # renvoie un nombre entier
|
||||
|
||||
# Appeler la fonction et récupérer la valeur renvoyée
|
||||
result = return_constant()
|
||||
print(result)
|
10
training/code/04-functions/01-base/base_03.py
Normal file
10
training/code/04-functions/01-base/base_03.py
Normal file
@ -0,0 +1,10 @@
|
||||
# Fonction produit
|
||||
def product(x, y):
|
||||
return x * y
|
||||
|
||||
|
||||
# Tests d'appel de la fonction
|
||||
print(product(10, 10))
|
||||
print(product(15, -15))
|
||||
print(product("hello ", 3))
|
||||
print(product([1, 2, 3], 2))
|
22
training/code/04-functions/01-base/base_04.py
Normal file
22
training/code/04-functions/01-base/base_04.py
Normal file
@ -0,0 +1,22 @@
|
||||
"""Fonction qui calcule un quotient."""
|
||||
|
||||
|
||||
def quotient(numerator, denominator=1):
|
||||
"""
|
||||
Fonction de quotient.
|
||||
|
||||
Renvoie `None` si l'argument `denominator` vaut 0.
|
||||
|
||||
"""
|
||||
try:
|
||||
return numerator / denominator
|
||||
except ZeroDivisionError:
|
||||
# Si impossible d'évaluer l'expression ligne 4
|
||||
return None
|
||||
|
||||
|
||||
# Tester plusieurs arguments pour la fonction
|
||||
print(quotient(10, denominator=4))
|
||||
print(quotient(10, denominator=-1))
|
||||
print(quotient(1, denominator=0))
|
||||
print(quotient(5))
|
Reference in New Issue
Block a user