python笔记20181130

本文深入探讨了Python中各种内建函数的使用方法和应用场景,包括max(), min(), pow(), divmod(), callable(), type(), isinstance(), cmp(), range(), int(), complex(), eval(), chr(), ord()等,以及字符串操作如capitalize(),为读者提供了全面的Python内建函数指南。

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

内建函数
max()
min()
pow()
divmod()
callable()
In [23]: def a():
…: pass
…: callable(a)
…:
Out[23]: True

type()
isnstance()
In [28]: isinstance(l,list)
Out[28]: True

In [29]: isinstance(l,str)
Out[29]: False

In [30]: isinstance(s,str)
Out[30]: True

In [31]: isinstance(s,int)
Out[31]: False

In [32]: isinstance(s,(int,str))
Out[32]: True

In [33]: isinstance(s,(int,dict))
Out[33]: False

In [35]: class A(object):
…: pass
…: a
…: a=A()
…:

In [36]: isinstance(a,A)
Out[36]: True

cmp()

In [38]: cmp(1,1)
Out[38]: 0

In [39]: cmp(0,1)
Out[39]: -1

In [40]: cmp(2,1)
Out[40]: 1

In [41]: cmp(‘hello’,‘hello’)
Out[41]: 0

In [42]: cmp(‘helloa’,‘hello’)
Out[42]: 1

In [43]: cmp(‘zlloa’,‘hello’)
Out[43]: 1

In [44]: cmp(‘z’,‘hello’)
Out[44]: 1

In [45]: cmp(‘helloa’,‘hello’)
Out[45]: 1

range()
In [46]: range(10)
Out[46]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

In [47]: xrange(10)
Out[47]: xrange(10)

In [48]: a=range(10)

In [49]: a
Out[49]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
xrange()
int ()
In [50]: int(12.2)
Out[50]: 12

complex()
In [52]: complex(‘12’)
Out[52]: (12+0j)

eval
In [5]: type(eval(hex(10)))
Out[5]: int
In [6]: eval("[‘a’,‘b’,1]")
Out[6]: [‘a’, ‘b’, 1]
In [7]: chr(1)
Out[7]: ‘\x01’

In [8]: chr(100)
Out[8]: ‘d’

In [9]: chr(97)
Out[9]: ‘a’

In [10]: chr(65)
Out[10]: ‘A’

In [11]: help(ord)

In [14]: ord(‘A’)
Out[14]: 65

In [15]: s=‘hello’

In [16]: s.capitalize()
Out[16]: 'Hello

In [57]: string.lowercase
Out[57]: ‘abcdefghijklmnopqrstuvwxyz’

In [58]: string.uppercase
Out[58]: ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ’

In [59]: string.capitalize
Out[59]:

In [60]: string.capitalize(‘hello’)
Out[60]: ‘Hello’

In [80]: def f(x,y):
…: return x+y
…: reduce (f,range(1,101))
…:
Out[80]: 5050

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值