day 03 int bool str (索引,切片) for 循环

基础数类型总览

  • 10203 123 3340 int +- * / 等等

  • '今天吃了没?' str 存储少量的数据,+ *int 切片, 其他操作方法

  • True False bool 判断真假

  • [12, True, 'alex', [1,2,3 ]] list 存储大量的数据。

  • (12, True, 'alex', [1,2,3 ]) tuple 存储大量的数据,不可改变里面的元素。

  • {'name': '太白金星'} dict 存储大量的关联型的数据,查询速度非常快。

  • set 交集,并集差集。。。

int

十进制二进制转换

  • '''
    二进制转换成十进制
    0001 1010     ------> ? 26
    '''
    b = 1 * 2**4 + 1 * 2**3 + 0 * 2**2 + 1 * 2**1 + 0 * 2**0
    # print(b) # 26

    '''
    42 -----> 0010 1010
    '''

    bit_lenth 十进制转化成二进制的有效长度

    # bit_lenth 有效的二进制的长度
    i = 4
    print(i.bit_length())  # 3
    i = 5
    print(i.bit_length())  # 3
    i = 42
    print(i.bit_length())  # 4

     

bool

  • bool str int 三者之间的转换

    # bool str int
    # bool <---> int
    '''
    True   1   False     0
    非零即True   0 是 False
    '''

    # str   <--->   int ***
    '''
    s1 = 10     int(s1) : 必须是数字组成
    i = 100     str(i)  
    '''
    # str bool ***
    # 非空即True
    s1 = ' '
    print(bool(s1))

    s1 = ''  # 空字符串
    print(bool(s1))
    # bool ---> str 无意义
    print(str(True))
  • 应用:

    s = input('输入内容')
    if s:
       print('有内容')
    else:
       print('没有输入任何内容')

 

str

  • 索引切片步长

    s1 = 'python全栈22期'
    # 对字符串进行索引,切片出来的数据都是字符串类型。
    # 按照索引取值
    # 从左至右有顺序,下标,索引。
    s2 = s1[0]
    print(s2,type(s2))
    s3 = s1[2]
    print(s3)
    s4 = s1[-1]
    print(s4)

    # 按照切片取值。
    # 顾头不顾腚
    s5 = s1[0:6]
    s5 = s1[:6]
    print(s5)
    s6 = s1[6:]
    print(s6)

    # 切片步长
    s7 = s1[:5:2]
    print(s7)
    print(s1[:])
    # 倒序:
    s8 = s1[-1:-6:-1]
    print(s8)

    # 按索引:s1[index]
    # 按照切片: s1[start_index: end_index+1]
    # 按照切片步长: s1[start_index: end_index+1:2]
    # 反向按照切片步长: s1[start_index: end_index后延一位:2]
    # 思考题:倒序全部取出来?
  • 练习题

    2.有字符串s = "123a4b5c"

    通过对s切片形成新的字符串s1,s1 = "123"
    通过对s切片形成新的字符串s2,s2 = "a4b"
    通过对s切片形成新的字符串s3,s3 = "1345"
    通过对s切片形成字符串s4,s4 = "2ab"
    通过对s切片形成字符串s5,s5 = "c"
    通过对s切片形成字符串s6,s6 = "ba2"

常用操作方法

strip: s觉普 去除空白\t \n***和指定的字符

split s普利特 默认按照空格分割返回列表也可以指定***可以限制分割次数

.upper 大写 啊普 .lower 小写咯额s

tartswith s大的位置 以什么位置开头 end with

replace(‘个数)

jion 囧e 可以操作可迭代对象连接起来变为字符串列表必须全是字符串类型str

count 看的 计数

l1='sdjkl11hj132121'
print(l1.upper()) #啊普
print(l1.lower()) #咯额
print(l1.count('s')) #看的
print('sss'.join(l1)) #连接
print(l1.startswith('s')) #s大的位置
print(l1.startswith('s'))
print(l1.split('1'))#分割s普利特
print(l1.strip()) #去空格 s觉普

猜你喜欢

转载自www.cnblogs.com/saoqiang/p/10908329.html