In the previous article, we have discussed Python float() Method with Examples
hex() Method in Python:
The hex() function converts the given number to a hexadecimal value.
The prefix 0x is always present in the returned string.
Syntax:
hex(number)
Parameters
The hex() function only accepts one argument.
x – integer number (either an int object or a __index__() method that returns an integer)
Return Value:
The hex() function converts an integer to its corresponding hexadecimal number and returns it as a string.
The hexadecimal string returned begins with the prefix 0x, indicating that it is in hexadecimal form.
Examples:
Example1:
Input:
Given Number = 20
Output:
The given number's{ 20 } Hexadecimal Value = 0x14 The hex() function Return Type = <class 'str'>
Note:
The prefix 0x denotes that the outcome is a Hexadecimal string.
Example2:
Input:
Given Number = 4.5
Output:
The given float number's{ 4.5 } Hexadecimal Value = 0x1.2000000000000p+2
hex() Method with Examples in Python
Method #1: Using Built-in Functions (Static Input)
1)
Approach:
- Give the number as static input and store it in a variable.
- Pass the given number as an argument to the hex() function to get the hexadecimal value of a given number.
- Store it in another variable.
- Print the given number’s Hexadecimal Value.
- Print the Return Type of hex() Function using the type() method by passing the hex(given number) as an argument to it.
- The Exit of the program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_numbr = 20 # Pass the given number as an argument to the hex() function to get the # hexadecimal value of a given number. # Store it in another variable. hexadeciml_valu = hex(gvn_numbr) # Print the given number's Hexadecimal Value. print("The given number's{", gvn_numbr, "} Hexadecimal Value = ", hexadeciml_valu) # Print the return Type of hex() Function using the type() method by passing # the hex(given number) as an argument to it. print("The hex() function Return Type =", type(hex(gvn_numbr)))
Output:
The given number's{ 20 } Hexadecimal Value = 0x14 The hex() function Return Type = <class 'str'>
2)Hexadecimal Value of a floating-point Number
Approach:
- Give the number as static input and store it in a variable.
- Pass the given number as an argument to the hex() function and apply float to the result to get the hexadecimal value of a given float number.
- Store it in another variable.
- Print the given float number’s Hexadecimal Value.
- The Exit of the program.
Below is the implementation:
# Give the number as static input and store it in a variable. gvn_numbr = 4.5 # Pass the given number as an argument to the hex() function and apply float # to the result to get the hexadecimal value of a given float number. # Store it in another variable. hexadeciml_valu = float.hex(gvn_numbr) # Print the given float number's Hexadecimal Value. print("The given float number's{", gvn_numbr, "} Hexadecimal Value = ", hexadeciml_valu)
Output:
The given float number's{ 4.5 } Hexadecimal Value = 0x1.2000000000000p+2
Method #2: Using Built-in Functions (User Input)
Approach:
- Give the number as user input using the int(input()) function and store it in a variable.
- Pass the given number as an argument to the hex() function to get the hexadecimal value of a given number.
- Store it in another variable.
- Print the given number’s Hexadecimal Value.
- Print the Return Type of hex() Function using the type() method by passing the hex(given number) as an argument to it.
- The Exit of the program.
Below is the implementation:
# Give the number as user input using the int(input()) function and store it in a variable. gvn_numbr = int(input("Enter some random number = ")) # Pass the given number as an argument to the hex() function to get the # hexadecimal value of a given number. # Store it in another variable. hexadeciml_valu = hex(gvn_numbr) # Print the given number's Hexadecimal Value. print("The given number's{", gvn_numbr, "} Hexadecimal Value = ", hexadeciml_valu) # Print the return Type of hex() Function using the type() method by passing # the hex(given number) as an argument to it. print("The hex() function Return Type =", type(hex(gvn_numbr)))
Output:
Enter some random number = 50 The given number's{ 50 } Hexadecimal Value = 0x32 The hex() function Return Type = <class 'str'>
2)Hexadecimal Value of a floating-point Number
Approach:
- Give the number as user input using the float(input()) function and store it in a variable.
- Pass the given number as an argument to the hex() function and apply float to the result to get the hexadecimal value of a given float number.
- Store it in another variable.
- Print the given float number’s Hexadecimal Value.
- The Exit of the program.
Below is the implementation:
# Give the number as user input using the float(input()) function and store it in a variable. gvn_numbr = float(input("Enter some random number = ")) # Pass the given number as an argument to the hex() function and apply float # to the result to get the hexadecimal value of a given float number. # Store it in another variable. hexadeciml_valu = float.hex(gvn_numbr) # Print the given float number's Hexadecimal Value. print("The given float number's{", gvn_numbr, "} Hexadecimal Value = ", hexadeciml_valu)
Output:
Enter some random number = 7.5 The given float number's{ 7.5 } Hexadecimal Value = 0x1.e000000000000p+2
Fed up with searching various pages for the list of Python Built in Functions? Look at the tutorial linked here and explore all coding samples of built-in functions of python.