Home » Python » Python programs

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?
Submitted by IncludeHelp, on April 26, 2019

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 the 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 





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.