zoukankan      html  css  js  c++  java
  • python基础3---基础数据类型&str操作

    1. 基础数类型总览(7种)

      • 10203 123 3340 int +- * / 等等
      • '今天吃了没?' str 存储少量的数据,+ *int 切片, 其他操作方法
      • True False bool 判断真假
      • [12, True, 'a1', [1,2,3 ]] list 存储大量的数据。
      • (12, True, 'a1', [1,2,3 ]) tuple 存储大量的数据,不可改变里面的元素。
      • {'name': '张三'} dict 存储大量的关联型的数据,查询速度非常快。
      • set 交集,并集差集。。。
    2. 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
        
    3. 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)) # True 空格也是字符
        
        s1 = ''  # 空字符串
        print(bool(s1)) # False
        # bool  ---> str  无意义
        print(str(True))
        
      • 应用:

        s = input('输入内容')
        if s:
            print('有内容')
        else:
            print('没有输入任何内容')
        
    4. 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"
        
      • 常用操作方法

        # upper lower
        # s1 = s.upper()
        # # s1 = s.lower()
        # print(s1,type(s1))
        
        # 应用:
        username = input('用户名')
        password = input('密码')
        code = 'QweA'
        print(code)
        your_code = input('请输入验证码:不区分大小写')
        if your_code.upper() == code.upper():
            if username == '太白' and password == '123':
                print('登录成功')
            else:
                print('用户名密码错误')
        else:
            print('验证码错误')
        
  • 相关阅读:
    人工神经网络(Artificial Neural Networks)
    潜语义分析(Latent Semantic Analysis)
    IOS Dictionary和Model相互转换
    jquery ajax跨域请求webservice
    日期格式转换
    1
    iptables详解
    yum报错-Network is unreachable"Error:
    41个Web开发者JavaScript实用小技巧
    比较常用的几个maven第三方镜像
  • 原文地址:https://www.cnblogs.com/he-qing-qing/p/14319089.html
Copyright © 2011-2022 走看看