Day 06 data type supplement: Advanced coding

01 today's content outline

  1. Supplement of basic data types
  2. Conversion between data types
  3. Advanced coding

02 yesterday's content review and assignment explanation

  1. id is ==

    • ==: whether the values are the same
    • Is: is the memory address the same
    • id: get the memory address of the object
  2. Code block: a file, interactive command line is a code block

  3. Caching mechanism under the same code block (string resident mechanism):

    • An initialization command is encountered in a file. Now the data is queried in the dictionary. If the data exists, it will not be created. If it does not exist, the corresponding data will be created.

    • Object: all numbers, bool, almost all strings

    • Purpose: to improve performance and save memory

  4. Caching mechanism under different code blocks (small data pool): in memory, two spaces are opened, one for storing int s of -5~256 and one for storing strings of certain rules. If your code encounters data that meets the conditions, it will directly reference the data created in advance.

    • -5~256 int,bool, a string satisfying one rule.
    • Advantages: improve performance and save memory space
  5. Set: List de duplication, relationship test, intersection and difference

  6. Depth copy:

    • Shallow copy: create a new space in memory to store the copy object (dictionary list), but all the elements in it share the same data with the elements in the copied object.

      import copy
      l1 = [1,2,3,[11,22]]
      # l2 = l1.copy()	#Shallow copy
      l2 = copy.copy(l1)	#Shallow copy
      print(l2)
      print(id(l1),id(l2))     #Different address
      print(id(l1[-1]),id(l2[-1]))     #Same as content data
      

    • Deep copy: create a new space in the memory to store the copy objects (dictionary list), but the immutable elements in it use the same data, and the variable data creates a new space

      #Deep copy
      import copy
      l1 = [1,2,3,[11,22]]
      l2 = copy.deepcopy(l1)
      print(l2)
      print(id(l1),id(l2))
      print(id(l1[1]),id(l2[1]))      #The data types that cannot be changed follow the same
      print(id(l1[-1]),id(l2[-1]))    #Variable data types create new spaces
      

03 specific content

  • Supplement of data type

    • str

      # #Supplement of data type
      # # str: just practice the supplementary method once
      s1 = 'taiBai'
      
      # capitalize the first letter and lower case the rest
      print(s1.capitalize())
      #swapcase case flip
      print(s1.swapcase())
      
      #title capitalize the first letter of each word, and separate them when non letters are encountered
      msg = 'taibai say3hi'
      print(msg.title())
      
      # Center center
      s1 = 'barry'
      print(s1.center(20))
      print(s1.center(20,'*'))   #placeholder 
      
      # Find finds the index through the element and returns the first one; Cannot find return -1
      # Index finds the index through the element, and returns when the first one is found; Report an error if it cannot be found
      print(s1.find('a'))
      print(s1.find('r'))
      print(s1.find('o'))     #Cannot find -1
      print(s1.index('o'))    #Error reporting
      

    • tuple

      #tuple
      #If there is only one element in a tuple and there is no comma, it is not a tuple. It is the same as the data type of the element***
      tu1 = (2,3,4)
      print(tu1,type(tu1))
      tu2 = (2)    #No commas
      print(tu2,type(tu2))
      tu3 = ('Taibai')
      print(tu3,type(tu3))
      tu4 = ([1,2,3])
      print(tu4,type(tu4))
      tu5 = (1,)
      print(tu5,type(tu5))
      
      # Count count
      tu = (1,2,3,4,5,6,1,2,3,4,5,6,7,7,4,3,5,7)
      print(tu.count(4))
      
      #Index find index by content
      tu = ('Taibai', 'Day day', 'Taibai')
      print(tu.index(('Taibai')))   # 0 returns the index value. If it is repeated, the first one will be returned
      

    • list

      ##list
      l1 = ['Taibai', '123', 'goddess', 'Dazhuang']
      # count pass
      # index
      print(l1.index('Dazhuang'))
      
      #Sort sort***
      l1 = [5, 4, 3, 7, 8, 6, 1, 9]
      l1.sort()    #Default from small to large
      print(l1)    #[1, 3, 4, 5, 6, 7, 8, 9]
      l1.sort(reverse=True)   #Default from large to small
      print(l1)   #[9, 8, 7, 6, 5, 4, 3, 1]
      
      # Reverse reverse**
      l1.reverse()
      print(l1)
      
      #Lists can be added
      l1 = [1, 2, 3]
      l2 = [1, 2, 3, 'Taibai', '123', 'goddess']
      print(l1+l2)        #[1, 2, 3, 1, 2, 3, 'Taibai', '123', 'Goddess']
      
      #Multiply list by number (Repeat * times)
      l1 = [1, 'daf', 3]
      l2 = l1*3
      print(l2)       #[1, 'daf', 3, 1, 'daf', 3, 1, 'daf', 3]
      
      l1 = [11, 22, 33, 44, 55]
      # The elements corresponding to an odd number of indexes are deleted (they cannot be deleted one by one. This l1 is just an example, and the elements inside are variable).
      # ***Important
      # Normal thinking:
      # First round out all the indexes.
      # To judge, index% 2 = = 1:pop (index)
      for index in range(len(l1)):
          if index % 2 == 1:
              l1.pop(index)
      print(l1)
      #Properties of the list:
      l1 = [11, 22, 33, 44, 55]
      #The simplest:
      del l1[1::2]
      print(l1)
      l1 = [11, 22, 33, 44, 55]
      # Deleting elements in reverse order
      for index in range(len(l1)-1,-1,-1):
          if index % 2 == 1:
              l1.pop(index)
      print(l1)
      
      l2 = []
      for i in  range(len(l1)):
          if i % 2 == 0:
              l2.append(l1[i])
          i += 1
      l1 = l2
      print(l1)
      
      #Thinking replacement
      l1 = [11, 22, 33, 44, 55]
      new_l1 = []
      for index in range(len(l1)):
          if index % 2 ==0:
              new_l1.append(l1[index])
      # print(new_l1)
      l1 = new_l1
      print(l1)
      
      #When looping through a list, it is best not to change the size of the list, which will affect your final results.
      
      

    • dictionary

      # #Supplement of data type
      # # str: just practice the supplementary method once
      s1 = 'taiBai'
      
      # capitalize the first letter and lower case the rest
      print(s1.capitalize())
      #swapcase case flip
      print(s1.swapcase())
      
      #title capitalize the first letter of each word, and separate them when non letters are encountered
      msg = 'taibai say3hi'
      print(msg.title())
      
      # Center center
      s1 = 'barry'
      print(s1.center(20))
      print(s1.center(20,'*'))   #placeholder 
      
      # Find finds the index through the element and returns the first one; Cannot find return -1
      # Index finds the index through the element, and returns when the first one is found; Report an error if it cannot be found
      print(s1.find('a'))
      print(s1.find('r'))
      print(s1.find('o'))     #Cannot find -1
      print(s1.index('o'))    #Error reporting
      

      tuple

      If there is only one element in a tuple and there is no comma, it is not a tuple. It is the same as the data type of the element

      tu1 = (2,3,4)
      print(tu1,type(tu1))
      tu2 = (2) \
      print(tu2,type(tu2))
      tu3 = ('Taibai')
      print(tu3,type(tu3))
      tu4 = ([1,2,3])
      print(tu4,type(tu4))
      tu5 = (1,)
      print(tu5,type(tu5))

      Count count

      tu = (1,2,3,4,5,6,1,2,3,4,5,6,7,7,4,3,5,7)
      print(tu.count(4))

      Index find index by content

      tu = ('Taibai', 'Ritian', 'Taibai')
      Print (tu.index ('Too white ')) \0 returns the index value. If it is repeated, the first one will be returned

      list

      l1 = ['Taibai', '123', 'Goddess',' Dazhuang ']

      count pass

      index

      print(l1.index('dazhuang '))

      Sort sort

      l1 = [5, 4, 3, 7, 8, 6, 1, 9]
      l1.sort() \
      print(l1) #[1, 3, 4, 5, 6, 7, 8, 9]
      l1.sort(reverse=True) \
      print(l1) #[9, 8, 7, 6, 5, 4, 3, 1]

      Reverse reverse

      l1.reverse()
      print(l1)

      Lists can be added

      l1 = [1, 2, 3]
      l2 = [1, 2, 3, 'Taibai', '123', 'Goddess']
      Print (l1+l2) \1, 2, 3, 1, 2, 3, 'Taibai', '123', 'Goddess']

      Multiply list by number (repeat n times)

      l1 = [1, 'daf', 3]
      l2 = l1*3
      print(l2) #[1, 'daf', 3, 1, 'daf', 3, 1, 'daf', 3]

      l1 = [11, 22, 33, 44, 55]

      The elements corresponding to an odd number of indexes are deleted (they cannot be deleted one by one. This l1 is just an example, and the elements inside are variable).

      important

      Normal thinking:

      First round out all the indexes.

      To judge, index% 2 = = 1:pop (index)

      for index in range(len(l1)):
      if index % 2 == 1:
      l1.pop(index)
      print(l1)

      Properties of the list:

      l1 = [11, 22, 33, 44, 55]

      The simplest:

      del l1[1::2]
      print(l1)
      l1 = [11, 22, 33, 44, 55]

      Deleting elements in reverse order

      for index in range(len(l1)-1,-1,-1):
      if index % 2 == 1:
      l1.pop(index)
      print(l1)

      l2 = []
      for i in range(len(l1)):
      if i % 2 == 0:
      l2.append(l1[i])
      i += 1
      l1 = l2
      print(l1)

      Thinking replacement

      l1 = [11, 22, 33, 44, 55]
      new_l1 = []
      for index in range(len(l1)):
      if index % 2 ==0:
      new_l1.append(l1[index])

      print(new_l1)

      l1 = new_l1
      print(l1)

      When looping through a list, it is best not to change the size of the list, which will affect your final results.

      #Supplement to dictionary

      update

      dic = {'name':' too white ','age': 18}
      Dic Update (hobby ='motion ', height ='175') \
      Print (DIC) \name':' Taibai ','age': 18,'hobby':' sport ','height':'175'}
      Dic Update (name = 'Taibai Jinxing') \
      Print (DIC) \name':' Taibai Venus','age': 18,'hobby': 'motion','height':'175'}
      Dic Update ([(1,'a','b', (3,'c','d')) \
      Print (DIC) \name':' Taibai Venus','age': 18,'hobby': 'motion', 'height':'175', 1:'a', 2:'b', 3:'c', 4:'d'}

      dic1 = {"name":"jin","age":18,"sex":"male"}
      dic2 = {"name": "alex", "weight": 75}
      dic1.update(dic2) \
      print(dic1)
      print(dic2)

      fromkeys has a hole!!! Values share one!!!!

      dic = dict.fromkeys('abc',100)
      dic1 = dict.fromkeys([1,2,3],'alex')
      print(dic)
      print(dic1)

      dic = dict.fromkeys([1,2,3],[])
      dic[1].append(666)
      print(dic) #{1: [666], 2: [666], 3: [666]}

      dic = {'k1':' too white ','k2':'barry','k3': 'white','age': 18}

      #Delete key value pairs containing'k 'elements from the dictionary

      for key in dic:
      if 'k' in key:
      dic.pop(key)
      print(dic) \!!! Change dictionary size during iteration

      for key in list(dic.keys()):
      if 'k' in key:
      dic.pop(key)
      print(dic)
      ​```

  • Conversion of data types

    # , 0, '', (), {}, [], set(),None converted to bool value of False
    

  • Classification of data types (understanding)

  • Advanced coding

    • ASCII code: including English, numbers, special characters and 01010101 pairing relationship.

      A 01000001 each character is represented by one byte.

    • GBK: only local characters (English letters, numbers and special characters)

      Corresponding to 01010101.

      A 01000001 characters in ASCII code: one character is represented by one byte.

      Chinese 01001001 01000010 Chinese: one character is represented by two bytes.

    • Unicode: contains the correspondence between all words in the world and binary 01010101

      a 01000001 01000010 01000011 00000001

      b 01000001 01000010 01100011 00000001

      China 01001001 01000010 01100011 00000000

    • UTF-8: contains the correspondence between all the words in the world and binary 01010101. At least one character is represented by eight bits and one byte

      A 01000001 characters in ASCII code: one character and one byte represent

      To 01000001 01000010 (European characters: Portugal, Spain, etc.): one character is represented by two bytes

      Chinese 01001001 01000010 01100011 Asian characters: one character is represented by three bytes

  1. Can different codebooks identify each other? No.
  2. All data in memory is encoded in Unicode, but when your data is used for network transmission or hard disk storage, it must be encoded in non Unicode (utf-8, gbk, etc.).

English:

​ str: 'hello'

The Encoding in memory: Unicode

The Expression:'hello'

​ bytes:

The Encoding in memory: non Unicode

The Expression: b'hello'

Chinese:

​ str:

The Encoding in memory: Unicode

The Manifestation: 'China'

​ bytes:

The Encoding in memory: non Unicode \utf-8

The Presentation: b'\xe4\xb8\xad\xe5\x9b\xbd'

# str ----> bytes
s1 = 'China'
b1 = s1.encode('utf-8')  #coding
print(b1,type(b1))   #b'\xe4\xb8\xad\xe5\x9b\xbd' <class 'bytes'>
b1 = s1.encode('gbk')
print(b1,type(b1)) #b'\xd6\xd0\xb9\xfa' <class 'bytes'>

# bytes ----> str
b1 = b'\xe4\xb8\xad\xe5\x9b\xbd'
s1 = b1.decode('utf-8')
print(s1,type(s1))

#gbk ---->utf-8
b1 =  b'\xd6\xd0\xb9\xfa'
s1 =  b1.decode('gbk')
print(s1)
b2 = s1.encode('utf-8')
print(b2)      # b'\xe4\xb8\xad\xe5\x9b\xbd'

04 today's summary

  • Supplement to data types: list (sort, reverse, add, multiply, loop problem), dictionary dict (update, loop problem)***
  • Advanced coding:
    • Why do bytes exist?
    • Str--->bytes (unicode---> non Unicode)
    • gbk<--------->utf-8

05 preview content

Posted by ianhull on Mon, 30 May 2022 15:12:59 +0530