python的一些内置函数

本文介绍了Python的内置函数,如format用于十进制到不同进制的转换,ord和chr用于Unicode码位的查看与字符转换。还探讨了enumerate、all和any在迭代中的应用,以及hash在数据存储中的作用。同时展示了dir用于查看对象可执行的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


# # 内置函数format的用法
# a=100
# print(format(a,'b'))   # 将十进制a转为二进制,输出:1100100
# print(format(a,'08b')) # 将十进制a转为8进制,不够8位用0补齐,够8位不管,输出:01100100;这个之后和子网掩码有用
# print(format(a,'o'))  # 将a转为8进制,输出:144
# print(format(a,'x'))  # 将a转为十六进制,输出:64


# ord :查看一个字在unicode中的码位是多少
b='国'
print(ord(b)) # 输出:22269

# chr:unicode中码位对应的文字符号是多少
print(chr(22269))  # unicode码位22269对应文字是:国

# 输出所有的unicode对应文字
# for i in range(65525):
#     print(chr(i)+" " ,end="")


# enumerate,all ,any
# all 首先all里面参数是可迭代的,all可以理解为“and”,当所有都是有值才返回True
print(all([1,2,3]))  # 返回:True
print(all(["",0,None]))  # 返回:False,这个3个值都代表空,没有

# any 内容也是可迭代的,any可以理解为or,只要有一个为True,则返回True,所有都是False才返回False
print(any(["",0,1]))  # 返回:True
print(any(["",0,None])) # 返回:False

# enumerate:输出可迭代对象及其下标
l = ['张三','李四','王五']
for i in enumerate(l):
    print(i) # 输出的是元组:(0, '张三') (1, '李四') (2, '王五')

# 下面这样写更好用,跟字典很像
for index,item in enumerate(l):
    print(index,item) # 输出:0 张三  1 李四  2 王五

# 上面这个我们之前是这样实现的
for i in range(len(l)):
    print(i,l(i))  # 输出:0 张三  1 李四  2 王五
# hash,
s='哈哈哈'
print(hash(s)) # 返回一定是个数字 → 转化为内存地址,然后进行数据存储 → 字典,元组,字符串都是可哈希的
# 这里输出:-1159055969,每次打印数字都不一样

# dir,当前数据能执行哪些操作
s='哈哈哈'
print(dir(s))

# 打印:['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值