Create integer variable by assigning hexadecimal value in Python

Here, we are going to learn how to create an integer variable by assigning value in hexadecimal format in Python? By IncludeHelp Last updated : April 08, 2023

Integer variable with hexadecimal value

The task is to create integer variables and assign values in hexadecimal format.

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 variable

x = 0x123AF
y = 0X1FADCB

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.

Note: To print value in hexadecimal format, we use hex() function.

# 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 

Python Basic Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.