Last job
1 '' ' 2 1 classification element 3 has the following value li = [11,22,33,44,55,66,77,88,99,90], to save all values greater than 66 to a first dictionary key, the 4 values less than 66 to a value stored in the second key. 5 that is: { 'k1': a list of all values greater than 66, 'k2': a list of all values less than 66} . 6 '' ' . 7 Li = [11,22,33,44,55,66,77,88, 99,90 ] . 8 li_higher = [] . 9 li_lower = [] 10 DIC = {} . 11 for I in Li: 12 is IF I == 66 : 13 is Continue 14 elif I> 66 : 15 li_higher.append (I) 16 the else : . 17 li_lower.append (I) 18 is dic.setdefault ( ' K1 ' , li_higher) . 19 dic.setdefault ( ' K2 ' , li_lower) 20 is Print (DIC) 21 is 22 is ' '' 23 is 2 the output of the commodity list, the user input number, displays the user selected commodities 24 commodity li = [ "phone", "computer", 'mouse pad', 'yacht'] 25 : 1: page displays the number + product name, such as: 26 1 phone 27 2 computer 28 ... 29 2: select the user to enter the serial number of the product, and then print the product name 30 3: If the user enters the product number is incorrect, you are prompted an error, and re-enter. 31 is 4: a user input Q or q, exit the program. 32 33 is '' ' 34 is Li = [ " mobile phone " , " computer " , " mouse pad ' , ' boat ' ] 35 for I in Li: 36 Print ( ' {} \ {} T ' .format (li.index ( I) +. 1 , I)) 37 [ In flag = True 38 is # have the flag 39 the while In flag: 40 Choice = INPUT (" Please enter a number or select a product exit by Q: ' ) 41 is IF choice.isdigit (): 42 is Choice = int (Choice) 43 is # determines whether the input number in the index range 44 is IF Choice> 0 and Choice <= len ( Li): 45 Print (Li [Choice +. 1 ]) 46 is elif choice.upper () == ' Q ' : 47 BREAK 48 the else : 49 Print ( " enter a number " )
1. Small knowledge summary
1 # 1 to python3 python2 difference of 2 python2 . 3 print () print ' ABC ' # may not print when added in parentheses . 4 Range () xrange (): is a generator . 5 the raw_input () . 6 . 7 python3 . 8 print ( ' ABC ' ) . 9 Range () 10 INPUT () . 11 12 is # 2. = assignment == equality comparison value is compared to the memory address comparing id (content) that is the memory address 13 is LI2 = in Li1 14 Print (id (in Li1), ID (LI2)) # is the same 15 16 # 3 for a small pool of digital data, the concept of string 17 range of the digital -5 - 256 18 is the string: 1 , can not have special characters . 19 2, S * or 20 is the same address, S * 21 is after all two address 20 is I1. 6 = 21 is I2. 6 = 22 is Print (ID (I1), ID (I2)) 23 is I1 = 300 24 I2 = 300 25 Print (ID (I1), ID (I2)) 26 is 27 # rest the list dict tuple set no concept of small data pool 28 L1 = [. 1 ,] 29 L2 = [. 1 ,] 30 Print (L1 iS L2)
2. coding
. 1 '' ' ASCII 2 A: 00000010. 8 bits of a byte . 3 Unicode . 4 A: 00000000 00000100 0,000,000,100,000,010 four 32-bit byte 5 in: 0,000,000,000,000,001 0,000,001,000,000,110 four 32-bit bytes . 6 UTF-. 8 . 7 A: 00100000 8 bits of a byte 8 are: 00000001 0,000,001,000,000,110 three 24 byte . 9 GBK 10 a: 00000110 8 bits of a byte 11 in which: 0,000,001,000,000,110 two 16-bit byte 12 is 1, the binary between individual coding, is not recognize each other, it will be garbled. 13 is 2, the file storage, transmission, is not unicode (only utf-8 utf-16 gbk, gb2312, ascii , etc.) 14 15 Py3: 16 str unicode is encoded in memory. . 17 bytes after the type to add B 18 is for English: . 19 STR: expressions: S = 'Alex' 20 is encoding: 010101010 Unicode 21 is bytes: expressions: S = b'alex ' 22 is encoding: 000101010 utf-8 gbk . . . . 23 is 24 for Chinese: 25 STR: Expressions: s = 'Chinese' 26 encoding: 010101010 Unicode 27 bytes: expressions: S = b'x \ E91 \ E91 \ E01 \ E21 \ E31 \ E32 ' 28 encoding: 000101010 utf-8 gbk. . . . 29 '' ' Alex ' 32 # encode encoding, how STR -> bytes 33 is S11 = s1.encode ( ' UTF-. 8 ' ) 34 is S11 = s1.encode ( ' GBK ' ) 35 Print (S11) 36 S2 = ' Chinese ' 37 = s2.encode S22 ( ' utf-8 ' ) 38 is S22 = s2.encode ( ' gbk ' ) 39 Print (S22)
will be converted into bytes str
with unicode encode encoding py3 form is converted into the form of gbk or utf-8
ps: encode encoding
decode decodes