03 python学习_数据类型整体分析_int_bool转换_字符串索引与切片

day03

1.数据类型整体分析

int:  1,2,3用于计算

bool:  True,False,用户判断

str:  存储少量数据,进行操作  'ffda','二飞','123','张三','李四','王八麻子'  

list:  储存大量的数据  [1,2,3,'张三','12368a'],[1,[2,3],4]

元祖:  只读。  (1,2,3,'第三方')

dict:  大量的关系存在字典里  {'name':'云姐','age':'16'}

                {'云姐':[],'二哥':[200,200,200]}

集合:少用  {1,2,34,'asdf'}

1.1 int的方法

i = 5
print(i.bit_length()) #bit_length() 取二进制的最小位数

1.2 bool

#bool方法 只要是0---->Flase  非0就是True
i = 1
b = bool(i)
print(b)
#bool---->int
#True  1
#False 0
while True:
    pass
while 1:
    pass    #用1代替True,效率高

#str---->bool
#非空字符串都是True

 1.3 str

字符串的索引

s = 'ABCDEFGHIJK'
#索引
s1 = s[0]
print(s1)

字符串的切片

#字符串切片的步长
s = 'ABCDEFGHIJK'   # s[首:尾:步长]
s5 = s[0:5:2]
print(s5)

s = 'ABCDEFGHIJK'
s6 = s[4:0:-1]  #反向加步长,倒着取
print(s6)

s = 'ABCDEFGHIJK'
s7 = s[::-1]    #把所有元素倒着取出来
print(s7)

 

猜你喜欢

转载自www.cnblogs.com/liruitai/p/9580750.html