# Fonctions #### Fonctions natives: ##### abs(x) ```python # -retourne la valeur absolue >>> abs(-3) 3 ``` ##### all(iterable) ```python # -retourne True si tous les éléments d'un élément itérable sont True >>> liste = [True, True, True, 1] >>> all(liste) True ``` ##### any(iterable) ```python # -retourne True si au moins un élément d'un élément itérable est True >>> liste = [True, False, True, 1] >>> any(liste) True ``` ##### bin(x) ```python # -convertit un entier en chaine de caractères binaires >>> bin(132) '0b10000100' ``` ##### bool() ```python # -retourne la valeur boléenne >>> bool(12) True >>> bool(0) False ``` ##### callable ```python # -détermine si un objet est callable (exécutable ou appelable) # -fonctions (user ou builtin), methodes >>> callable("A") False >>> callable(int) True ``` ##### chr(n) ```python # -retourne la caractère qui correspond à l'unicode n >>> chr(97) 'a' ``` ##### delattr() ```python # -efface un attribut d'un objet class Person: name = "John" age = 36 country = "Norway" delattr(Person, 'age') ``` ##### eval(expression) ```python # - >>> v =101 >>> eval('v+1') 102 ``` ##### format(val, format) ```python # -formatte une valeur selon le format. spécifié >>> x = format(5, 'b') # binaire >>> x '101' >>> x = format(44, 'x') # hexa >>> x '2c' ``` ##### getattr(objet, attribut) ```python # -retourne la valeur de l'attribut de l'objet spécifié class Person: name = "John" age = 36 country = "Norway" x = getattr(Person, 'age') 36 ``` ##### hasattr(objet, attribut) ```python # -retourne True si l'objet spécifié à l'attribut class Person: name = "John" age = 36 country = "Norway" x = hasattr(Person, 'age') True ``` ##### help(element) ```python # -retourne l'aide sur l'élément >>> help(tuple) Help on class tuple in module builtins: class tuple(object) | tuple(iterable=(), /) | | Built-in immutable sequence. ``` ##### hex() ```python # -convertit un nombre en hexa` >>> hex(15) '0xf' ``` ##### locals() ```python # -retourne un dictionnaire avec des variables en cours ``` ##### map(function) ```python # -exécute une fonction sur chaque item d'un élément itérable ``` ##### max() / min() ```python # -retourne la valeur maxi ou mini >>> min([1,3,6,99,125,-3]) -3 >>> max([1,3,6,99,125,-3]) 125 ``` ##### next(iterable) ```python # -retourne l'item suivant d'un itérable >>> mylist = iter(["apple", "banana", "cherry"]) >>> x = next(mylist) >>> x 'apple' >>> x = next(mylist) >>> x 'banana' ``` ##### ord(caractère) ```python # -retourne le code unicode du caractère >>> x = ord("€") >>> x 8364 ``` ##### pow(x, y, z) ```python # -retourne x puissance y (modulo z) >>> pow(2,4) 16 ``` ##### print(object(s) separator=separator, end=end, file=file, flush=flush) ```python # -affiche le message à l'écran ou sur une autre sortie # -par défaut, end='\' >>> print("Hello", "how are you?", sep=" --- ") Hello --- how are you? fichier = open("test.py","r") print(fichier.read()) fichier.close() ``` ##### randint() ```python # -retourne un entier aléatoire >>> import random >>> random.randint(1,100) 76 ``` ##### random() ```python # -retourne une valeur aléatoire >>> import random >>> random.random() 0.893485376651602 ``` ##### range(x) ```python # -crée une séquence de nombre de 0 à x (exclu) x = range(4) # de 0 à 3 for n in x: print(n) 0 1 2 3 x = range(3, 6) # de 3 à 5 for n in x: print(n) 3 4 5 x = range(0,6,2) # de 0 à 5 et un pas de 2 for n in x: print(n) 0 2 4 ``` ##### reverse() ```python # -inverse l'ordre d'une liste >>> x = [1,2,3] >>> x.reverse() >>> x [3, 2, 1] ``` ##### reversed() ```python # -retourne un itérateur inversé >>> list(reversed([1,2,3,4])) [4, 3, 2, 1] ``` ##### round(number) ```python # -retourne l'arrondi d'un nombre >>> round(-1.2) -1 >>> round(-1.2563679) -1 ``` ##### set(iterable) ```python # -crée un objet set # -un set est une collection non ordonnée et non indexé x = set(('apple', 'banana', 'cherry')) >>> x {'apple', 'cherry', 'banana'} ``` ##### slice(start, end, step) ```python # -retourne un objet slice >>> a = ("a", "b", "c", "d", "e", "f", "g", "h") >>> x = slice(2) >>> print(a[x]) ('a', 'b') >>> x = slice(3, 5) >>> print(a[x]) ('d', 'e') >>> x = slice(0, 8, 3) >>> print(a[x]) ('a', 'd', 'g') ``` ##### shuffle() ```python # -mélange aléatoirement une liste >>> import random >>> x = [1,2,3,4,5] >>> random.shuffle(x) >>> x [2, 5, 4, 1, 3] ``` ##### list.sort() ```python # -permet de trier une liste >>> l = [67,34,89,12,70,345,2,-56] >>> l.sort() >>> l [-56, 2, 12, 34, 67, 70, 89, 345] ``` ##### sorted(iterable) ```python # -tri un élément itérable >>> sorted([3,2,12,1]) [1, 2, 3, 12] ``` ##### sum(iterable) ```python # -retourne la somme des valeurs d'un élément itérable >>> sum([12,24,36]) 72 ``` ##### upper() ```python # -met en majuscule la chaine de caractères >>> "Python".upper() 'PYTHON' ``` ##### zip(*iterables) ```python # -permet de regrouper sous la forme d'un tuple les items de listes. # - https://stackoverflow.com/questions/31683959/the-zip-function-in-python-3 >>> a = ["sharon", "sophie", "halle"] >>> b = ["stone", "marceau", "berry"] >>> zip(a,b) >>> list(zip(a,b)) [('sharon', 'stone'), ('sophie', 'marceau'), ('halle', 'berry')] ```