Data Types built-in method 1

zero,

  • Variable Type: if the value changes, id unchanged

  • Immutable type: in the case of changing only, id change

  • Ordered: Whenever data is indexed ordered

A, int

  1. use

    Deposit qq number, phone number, identity card number with no letters

  2. Defined way

    age = 18

  3. Variable or immutable

    Immutable

  4. Hexadecimal conversion

    • Other binary decimal turn

      # 二进制转十进制
      10 —— 1 * (2 ** 1) + 0 * (2 ** 0)
      
      # 八进制转十进制
      
      235 —— 2 * (8 ** 2) + 3 * (8 ** 1) + 5 * (8 ** 0)
      
      # 十六进制转十进制
      
      217 —— 2 * (16 ** 2) + 1 * (16 ** 1) + 5 * (8 ** 0)
    • int can pass the second parameter, the first parameter represents what band, and then converted into a decimal

      - print(int('1100', 2))
      - print(int('14', 8))
      - print(int('c', 16))
    • Decimal turn the other band

      - 十进制转二进制
        print(bin(12))   # 0b1100
      - 十进制转八进制
        print(oct(12))   # 0o14
      - 十进制转十六进制
        print(hex(12))   # 0xc

    Second, the string

    1. description

    2. Defined way

      ‘ ’ “ ” ‘’‘ ’‘’

    3. Ordered or unordered

      Ordered

    4. Variable or immutable

      Immutable

    5. Operation and built-in method

      • By index values

        s = 'hello world'
        print(s[0])    # h
        print(s[-1])    # d
      • Sections (care regardless of tail)

        s = 'hello world'
        print(s[0:5])   # hello
        print(s[0:10:2])    # hlowr
        print(s[5:0:-2])    # le
        print(s[-1:-4:-1])    # dlr
      • len (count the number of characters)

        s1 = '  '
        print(len(s1))    # 2
      • in, not in (determined whether there is a character string in a large string)

        print('a' in 'abcd')    # True
      • strip (left and right sides of the character string removed)

        strip()    # 默认去除字符串首位的空格
        
        name1 = '    byx '
        print(name1.strip())    # byx
        
        name2 = '$by$x$'    
        print(name2.strip('$'))    # by$x    中间的符号不会去除
        print(name2.lstrip('$'))    # by$x$
        print(name2.rstrip('$'))    # $by$x
      • split (the string into a list)

        data = 'byx|18|male'
        print(data.split('|'))    # ['byx', '18', 'male']    切分成列表
        print(data.rsplit('|', 1))    # ['byx|18', 'male']    从右边切一个'|'
      • for loop

        for i in 'byx':
            print(i, end=' ')    # b y x 
      • lower, upper (uppercase, lowercase conversion)

        s = 'ByX'
        print(s.lower())    # byx
        print(s.upper())    # BYX
        print(s)    # ByX    s 本身没变
      • startswith, endswith (first determination)

        s = 'byx'
        print(s.startswith('b'))    # True
        print(s.endswith('x'))    # True
      • join (mosaic)

        user_list = ['byx', '18', 'male']
        res = '|'.join(user_list)
        print(res)    # byx|18|male
      • replace (replace)

        str = 'Rome was not built in one day'
        res = str.replace('day', 'month')
        print(res)    # Rome was not built in one month
        print(str)    # Rome was not built in one day    原字符串没变
      • isdigit (determined whether the input digital)

        age = input('请输入年龄')
        if age.isdigit():    
            age = int(age)
        else:
            print('请输入数字')
      • find, index (look for location)

        s = 'Rome was not built in one day'
        print(s.find('was'))    # 5    返回所在位置
        print(s.find('xxx'))    # -1    找不到时,返回 -1
        print(s.find('was', 0, 5))    # -1    通过索引限制查找范围,顾头不顾尾
        print(s.find('was', 0, 5))    # 5    只有找到整个‘was’时,才不返回-1,并返回5
        
        print(s.index('was', 0, 5))    # 报错    找不到值报错
        print(s.index('was', 0, 8))    # 5    只有找到整个‘was’时,才不报错,并返回5
      • count (count the number of occurrences)

        s = 'Rome was not built in one day'
        print(s.count('o'))    # 3
      • center, ljust, rjust, zfill (center filling, filling the left and right filling)

        s = 'byx'
        print(s.center(7, '*'))    # **byx**    居中填充,一共7位字符
        print(s.ljust(7, '*'))    # byx****    左移后填充,一共7位字符
        print(s.rjust(7, '*'))    # ****byx    右移后填充,一共7位字符
        print(s.zfill(7))    # 0000byx    左边用0填充,一共7位字符
      • expandtabs (changing the number of key spaces table)

        s = 'b\tyx'
        print(s)    # b yx
        print(s.expandtabs(10))    # b         yx    # table键变为10个空格
      • captalize, swapcase, title

        s = 'RoMe waS nOt BuILT in ONe daY'
        print(s.capitalize())    # Rome was not built in one day    字符串首字母大写
        print(s.swapcase())    # rOmE WAs NoT bUilt IN onE DAy    大小写转换
        print(s.title())    # Rome Was Not Built In One Day    所有单词首字母大写

    Third, the list

    1. Definitions: [] and storing a plurality of arbitrary elements, separated by commas

      l = list('abc')
      l1 = list({'name': 'byx', 'age': 18})    
      print(l)    # ['a', 'b', 'c']
      print(l1)    # ['name', 'age']
      # 内部是for循环
    2. Variable or immutable

      Variable Types

    3. Ordered or unordered

      Ordered

    4. Operation and built-in method

      • By index values ​​(Forward + Reverse Access Access)

        l = [1, 2, 3, 4]
        print(l[2])    # 3
        
        l[0] = 5    
        print(l)    # [5, 2, 3, 4]    可以通过索引修改元素
      • Sections (care regardless of tail)

        l = [1, 2, 3, 4]
        print(l[0:4:1])      # [1, 2, 3, 4]
        print(l[0::])    # [1, 2, 3, 4]    默认取到最后,默认步长为1
        print(l[5::-1])    # [4, 3, 2, 1] 
      • len (length)

        l = [1, 2, 3, 4]
        print(len(l))    # 4
      • in and not in (the members of calculation)

        l = [1, 2, 3, 4]
        print(5 in l)    # False
      • append, insert, extend (add elements)

        l = [1, 2, 3, 4]
        l.append(5)
        print(l)    # [1, 2, 3, 4, 5]    尾部添加一个元素
        
        l1 = [1, 2, 3, 4]
        l1.insert(2, 5)
        print(l1)    # [1, 2, 5, 3, 4]    通过索引在任意位置添加一个元素
        
        l1 = [1, 2, 3, 4]
        l2 = [5, 6]
        l1.extend(l2)
        print(l1)    # [1, 2, 3, 4, 5, 6]    列表拼接
        
        l3 = [1, 2, 3, 4]
        l3.extend([5,])
        print(l3)    # [1, 2, 3, 4, 5]    内部是for循环,参数要传可迭代对象
      • del, pop, remove (delete)

        l = [1, 2, 3, 4]
        del l[2]    # del 按照索引删除,适用于所有的删除操作 无返回值
        print(l)    # [1, 2, 4]
        del l    # 删除列表
        
        l = [1, 2, 3, 4, 5]
        l.pop()
        print(l)    # [1, 2, 3, 4]    尾部弹出一个参数
        res = l.pop()
        print(res)    # 4    弹出的元素就是返回值
        print(l)    # [1,2, 3]
        res1 = l.pop(1)
        print(res1)    # 2    可以按照索引指定弹出元素
        
        l = [1, 2, 3, 4]
        l.remove(3)
        print(l)    # [1, 2, 4] 指定删除元素,无返回值
      • count (Count)

        l = ['a', 'b', 1, 3, 3]
        print(l.count(3))    # 2
        print(l.count('c'))    # 0
      • clear (empty)

        l = [1, 2, 3, 4]
        res = l.clear()
        print(l)    # []
        print(res)    # None    列表被清空
      • sort, reverse (sort and reverse)

        l = [2, 1, 5, 4, 3]
        l.sort()
        print(l)    # [1, 2, 3, 4, 5]
        
        l = [2, 1, 5, 4, 3]
        l.sort(reverse=True)
        print(l)     #[5, 4, 3, 2, 1]
    5. Queue and Stack

      • Queue (FIFO)

        l1 = []
        # 先进
        l1.append('a')
        l1.append('b')
        l1.append('c')
        # 先出
        print(l1.pop(0))    # a
        print(l1.pop(0))    # b
        print(l1.pop(0))    # c
      • Stack (last out)

        l1 = []
        # 先进
        l1.append('a')
        l1.append('b')
        l1.append('c')
        # 先出
        print(l1.pop())    # c
        print(l1.pop())    # b
        print(l1.pop())    # a

Guess you like

Origin www.cnblogs.com/binyuanxiang/p/11514660.html