day9 summary and assignment

day9 summary

I aggregate

  1. Cognitive set

    A collection is a container type data type. Take {} as the flag of the container. Multiple data in it are separated by commas: {data 1, data 2, data 3,...}

    Variable (support addition, deletion and modification); Unordered (subscript operation is not supported)

    Requirements for elements: 1 Must be immutable data 2 Element is unique

    # Empty set
    set1 = set()
    print(type(set1), len(set1))
    
    # The collection is out of order
    print({1, 2, 3} == {3, 2, 1})       # True
    
    # Collection elements must be data of immutable type
    set1 = {1, 'acb', True, (10, 20)}
    print(set1)
    
    # set2 = {1, 'acb', True, [10, 20]}     # report errors!
    
    # Collection elements are unique
    set3 = {10, 20, 10, 20, 30, 10}
    print(set3)     # {10, 20, 30}
    
  2. Addition, deletion, modification and query of set

    1. Search - can only traverse

      Supplement: when traversing the unordered sequence with the for loop, the system will automatically convert the unordered sequence into a list before traversing, and then traverse the list

    2. increase

      1. Assemble Add (element) - adds the specified element
      2. Assemble Update - adds all the elements in the sequence to the collection
    3. Delete

      Assemble Remove (element)

  3. Mathematical set operation

    1. Sets and mathematical sets used in python are one thing, and support mathematical set operations

    2. Mathematical set operations supported by python: intersection (&), Union (|), difference (-), symmetric difference (^), subset (> =, < =), true subset (>, <)

      set1 = {1, 2, 3, 4, 5}
      set2 = {3, 4, 5, 6, 7, 8}
      
      # 1) Intersection - gets the common part of two sets
      print(set1 & set2)      # {3, 4, 5}
      
      # 2) Union - combines two sets into a single set
      print(set1 | set2)      # {1, 2, 3, 4, 5, 6, 7, 8}
      
      # 3) Difference set
      # Set 1 - Set 2 - get the rest of set 1 after removing the part contained in set 2
      print(set1 - set2)      # {1, 2}
      print(set2 - set1)      # {8, 6, 7}
      
      # 4) Symmetric difference sets - merge two sets and remove the common part
      print(set1 ^ set2)      # {1, 2, 6, 7, 8}
      
      # 5) Subsets and proper subsets
      # Set 1 > Set 2 - determines whether set 2 is a true subset of set 1
      # Set 1 < set 2 - determines whether set 1 is a true subset of set 2
      print({1, 2} < {1, 2, 3})       # True
      print({100, 200, 300, 400, 1, 2} > {1, 2})      # True
      
    3. Exercise: use three sets to represent the names of students who choose courses in three disciplines (a student can choose multiple courses at the same time)

      python = {'stu1', 'stu3', 'stu4', 'stu6', 'stu9', 'stu10'}
      java = {'stu2', 'stu3', 'stu5', 'stu6', 'stu9', 'stu10'}
      h5 = {'stu2', 'stu4', 'stu6', 'stu7', 'stu8', 'stu10'}
      
      # 1. How many students are there in total
      set1 = python | java | h5
      print(len(set1))
      
      # 2. Find the number of people who have only selected the first discipline and their corresponding names
      set2 = python - java - h5
      print(set2)
      
      # 5. Find the number of students who have selected three courses and their corresponding names
      set3 = python & java & h5
      print(set3)
      
      # 3. Find the number of students who have chosen only one subject and their corresponding names
      set4 = python ^ java ^ h5 - set3
      print(set4)
      
      # 4. Find the number of students who have only selected two subjects and their corresponding names
      set5 = set1 - set3 - set4
      print(set5)
      

    II Strings and characters

    1. What is a string (str)

      1. String is a container data type; Take '', '', '' and '' '' as the marks of the container. All the symbols in the quotation marks are string elements (string elements are also called characters)
      2. Immutable (addition, deletion and modification are not supported), orderly (subscript operation is supported)
      3. Elements of string: all symbols can be used as elements of string, and the elements of string are also called characters. There are two types of characters: ordinary characters and escape characters
    """
    str1 = 'abcsdfa'
    str2 = "abc123"
    
    str3 = '''abc123
     Dr. Ashi and
     Dr. Ashi and
     Love to be coquettish is'''
    
    str4 = """
            man
            Asterisk plus semicolon asd
            Assad card number
            Assad card number
    """
    print(type(str3), type(str4))       # <class 'str'> <class 'str'>
    
    # Empty string
    str5 = ''
    print(len(str5))        # 0
    
    str6 = ' '
    print(len(str6))        # 1
    
    # The character can be any symbol
    str7 = 'sfhJAMF234 Doctor SARS~!@#$%([}❀😁'
    print(str7)
    
    1. Character - each individual symbol in a string is a character

      1. Characters are divided into ordinary characters and escape characters

        1. Ordinary characters - characters representing the symbol itself are ordinary characters

        2. Escape character - a symbol with a special function or meaning is an escape character. General format of all escape characters: \ X, \ XXXX (escape characters are formed by adding \ to ordinary characters, but not all ordinary characters can be changed into escape characters by adding \ before them)
          \n - line feed
          \t - Horizontal tab (equivalent to pressing tab once)
          ’-A single quotation mark
          "- a double quotation mark
          \- a backslash

          Note: the length of all escape characters is 1

          1. \u4 digit hexadecimal number - coded character (4-digit hexadecimal number is the coded value of the character in the computer)
          2. Hexadecimal number - the number on each bit can be 09 or af (A-F)

    III Character encoding

    1. Principle of computer data storage

      1. The data that the computer can directly store is only numbers. When saving numbers, the complement of numbers is saved.
      2. If you want to save non digital data to the computer, you must first convert the data into numbers when storing.
      3. If you need to store text, the computer will save the coding value corresponding to the text symbol.
    2. Character encoding

      1. Each text symbol (character) corresponds to a fixed number in the computer, and this number is the coded value of this symbol.
    3. Character coding table - a table that records the correspondence between each symbol and number

      1. Common code table: ASCII code table, Unicode code table (Python)

        1. ASCII code table
          ASCII code table only has 128 characters (not enough)
          In ASCII code table: numeric characters in front of letters, uppercase letters in front of lowercase letters, there is a gap between uppercase letters and lowercase letters, numbers increase continuously from 0, letters increase continuously from (a/A), a - 97 and A - 65
        2. Unicode code table - is an extension of ASCII code table, which contains ASCII code table
          The Unicode code table contains all symbols (Universal codes) of all countries and nationalities in the world
          Chinese code range: 4e00 ~ 9fa5
    4. Application of coded value in python

      1. chr function: chr (coded value) - the character corresponding to the coded value; Coded value - integer

      2. Ord function: ord (character) - get the encoding value corresponding to the character; Character - a string with a length of 1

      3. Coded character

        print(chr(97), chr(65))
        # print(chr(0x2800))
        
        # The program directly represents a hexadecimal number: prefix 0x/0X
        # for x in range(0x1800, 0x18AF+1):
        #     print(chr(x), end=' ')
        
        # Hex - converts a number to hexadecimal
        print(ord('more than'), ord('Ting'), ord('a'))
        print(hex(20313), hex(23159))
        
        # Coded character
        str1 = 'a\u0061 one\u4e00'
        print(str1)
        
        # Judge whether the specified character is Chinese
        ch = 'yes'
        print('\u4e00' <= ch <= '\u9fa5')
        print(chr(0x9fa5))
        

    IV Common string operations

    1. Query - get data

      The string gets as like as two peas for the character and the list.

      str1 = 'hello world!'
      print(str1[1], str1[-1])
      
      str2 = '\t123\u5fa3abc'
      print(str2[5])
      
      str1 = 'hello world!'
      print(str1[-3:])        # 'ld!'
      print(str1[-5::-1])     # 'ow olleh'
      
      for x in str1:
          print(x)
      
    2. String related operations

      1. +,*
      str1 = 'abc'
      str2 = '123'
      print(str1 + str2)      # 'abc123'
      print(str1 * 3)     # 'abcabcabc'
      
      1. Compare size: >, <, > =<=

        The two strings compare the size, which is the encoded value size of the first pair of unequal characters;

        Application: judge the nature of characters
        a. Judge whether the character x is a numeric character: '0' < = x < = '9'
        b. Judge whether the character x is lowercase: 'a' < = x < = 'z'
        c. Judge whether the character x is A capital letter: 'A' < = x < = 'Z'
        d. Judge whether the character x is a letter: 'a' < = x < = 'Z' or 'a' < = x < = 'Z'
        e. Judge whether the character x is Chinese: '\ u4e00' < = x < = '\ u9fa5'

      2. In and not in

        String 1 in string 2 - judge whether string 1 is in string 2 (judge whether string 1 is a substring of string 2)

        str1 = 'abc123'
        print('a' in str1)      # True
        print('abc' in str1)    # True
        print('23' in str1)     # True
        print('13' in str1)     # False
        

    task

  4. Enter a string and print all characters in odd digits (subscripts are characters in bits 1, 3, 5, 7...)

    For example, input 'abcd1234' and output 'bd24'

    str1 = 'abcd1234 '
    print(str1[1::2])
    
  5. Enter the user name and judge whether the user name is legal (the length of the user name is 6 ~ 10 characters)

    name = input('enter one user name:')
    if 6 <= len(name) <= 10:
        print('legitimate')
    else:
        print('wrongful')
    
  6. Enter the user name to judge whether the user name is legal (the user name can only be composed of numbers and letters)

    For example, 'ABC' - Legal '123' - Legal 'abc123a' - Legal

    name = input('enter one user name:')
    for i in name:
        if not('0' <= i <= '9' or 'a' <= i <= 'z' or 'A' <= i <= 'Z'):
            print('wrongful')
            break
    else:
        print('legitimate')
    
  7. Enter the user name and judge whether the user name is legal (the user name must contain and can only contain numbers and letters, and the first character must be capital letters)

    For example, 'ABC' - illegal 'Mabc' - illegal '123' - illegal 'abc123' - illegal 'Abc123ahs' - Legal

    name = str(input("Please enter user name:"))
    if not('A' <= name[0] <= 'Z'):
        print("wrongful")
    else:
        for i in name:
            if not ('0' <= i <= '9' or 'A' <= i <= 'Z' or 'a' <= i <= 'z'):
                print("wrongful")
                break
        else:
            print("legitimate")
    
  8. Enter a string and take out all the numeric characters in the string to produce a new string

    For example: input * * 'abc1shj23kls99+2kkk' * * output: '123992'

    str1 = 'abc1shj23kls99+2kkk'
    str2 = ''
    for i in str1:
        if  '0' <= i <= '9':
            str2 += i
    print(str2)
    
  9. Input a string and output all lowercase letters in the string into corresponding uppercase letters (implemented by upper method and self writing algorithm)

    For example: input * * 'A2H2KLM12 +' * * output 'A2H2KLM12 +'

    # Method 1
    str1 = 'a2h2klm12+'
    str2 = str1.upper()
    print(str2)
    
    # Method 2
    str1 = 'a2h2klm12+'
    str2 = ''
    for i in str1:
        if 'a' <= i <= 'z':
            i = chr(ord(i)- 32)
            str2 += i
        else:
            str2 += i
    print(str2)
    
  10. Enter a number less than 1000 to generate the corresponding student number

    For example: input * * '23', output 'py1901023' * * input * * '9', output 'py1901009' * * input * * '123', output 'py1901123'**

    num = input('Please enter a positive integer less than 1000:')
    a = len(num)
    student_number = ''
    if a == 1:
        student_number  = 'py190100' + num
    elif a == 2:
        student_number = 'py19010' + num
    else:
        student_number = 'py1901' + num
    print(student_number)
    
  11. Enter a string to count the number of non alphanumeric characters in the string

    For example: input * * 'anc2 + 93 SJ nonsense' * * output: 4 input * * '= = =' * * output: 3

    count = 0
    str1 = 'anc2+93-sj nonsense'
    for i in str1:
        if not ('0' <= i <= '9' or 'A' <= i <= 'Z' or 'a' <= i <= 'z'):
            count += 1
    print(count)
    
  12. Enter a string, change the beginning and end of the string into '+' to produce a new string

    For example: input string * * 'abc123', output '+ bc12 +'**

    str1 = 'abc123'
    str2= str1[1:-1]
    str3 = '+'
    str4 = str3 + str2 + str3
    print(str4)
    
  13. Enter a string to get the middle character of the string

    For example: input * * 'abc1234' * * output: '1' input * * 'abc123' * * output * * 'c1'**

    str1 = input('Please enter a string:')
    a = len(str1)
    if a % 2 == 0:
        print(str1[a // 2 - 1],str1[a // 2],sep='')
    else:
        print(str1[a // 2])
    
  14. The writer realizes the function of string function find/index (get the position of string 2 in string 1 for the first time)

    For example, string 1 is: how are you? Im fine, Thank you! , String 2 is: you, print 8

    str1 = 'howy are you? Im fine, Thank you!'
    str2 = 'you'
    if str2 in str1:
        for i in range(len(str1)+1):
                if str1[i] == str2[0]:
                    if str1[i + 1] == str2[1]:
                        print(i)
                        break
    
  15. Gets the common characters in two strings

    For example, string 1 is: abc123, string 2 is: huak3, print: common characters are: a3

    str1 = input('Please enter the first string:')
    str2 = input('Please enter the second string:')
    for x in str1:
        if x in str2:
            print(x, end='')
    

Keywords: Python

Added by acirilo on Thu, 24 Feb 2022 15:13:35 +0200