Sometimes we need to work with binary and/or hexadecimal numbers in Python. Maybe we are designing a program that is processing packet capture data, or we are working with IP addressing at a low level, whatever the case may be. In Python, we can easily assign binary and/or hexadecimal numbers to variables in a couple of different ways.
Precede a binary number with 0b. Precede a hexadecimal number with 0x.
Use the int function, where the first positional argument is a string representation of our binary or hexadecimal number, and the second argument is an integer representing the base value, 2 for binary, and 16 for hexadecimal.
Regardless of the fact that we assigned binary and hexadecimal numbers to the variables, decimal values are printed. If we need to print the binary or hexadecimal numbers, we have to use the bin and/or hex functions for casting.