python表达式的princp呢

2024-04-25 14:36:20 发布

您现在位置:Python中文网/ 问答频道 /正文

lambda x : '%x' % x

函数是十进制到十六进制的,原理是什么?我是Python新手,提前谢谢


Tags: lambda函数原理新手
2条回答

在字符串格式表示法中,“%x”是十六进制输出的placeholder。你知道吗

函数接受一个值并将其格式化为十六进制字符串。你知道吗

它不是“十进制到十六进制”,而是“以十六进制表示法作为字符串返回(无论给定什么)”。你知道吗

例如

print '%x' % 0b11111111   # -> 'ff'  (from binary)
print '%x' % 0377         # -> 'ff'  (from octal)
print '%x' % 255          # -> 'ff'  (from decimal)
print '%x' % 0xff         # -> 'ff'  (from hex)
a = 255

#use a hexadecimal format string to display the value of a - prints ff
print "%x" % a 

#create a function that takes a value and returns its hexadecimal representation
tohex = lambda x : '%x' % x

#call the function - prints ff
print tohex(255)

相关问题 更多 >