Meilleure réponse
Cest la beauté de Python. De nombreuses opérations courantes sont déjà mises en œuvre pour vous. Cest un tel cas.
 Vous pouvez utiliser la fonction intégrée bin() . Voici la description de bin() Python fournit: 
 Convertit un nombre entier en une chaîne binaire. Le résultat est une expression Python valide. Si  x  nest pas un objet Python int, il doit définir un \_\_index\_\_() qui renvoie un entier. 
Pour lutiliser, il vous suffit de passer un entier entier non négatif. Voici quelques exemples dutilisation.
 >>> bin(0) 
 "0b0"  
 >>> bin(12) 
 "0b1100"  
 >>> bin(25) 
 "0b11001"  
 >>> bin(250) 
 "0b11111010"  
 >>> bin(2440) 
 "0b100110001000" 
Mais peut-être aimeriez-vous implémenter votre propre fonction qui convertit du décimal au binaire, à des fins dapprentissage. Dans ce cas, voici une description de lalgorithme que jai utilisé, et voici mon implémentation:
 def decimal\_to\_binary(decimal): 
  """ Given a whole, decimal integer,  
  convert it to a binary  
  representation 
  """ 
  # I"m only making this function support  
  # non-negative, whole integer only. 
  if not isinstance(decimal, int) or decimal < 0: 
  raise TypeError("Input must be a non-negitive, whole integer.") 
  # we need a stack to store each binary digit in. 
  stack = [] 
  # while their are still digits left 
  # to convert in decimal. 
  while decimal > 0: 
  # caclute each binary number by dividing decimal  
  # by two. And since we are "building" our binary 
  # string backwards, insert() in the front of the  
  # list instead of append()-ing to the back. 
  stack.insert(0, str(decimal \% 2)) 
  # after we"ve calcute the binary value of the current  
  # decimal, divide the decimal by 2. But make sure we 
  # use // instead of / to get a while number! 
  decimal = decimal // 2 
  # join() together each value in stack, and return 
  # the finished binary string. Note: I simply 
  # added the "0b" prefix because that is how Python 
  # prepends its binary strings. If you don"t perfer that, 
  # then simply remove the "0b" + part from bellow. 
  return "0b" + "".join(stack) 
Tests et utilisation:
 >>> bin(0) == decimal\_to\_binary(0) 
 True 
 >>> bin(12) == decimal\_to\_binary(12) 
 True 
 >>> bin(25) == decimal\_to\_binary(25) 
 True 
 >>> bin(250) == decimal\_to\_binary(250) 
 True 
 >>> bin(2440) == decimal\_to\_binary(2440) 
 True 
Réponse
La solution fournie par Dripto Biswas est certainement la plus rapide. Cependant, il existe des solutions alternatives, au cas où vous seriez simplement curieux de savoir 🙂
Méthode # 2
 >>> format(5, "b") 
 "101" 
Méthode # 3
 >>> "{0:b}".format(5) 
 "101" 
Maintenant, si vous souhaitez le reconvertir,
 >>> int("101", 2) 
 5