Q:

Create integer variable by assigning hexadecimal value in Python

belongs to collection: Python basic programs

0

Hexadecimal value assignment

To assign value in hexadecimal format to a variable, we use 0x or 0X suffix. It tells to the compiler that the value (suffixed with 0x or 0X) is a hexadecimal value and assigns it to the variable.

Syntax to assign an hexadecimal value to the variable

    x = 0x123AF
    y = 0X1FADCB

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Python code to create variable by assigning hexadecimal value

In this program, we are declaring some of the variables by assigning the values in hexadecimal format, printing their types, values in decimal format and hexadecimal format.

# Python code to create variable 
# by assigning hexadecimal value 

# creating number variable
# and, assigning hexadecimal value
a = 0x123
b = 0X123
c = 0xAFAF
d = 0Xafaf
e = 0x7890abcdef

# printing types
print("type of the variables...")
print("type of a: ", type(a))
print("type of b: ", type(b))
print("type of c: ", type(c))
print("type of d: ", type(d))
print("type of e: ", type(e))

# printing values in decimal format
print("value of the variables in decimal format...")
print("value of a: ", a)
print("value of b: ", b)
print("value of c: ", c)
print("value of d: ", d)
print("value of e: ", e)

# printing values in hexadecimal format
print("value of the variables in hexadecimal format...")
print("value of a: ", hex(a))
print("value of b: ", hex(b))
print("value of c: ", hex(c))
print("value of d: ", hex(d))
print("value of e: ", hex(e))

Output

type of the variables...
type of a:  <class 'int'>  
type of b:  <class 'int'>  
type of c:  <class 'int'>  
type of d:  <class 'int'>  
type of e:  <class 'int'>  
value of the variables in decimal format...  
value of a:  291  
value of b:  291  
value of c:  44975
value of d:  44975
value of e:  517823253999  
value of the variables in hexadecimal format... 
value of a:  0x123
value of b:  0x123
value of c:  0xafaf  
value of d:  0xafaf  
value of e:  0x7890abcdef 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python basic programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Typecasting Input to Integer, Float... >>
<< Create integer variable by assigning octal value i...