 # Python hex() function

## hex() function in Python

hex() function is one of the built-in functions in Python3, which is used to convert an integer number into it’s corresponding hexadecimal form.

Syntax :

```hex(x)
Parameters :
x - an integer number (int object)

Errors and Exceptions :

```TypeError :  Returns TypeError when anything other than
integer type constants are passed as parameters.```

Code #1 : Illustrates use of hex() function.

 `# Python3 program to illustrate` `# hex() function` `print``(``"The hexadecimal form of 23 is "` `                            ``+` `hex``(``23``))` `                            ` `print``(``"The hexadecimal form of the "` `      ``"ascii value is 'a' is "` `+` `hex``(``ord``(``'a'``)))` `      ` `print``(``"The hexadecimal form of 3.9 is "` `                        ``+` `float``.``hex``(``3.9``))`

Output :

```The hexadecimal form of 23 is 0x17
The hexadecimal form of the ascii value os 'a' is 0x61
The hexadecimal form of 3.9 is 0x1.f333333333333p+1```

Code #2: Demonstrate TypeError when floating point values are passed as parameter.

 `# hex() accepts only integer vaues as parameters` `print``(``"The hexadecimal form of 11.1 is "` `                            ``+` `hex``(``11.1``))` `'''` `# The hexadecimal conversion of floating ` `# point integers can be done using the` `# function float.hex()` `print("The hexadecimal form of 11.1 is "` `                    ``+ float.hex(11.1))` `# Output :` `# The hexadecimal form of 11.1 is 0x1.6333333333333p+3` `# Similarly, float.hex() throws a TypeError` `# when integer values are passed in it.` `'''`

Output :

```Traceback (most recent call last):
File "/home/7e1ac7e34362fd690cdb72cf294502e1.py", line 2, in
print("The hexadecimal form of 11.1 is "+hex(11.1))
TypeError: 'float' object cannot be interpreted as an integer```

Applications :
hex() is used in all the standard conversions. For example conversion of hexadecimal to decimal, hexadecimal to octal, hexadecimal to binary.
Code #3 :

 `# TypeConversion from decimal with base 10` `# to hexadecimal form with base 16` `# Taking input from user` `# an integer with base 10` `number ``=` `int``(``input``(``"Enter a number with base 10\n"``))` `# The choices present to the user` `print``(``"a. Decimal to Hexadecimal "``)` `print``(``"b. Decimal to Octal"``)` `print``(``"c. Decimal to Binary"``)` `# taking user input` `print``(``"Enter your choice :- "``)` `choice ``=` `input``()` `# Running a variable choice` `# Hexadecimal form if choice` `# is set to 'a'` `if` `choice ``is` `'a'``:` `    ``# lstrip helps remove "0x" from the left` `    ``# rstrip helps remove "L" from the right,` `    ``# L represents a long number` `    ``print``(``"Hexadecimal form of "` `+` `str``(number) ``+` `        ``" is "` `+` `hex``(number).lstrip(``"0x"``).rstrip(``"L"``))` `        ` `if` `choice ``is` `'b'``:` `    ` `    ``# Octal representation is done` `    ``# by adding a prefix "0o"` `    ``print``(``"Octal form of "` `+` `str``(number) ``+` `        ``" is "` `+` `oct``(number).lstrip(``"0o"``).rstrip(``"L"``))` `        ` `if` `choice ``is` `'c'``:` `    ` `    ``# Binary representation is done by` `    ``# the addition of prefix "0b"` `    ``print``(``"Binary form of "` `+` `str``(number) ``+` `        ``" is "``+``bin``(number).lstrip(``"0b"``).rstrip(``"L"``))`

Output :

input variant a)

```Enter a number with base 10
123
b. Decimal to Octal
c. Decimal to Binary
a
Hexadecimal form of 123 is 7b```

input variant b)

```Enter a number with base 10
123456789
b. Decimal to Octal
c. Decimal to Binary
a
Hexadecimal form of 123456789 is 75bcd15```

Last Updated on October 26, 2021 by admin

## Accessing elements of a Pandas SeriesAccessing elements of a Pandas Series

Accessing elements of a Pandas Series Pandas Series is a one-dimensional labeled array capable of holding data of any type (integer, string, float, python objects, etc.). Labels need not be unique but must be a hashable type. An element in

## Pandas DataFrame.isinPandas DataFrame.isin

When working with Pandas DataFrames, there are often cases where we need to check if a value is present in a column or not. One way to achieve this is to use the isin() method of a DataFrame, which returns

## Python – Accessing Key-value in DictionaryPython – Accessing Key-value in Dictionary

Python | Accessing Key-value in Dictionary Dictionary is quite a useful data structure in programming that is usually used to hash a particular key with value, so that they can be retrieved efficiently. Let’s discuss various ways of accessing all the

## Python – os.remove() methodPython – os.remove() method

Python | os.remove() method OS module in Python provides functions for interacting with the operating system. OS comes under Python’s standard utility modules. This module provides a portable way of using operating system dependent functionality. All functions in os module raise OSError in

## Creating Pandas dataframe using list of listsCreating Pandas dataframe using list of lists

Pandas DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. It is generally the most commonly used pandas object. Pandas DataFrame can be created in multiple ways. Let’s discuss how to create Pandas dataframe using list

## How to Drop rows in DataFrame by conditions on column values?How to Drop rows in DataFrame by conditions on column values?

How to Drop rows in DataFrame by conditions on column values? In this article, we are going to see several examples of how to drop rows from the dataframe based on certain conditions applied on a column. Pandas provide data

## Hotel Management Project in PythonHotel Management Project in Python

Hotel Management Project in Python This Project on Hotel Management is a general software developed (using Python) to simplify hotel operations by automating them. In this project, “AnCasa” is the project’s hotel name. It covers major aspects of hotel management;

## OpenCV – Analyze an image using HistogramOpenCV – Analyze an image using Histogram

OpenCV Python Program to analyze an image using Histogram In this article, image analysis using Matplotlib and OpenCV is discussed. Let’s first understand how to experiment image data with various styles and how to represent with Histogram. Importing image data