1, yesterday Recap
Own more practice
2, work
# Job # 1, the following variables (TU is ancestral), implement the required functionality. tu = ( "alex", [ 11,22, { "k1": 'V1', "k2": [ "age", "name"], "k3" :( 11,22,33)}, 44] ) # a. about ancestral characteristics. # Can not be changed son, grandson might be able to change # the first element of b. May I ask tu variable whether "alex" can be modified?. # Can not # c. I ask tu corresponding value of the variable "k2" What type? Can be modified? If you can, please add an element of "Seven". # K2 corresponds to a list can be changed, (meta {character [list] Code} Group) # TU [. 1] [2] [ 'K2']. The append ( 'Seven') # Print (TU) # D. I ask tu variables corresponding to the value "k3" What type? can be modified? If you can, please add an element of "Seven". # Tuple is not # 2, the dictionary DIC, DIC = { 'K1': 'V1', 'K2': 'V2', 'K3': [11,22,33]} . #A is iterate through all of the output Key for i in dic.keys (): . #c iterate through all of the key and the output value. for K, V in dic.items (): Print (K, V) for I in dic.items (): Print (I) . #d add the dictionary a key-value pair, "k4": "v4" , added after the output dictionary. DIC [ 'K4'] = 'V4' Print (DIC) #E. Please, outputting the dictionary edit "k1" corresponds to a value "alex" modification in the dictionary. dic.setdefault ( 'K4', 'V4') Print (DIC) #f. append the element 44, the output dictionary k3 corresponding modified value. DIC [ 'k3'] = [11,22,33,44] Print (DIC) #G. Please insert element 18, the output of a modified dictionary k3 values corresponding to the position. DIC = DIC1 [ 'K3'] dic1.insert (1,18) DIC [ 'K3'] = DIC1 Print (DIC) #. 3, element classification. # have the following values li = [11,22,33,44,55 , 66,77,88,99,90], # Save all values greater than 66 to the first key in the dictionary. # 66 is less than the stored value to a value of the second key. # Namely: { 'k1': a list of all values greater than 66, 'k2': a list of all values less than 66} li = [11,22,33,44,55,66, l_greater = [] # 66 greater than the list of all l_less = [] # 66 is less than all listings for I in Li: IF I = 66 =: Continue IF I> 66: l_greater.append (I) the else: l_less.append (I) dic.setdefault ( 'K1', l_greater) dic.setdefault ( 'K2', l_less) Print (DIC) #. 4, output of goods list, the user enters the serial number, displays the user selected commodity # commodity li = [ "phone", "computer", "mouse pad", "yacht"] requirements: # 1, page display number + product name, such as: # 1 phone # 2 computer # ... ... # 2, a user input selection of a product number, product name and then print # 3, if the user enters the product number is incorrect, you are prompted an error, and re-enter. # 4, a user input Q or q, exit the program. 1 the while: li = [ "phone", "computer", " Print (. '} {\ T {}' the format (li.index (I) +. 1, I)) num = input ( 'Enter the product number / input Q or q quit:') IF num.isdigit (): NUM = int (NUM) IF 0 <NUM and NUM <= len (Li): Print (NUM Li [- . 1]) the else: Print ( "Please enter a valid number ') elif num.upper () ==' Q ': BREAK the else: Print (" enter a number ")
3, the difference python2, python3 of
python 2: python 3:
print() print'abc' print('abc')
range () xrange () generator range ()
raw_input() input()
4、= == is
=: Assign a value to another value
==: compare values for equality
is: comparison, compare the memory address id (content)
li1 = [1,2,3] li2 = li1 case li1 and li2 with whether a memory address
print (li1 is li2) #is is a result of the comparison memory address Ture
print (id (li1), id (li2)) # id is checked li1 and li2 memory address, same result
5, number, string
Small data pool: space-saving to some extent,
Digital range: -5--256
String: 1, can not have special characters special
2, s * 20 is the same address, s * 21 are two addresses after
l1 = 6
l2 = 6
print(id(l1),id(l2))
The remaining: list dict tuple set no small data pool concept
6, coding
ascii:
A: 00000010 a byte 8
unicode:
A: 00000000 00000001 00000010 00000100 32 four bit bytes
In: 0,000,000,000,000,001 0,000,001,000,000,110 four-byte 32-bit
utf-8:
A: 0010 0000 8 bits of a byte
In: 00000001 0,000,001,000,000,110 three bytes 24
gbk:
A: 00000110 8 bits of a byte
In: 0,000,001,000,000,110 two bytes 16
Binary between (1) the individual is not coded each other, will be garbled.
Stored transmission (2) file can not be unicoide (only utf-8 utf-16 gbk gb2312 asciid etc.)
python3:
str unicode stored in memory using them.
bytes type
For English:
str: Expressions: s = 'alex'
Encoding: 010101010 unicode
bytes: Expressions: s = b'alex '
Encoding: 000101010 utf-8 gbk
For Chinese:
str: forms: s = 'China'
Encoding: 010101010 unicode
bytes: Expressions: s = b'x \ e91 \ e91 \ e01 \ e21 \ e31 \ e32 'denotes a three-byte Chinese
Encoding: 000101010 utf-8 gbk
# s = 'alex'
# s1 = b'alex'
# print(s,type(s))
# print(s1,type(s1))
# Encode encoding externally str -> bytes inside the # unicod -> utf-8, gbk () may be disposed within the coding S1 = 'Alex' S11 = s1.encode ( 'UTF-. 8') S12 = S1 .encode ( 'GBK') Print (S11) # b'alex ' Print (S12) S2 =' Chinese ' S22 = s2.encode (' UTF-. 8 ') S23 = s2.encode (' GBK ') Print (S22 ) Print (S23)
# Job to explain cart # commodity buyers and sellers money li = [ { 'name': 'apple', '. Price': 10}, { 'name': 'banana', '. Price': 20}, { 'name' : 'watermelon', '. price': 30}, ] # the goods on the shelves shopping_car = {} Print ( "Welcome to the big hammer fruit shop ') money = the INPUT (' Show me your money ') money.isdigit IF () and int (Money)> 0: the while. 1: for I, K in the enumerate (Li): Print ( '{number}, {} commodity price {}' format (i, k [ '. name '], K ['. price '])) the Choose = iNPUT (' enter the product number you want to buy ') IF choose.isdigit () and int (the Choose) <len (Li): NUM = iNPUT (' you to get the number of items to buy ') IF NUM.isdigit():isdigit(): if int(money) > li[int(choose)]['price']*int(num): money = int(money)-li[int(choose)]['price']*int(num) IF Li [int (the Choose)] [ 'name'] in shopping_car: shopping_car [Li [int (the Choose)] [ 'name']] = shopping_car [Li [int (the Choose)] [ 'name']] + int ( NUM) the else: shopping_car [Li [int (the Choose)] [ 'name']] = int (NUM) Print ( 'cart obtained goods are {}, your balance is {}' format (shopping_car, money .) ) the else: Print ( 'beast, go back to your wife for money') the else: Print ( 'say the number is a')