python数据类型之内置方法
python有六大数据类型,分别为整型、字符串、列表、字典、元祖和集合,这些基本数据类型都内置了很多方法,接下来一一探寻。 python中整型有两种:int和float 1 int使用dir函数查看有多少内置方法 <span style="color: #008000"># <span style="color: #008000"> python 2.x<span style="color: #000000">dir(int) <span style="color: #008000">#<span style="color: #008000"> ['abs','cmp','coerce','div','float','hash','hex','int','long','neg','nonzero','oct','pos','rdiv','repr','rlshift','rsub','setattr','xor','denominator','real'] 因为是整型类型,所以有加减乘除、取绝对值的方法,像__abs__是取绝对值,__add__是加法等,其中重要的几个方法是:bit_length、conjugate、from_bytes、imag、numerator、real、to_bytes digit = 123
num = 2.3 - 2.5j= num.real
(result)
result = num.imag
(result)
= num.conjugate()
(result)
(int.from_bytes(bytes=b,byteorder=<span style="color: #008000">#<span style="color: #008000">打印输出 65 ,即将字符A转换为十进制
num = 2= num.to_bytes(5,byteorder=
i <span style="color: #008000">#<span style="color: #008000">打印输出2n0n0n0n0<span style="color: #008000">
<span style="color: #008000">n表示回车
imag、real分别是计算出复制的实部和虚部,conjugate得出共轭复数。 2 float
a = 3.0
= 3.1
a = 15.0
3 str<span style="color: #008000"># <span style="color: #008000">python2.7<span style="color: #000000">dir(str) <span style="color: #008000">#<span style="color: #008000">['add','getitem','getnewargs','getslice','reduce','_formatter_field_name_split','_formatter_parser','decode','find','index','join','partition','split','swapcase','zfill']
str1 =
(str1.count(
a = =
![]()
str1 =
(str1.endswith(
str1 =
(str1.find(
str1 =
(str1.index(
str1 =
=
S =
=
S =
=
S =
(
S =
S =
S =
(S.replace(,
S =
(S.split(
S =
(S.startswith(
=
(S.startswith(
S =
S =
?4 list<span style="color: #008000"># <span style="color: #008000"> python 2.x<span style="color: #000000">dir(list) <span style="color: #008000">#<span style="color: #008000"> ['add','delslice','lt','setslice','reverse','sort']
lis = [,,,1,2,34
lis = [,3
lis = [,3=
lis = [,3]
lis1 = [,3= [4,5,6,7,8
lis1 = [,,3= lis1.index(
lis1 = [,34,
lis1 = [,3
= [,34
lis1 = [,3= lis1.remove(
lis1 = [,3
lis1 = [,,,
5 dict<span style="color: #008000"># <span style="color: #008000">python2.x<span style="color: #000000">dir(dict) <span style="color: #008000">#<span style="color: #008000">['class','has_key','iteritems','iterkeys','itervalues','values','viewitems','viewkeys','viewvalues']
dic = {: ,: ,:
dic = {: ,: =
dic = {: ,: = dic.fromkeys((),(
dic = {: ,: = dic.get(= dic.get(
dic = {: ,: key,value
dic = {: ,: =
dic = {: ,: ,: = dic.pop(
dic = {: ,: =
dic = {: ,: ,1
dic = {: ,: = {: 23
dic = {: ,: =
6 tuple因为元祖为不可变数据类型,和列表相比方法只有两个 定义元祖需要注意的是,假如只有一个元素,则需要加一个逗号,不然数据类型就不是元祖了
t = (1,(t.index(
t = (1,8,(t.count(1
7 set有序or无序判断有序和无序就看是否可以通过索引取值,可以通过索引取值的数据类型都是有序的 可变or不可变可变:值变化的同时,id没变。证明是在原值的基础上改变。 不可变:值变化的同时,id也改变。证明是生成一个新值,并没有改变原值。 字符串常用操作
msg =
(msg[5(msg[len(msg)-1(msg[-1=
<span style="color: #008000"># <span style="color: #008000"> 2、切片(顾头不顾尾,步长): 想要从一个大字符串中切出一个小字符串msg=<span style="color: #800000">'<span style="color: #800000">hello world<span style="color: #800000">' <span style="color: #0000ff">print(msg[0:5<span style="color: #000000">]) <span style="color: #0000ff">print<span style="color: #000000">(msg) <span style="color: #0000ff">print(msg[0:5:2]) <span style="color: #008000">#<span style="color: #008000">0 2 4 <span style="color: #008000"># <span style="color: #008000"> 了解msg = <span style="color: #800000">'<span style="color: #800000">hello world<span style="color: #800000">' <span style="color: #0000ff">print(msg[-1:-5:1<span style="color: #000000">]) <span style="color: #0000ff">print(msg[-1:-5:-1]) <span style="color: #008000">#<span style="color: #008000">d l r o <span style="color: #0000ff">print(msg[0:5:1<span style="color: #000000">]) <span style="color: #0000ff">print(msg[-1::-1]) <span style="color: #008000">#<span style="color: #008000">掌握 列表常用操作<span style="color: #008000"># <span style="color: #008000"> # li[4]='e'<span style="color: #008000"> <span style="color: #008000"> del li[0]<span style="color: #008000"> <span style="color: #008000"> print(li)<span style="color: #008000"> <span style="color: #008000">2、切片(顾头不顾尾,步长)<span style="color: #008000"> <span style="color: #008000"> li=['a','d']<span style="color: #008000"><span style="color: #008000"> print(li[0:3])(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |