Nombre en hexadécimal

PythonPythonBeginner
Pratiquer maintenant

This tutorial is from open-source community. Access the source code

💡 Ce tutoriel est traduit par l'IA à partir de la version anglaise. Pour voir la version originale, vous pouvez cliquer ici

Introduction

En Python, nous pouvons facilement convertir un nombre décimal en son équivalent hexadécimal à l'aide de la fonction hex(). Dans ce défi, vous devrez écrire une fonction qui prend un nombre décimal en argument et renvoie sa représentation hexadécimale.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python/BasicConceptsGroup -.-> python/comments("Comments") python/FunctionsGroup -.-> python/function_definition("Function Definition") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") subgraph Lab Skills python/comments -.-> lab-13732{{"Nombre en hexadécimal"}} python/function_definition -.-> lab-13732{{"Nombre en hexadécimal"}} python/build_in_functions -.-> lab-13732{{"Nombre en hexadécimal"}} end

Nombre en hexadécimal

Écrivez une fonction to_hex(dec) qui prend un nombre décimal en argument et renvoie sa représentation hexadécimale. Votre fonction devrait effectuer les étapes suivantes :

  1. Utilisez hex() pour convertir le nombre décimal en son équivalent hexadécimal.
  2. Retournez la représentation hexadécimale.
def to_hex(dec):
  return hex(dec)
to_hex(41) ## 0x29
to_hex(332) ## 0x14c

Sommaire

Dans ce défi, vous avez appris à convertir un nombre décimal en son équivalent hexadécimal à l'aide de la fonction hex() en Python. Vous avez également écrit une fonction qui effectue cette conversion.