Guide des Fonctions Intégrées Python avec Exemples
Ce guide présente les fonctions intégrées les plus utiles de Python, accompagnées d'exemples pratiques
pour illustrer leur utilisation.
Fonctions de base
print(obj)
Ex: print('Bonjour') # Affiche: Bonjour
type(obj)
Ex: print(type(42)) # Affiche: <class 'int'>
len(obj)
Ex: print(len('Python')) # Affiche: 6
id(obj)
Ex: a = 10
print(id(a)) # Affiche l'identifiant mémoire de a
help(obj)
Ex: help(str) # Affiche l'aide sur le type str
Conversions de types
int(x)
Ex: print(int('5')) # Affiche: 5
float(x)
Ex: print(float('3.14')) # Affiche: 3.14
str(x)
Ex: print(str(123)) # Affiche: '123'
bool(x)
Ex: print(bool(0)) # Affiche: False
list(x)
Ex: print(list('abc')) # Affiche: ['a', 'b', 'c']
tuple(x)
Ex: print(tuple([1, 2])) # Affiche: (1, 2)
dict()
Ex: print(dict(a=1, b=2)) # Affiche: {'a': 1, 'b': 2}
set(x)
Ex: print(set([1, 2, 2])) # Affiche: {1, 2}
Itérateurs et générateurs
range()
Ex: print(list(range(1, 4))) # Affiche: [1, 2, 3]
enumerate()
Ex: for i, v in enumerate(['a', 'b']): print(i, v)
zip()
Ex: print(list(zip([1, 2], ['a', 'b']))) # Affiche: [(1, 'a'), (2, 'b')]
iter()
Ex: it = iter([1, 2])
print(next(it)) # Affiche: 1
next()
Ex: print(next(it)) # Affiche: 2
Mathématiques
abs(x)
Ex: print(abs(-7)) # Affiche: 7
round(x)
Ex: print(round(3.14159, 2)) # Affiche: 3.14
pow(x, y)
Ex: print(pow(2, 3)) # Affiche: 8
divmod(a, b)
Ex: print(divmod(7, 3)) # Affiche: (2, 1)
sum()
Ex: print(sum([1, 2, 3])) # Affiche: 6
min()
Ex: print(min(3, 1, 4)) # Affiche: 1
max()
Ex: print(max(3, 1, 4)) # Affiche: 4
Chaînes de caractères
chr(i)
Ex: print(chr(65)) # Affiche: A
ord(c)
Ex: print(ord('A')) # Affiche: 65
format()
Ex: print(format(3.14159, '.2f')) # Affiche: 3.14
Entrées / Sorties
input()
Ex: # input('Entrez votre nom: ')
open()
Ex: # with open('[Link]', 'r') as f:
# print([Link]())
Évaluation / Exécution
eval()
Ex: print(eval('3 + 4')) # Affiche: 7
exec()
Ex: exec("x = 5\nprint(x)") # Affiche: 5
compile()
Ex: code = compile('print(42)', '', 'exec')
exec(code)
Autres
isinstance()
Ex: print(isinstance(3, int)) # Affiche: True
issubclass()
Ex: print(issubclass(bool, int)) # Affiche: True
all()
Ex: print(all([True, True])) # Affiche: True
any()
Ex: print(any([False, True])) # Affiche: True
sorted()
Ex: print(sorted([3, 1, 2])) # Affiche: [1, 2, 3]
reversed()
Ex: print(list(reversed([1, 2, 3]))) # Affiche: [3, 2, 1]
globals()
Ex: print(list(globals().keys()))
locals()
Ex: print(list(locals().keys()))
vars()
Ex: class A: pass
a = A()
print(vars(a))
callable()
Ex: print(callable(print)) # Affiche: True