Python Code Example: convert decimal to hexadecimal number

Code Explanation

LineDescription
1Function header of the function decToHex() with the input parameter x
2Returns the hexadecimal representation of the number x. The hex() function normally returns a string in the following form: ‘0x….’. The function .split() removes the substring 0x from the string
4Calls the function decToHex() with the parameter 140 and prints the result
def decToHex(x):
    return hex(x).split('x')[-1]

print(decToHex(140))
Output
8c