Pyhton basic data types V (supplemental)

1. Content Today

  1. The underlying data type of supplement
  2. Convert between data types
  3. Common pit
  4. Advanced coding

2. Review yesterday

  1. id is ==
    • id: acquiring an object memory address; IS: whether the same memory address; ==: whether the data is the same
  2. Code block: a file, interactive command line is a block of code
  3. Caching code block
    • Advantages: improve performance, save space
    • A cache mechanism in the same block of code (string resides mechanism)
      • All figures, bool, almost all of the strings
    • Caching mechanism in different code blocks (small data pool)
      • -5 ~ 256 int, bool, string meet certain rules
  4. Collection: Role: List de-duplication, relationship test
  5. Shades copy:
    • Shallow copy: open up a new space objects stored copy of the (lists, dictionaries) in memory, but all the elements inside the object with the copy inside a shared object
    • Deep copy

3. The specific content

  1. Data type of supplement

    #str
    #首字母大写:
    s = 'xiaoshuo'
    s1 = s.capitalize()
    print(s1)
    
    #每个单词首字母大写
    s1 = s.title()
    print(s1)
    
    #大小写互换
    s1 = s.swapcase()
    print()
    
    #统计出现的次数
    s1 = s.count('o')
    print(s1)
    
    #查找
    print(s.find('o')) #find 查找不到返回-1
    print(s.index('o'))#index 查找不到就报错
    
    
    #list
    li = list('8765432') #迭代定义的方式
    print(li)
    
    #统计
    print(li.count('8'))
    
    #查看索引
    print(li.index('8'))
    
    #反转
    li.reverse()
    print(li)
    
    #排序
    li.sort()   #升序
    li.sort(reverse = True)  #降序
    print(li)
    
    
    #tuple
    tu = tuple('123456') #迭代定义元组
    
    #统计
    print(tu.count('1'))
    
    #查找索引
    print(tu.index('1'))
    
    
    #dict
    dic = dict(k1 = 1,k2 = 2, k3 = 3)
    print(dic)
    
    #随机删除:
    dic.popitem()
    python3.6版本之后,默认删除最后个键值对
    python3.5版本以前,随机删除
    
    #批量创建字典:
    dic = {}
    dic1 = dic.fromkeys('qwert',[1234])
    #第一个参数可迭代对象
    #第二个参数时每个键对应的值---用的都是痛一个内存地址
    
    
    #set
    s = set('12345')  #迭代定义方式
    print(s)
  2. Basic data type conversion

    #数据类型转换:
     #字符串转化数字的时候必须都是十进制数
      #字符串转列表:
         print(s.split())
      #列表转换字符串
         print(''.join(li))
      #除字典外,数据类型之间可以直接互相转换
    
    
    #基础数据类型分类:
     str;int;bool;tuple;list;dict;set
    #有序
     str;int;bool;tuple;list
    #无序
     dict;set
    #可变
     list;dict;set
    #不可变
     str;int;boo;tuple
    #访问方式
     #直接访问
         int;bool;set
      #顺序访问
         list;tuple;set
      #键访问
         dict    
  3. Common pit

    1. List
      • According to the index cycle to delete list elements when you want to delete in reverse order, otherwise the result is not what you want
    2. dictionary
      • Dictionary can not change the size of the dictionary at the time for the cycle
      • If you want to delete the data cycle, we need to define a list of elements within the dictionary list to delete the cycle
  4. Advanced coding

    encode()  #编码
    decode()  #解码
    #不同的密码本之间不能相互识别
    #数据在内存中全部是以Unicode编码的,但是当你的数据用于网络传输或者存储到硬盘中,必须是以非Unicode编码的(utf-8,gbk等)
    #用什么编码集编码,就用什么编码集解码
    

Guess you like

Origin www.cnblogs.com/xiaohei-chen/p/11879659.html