本文介绍了如何在Python中创建整数变量并以十六进制格式分配值。通过使用0x或0X后缀,可以告诉编译器将值解释为十六进制,并将其赋给变量。程序示例展示了声明变量、打印变量类型以及它们的十进制和十六进制表示的过程。
摘要生成于
,由 DeepSeek-R1 满血版支持,
python中整数变量+
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.
要将十六进制格式的值分配给变量,我们使用0x或0X后缀。 它告诉编译器该值(后缀为0x或0X )是一个十六进制值,并将其分配给该变量。
Syntax to assign an hexadecimal value to the variable
为变量分配十六进制值的语法
x = 0x123AF
y = 0X1FADCB
通过分配十六进制值来创建变量的Python代码 (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.
注意:要以十六进制格式显示值,我们使用hex()函数 。
# 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
翻译自: https://www.includehelp.com/python/create-integer-variable-by-assigning-hexadecimal-value.aspx
python中整数变量+
八进制是由 0 ~ 7组成的
十进制是由 0 ~ 9组成的
十六进制是由 0 ~ 15 组成的,可是 9 后面的的 10 是用字母来代替 A~ F ,也就是 0 ~ F,用字母代替了数字,避免不再重复
之前刚接触 python 的时候学了以下 二进制,现在从八进制开始继续往下走
python 提供了一个方法,可以将任意数字专成 八进制
oct() : 将...
十进制整数转十六进制整数用hex();十六进制整数转十进制整数用int()
类似地,十进制整数转二进制整数用bin();十进制整数转八进制整数用oct()
hex() 函数
描述:hex() 函数用于将10进制整数转换成16进制,以字符串形式表示。
hex(x)
参数说明:x – 10进制整数
返回值:返回16...