python daily practice (five)

# 1. list function you know the dictionary (dictionary exclusive). 
"" " 
Dict.pop () to delete an element and returns 
dict.get () to get through key elements, if not the key to return None 
dict.updtae () key batch update absence of such bonds has increased update 
dict. keys () Gets a list of all keys generated imitation 
dict.values () Gets a list of all attributes generated imitation 
dict.setdefault () to add new keys will not have as 
dict.items () to turn the key element group 
"" " 
# 2. list your understanding of the function of the collection (collection of unique). 
"" " 
Set.discard () to remove elements 
set.add () Additional elements 
set.union () generates and returns a new set collection 
set.intersection () returns the intersection generates a new set of 
set.difference () returns a new generation difference set collection 
set .update () is a collection of batch updates if no updates repeated duplicate values will not be updated 
"" " 
# 3. list your understanding can be converted to a Boolean value and the value to False. 
"" " 
" "0, [], {}, () 
" "" 
# 4. 
while True:
# - cycle prompts the user to acquire a value corresponding to the key value according to the user dictionary input and output. 
    s = input ( "Please enter the name or Hobby") 
    for I in info: 
        IF S I ==: 
            Print (info [I]) 

# - cycle prompts the user, is obtained according to the key input of the user dictionary corresponding and outputs the value (if the key does not exist, then get the default "key does not exist", and outputs). 
# Note: regardless of the cycle is aborted (you can write an infinite loop) 

the while True: 
    S = the INPUT ( "Please enter the name or Hobby") 
    v = info.get (S, 'key does not exist') 
    Print (v) 


# 5. use code verification "name" is in the dictionary keys? 
info = { 'name': 'Gang egg', 'hobby': 'hammer', 'Age': '18 is'} 
IF 'name' in info.keys (): 
    Print ( 'present') 
the else: 
    Print ( 'does not exist') 


# 6. use code verification "alex" whether the value in the dictionary? 
info = { 'name': 'Gang egg', 'hobby': 'hammer', 'age': '18 '
 
v1 = { 'Wu Pei Qi', 'Jie', 'Bai', 'View goddess'} 
v2 = { 'Jie', 'View goddess'} 
# - Please give v1 and v2 intersection and outputs 
S1 = v1.intersection (v2) 
Print (S1) 
# - Please give v1 and v2 and the output current, and 
S2 = v1.union (v2) 
Print (S2) 
# - Please give v1 and v2 and the output differential current 
v1.difference = S3 (v2) 
Print (S3) 
# - Please give v2 and v1 and outputs the difference set 
S4 = v2.difference (v1) 
Print (S4) 


# cycles. 8 prompts the user, and inputs the content to append. list (if the input is stopped N or n cycles) 
LST = [] 
the while True: 
    S = iNPUT ( 'please feel free to enter, update stop input N') 
    IF s.upper () == 'N': 
        BREAK 
    the else: 
        LST .append (S) 
Print (LST) 

#. 9.Cycle prompts the user, and inputs the contents to the collection (n if the input is stopped or N cycle)
SET = SE ()  
the while True:
    S = INPUT ( 'please feel free to enter, exit the input input n') 
    IF s.lower () == 'n': 
        BREAK 
    the else: 
        se.add (S) 
Print (SE) 

# 10. The write code achieve 
v1 = { 'alex', 'Wu sir', 'Shaw large'} 
v2 = [] 
# # cycle prompts the user to input, if the input value is present in v1, then added to the v2 if the v1 does not exist, Add to v1 in. (N or n if the input is stopped cycle) 
the while True: 
    S = INPUT ( 'Enter (exit N input):') 
    IF s.upper () == 'N': 
        BREAK 
    elif S in V1: 
        v2.append ( S) 
    the else: 
        v1.add (S) 
Print (v1) 
Print (v2) 


# 11. determine the following key values that do dictionary? Collection of elements that do? 
# --1 dictionaries and collections can 
# - -1 dictionaries and collections can 
# - '' a collection of dictionaries and can 
# - None dictionaries and collections can 
# - [1, 
# - (1) can be set, and the dictionary 
# - {11, 22, 33, 4} dictionaries can not, can not set 
# - { 'name': ' wupeiq', ' age ': 18} dictionary can not, can not set 



# 12. is == and the difference between? 
"" " 
IS: is more variable memory address 
==: property value is relatively variable 
" "" 



# 13.type use and effect? 
# View object type, easier for developers to know what type of variable 

# 14.id use and role of? 
# View object memory address where the developer than the same variable points to a memory address 

15. See Code # and write results explain 
# v1 = { 'k1': 'v1', 'k2': [1, 2, . 3]} 
# = {V2 'K1': 'V1', 'K2': [. 1, 2,




V2 = V1 # 
# Print (RESULT1) Results: true 
# Print (result2) Results: true 
reasons: v2 = v1 corresponds to two variables point to the same memory address, () verification ID 


# 17. A look at the code WriteResult and explain why 
# V1 = { 'K1': 'V1', 'K2': [. 1, 2,. 3]} 
# V2 = V1 
# V1 [ 'K1'] = 'wupeiqi' 
# Print (V2) results: v2 [ 'K1'] = 'wupeiqi' 
# reasons: variable points to a memory address of a variable other operating variables change 


# 18 to see the results and explain the reasons for writing the code 
# v1 = 'life is short, I used Python' 
# v2 = [. 1, 2,. 3,. 4, V1] 
# = V1 "life is short, with wool the Python" 
# Print (V2) 
"" " 
Cause: 
modifications 1. string corresponds reassigned because the string is Ha Greece can not be modified, it will only open up a new memory address 
2.v1 string reassigned after the memory address v1 v1 operating outside the list and v2 are not the same address 
. "" " 


# 19. look to write code results and explain 
# info = [1, 2,3]
# userinfo = {'account': info, 'num': info, 'money': info}Who is eager to learn Python? "the append (9) 
# Print (UserInfo) 
# info = "questions how so much."









# 
# print(user_list)
"""
Reasons: 
1.user_list added info for cycling through a list of a total of 10 times, noted that user_list elements in the list is the info 
2. When the info element properties change, the corresponding info in user_list may also change because user_list info element in the memory address 
and info list memory address is the same. 
"" " 

# 23 to see the results and to write the code explain 
# Data} = { 
# for I in Range (10): 
# Data [ 'User'] = I 
# # Print (Data) 
" "" 
Results: 9 
reasons: 
1. initialize the data dictionary is empty 
2. cycled 10 times, data repeatedly updated user key 10 times so that the index value i = 9 starts from 0 
the index print 3. the user key 9 
"" " 

# 24. See explain the results and to write code 
# DATA_LIST = [] 
# for I in Range (10): 
# = {} Data 
# Data [ 'User'] = I 
# data_list.append (Data) 
# Print (DATA_LIST) 
"" "
1 cycle 10 times 1 = 9, the index value starting from 0. 
set.update () is a set of batch updates repeated if no duplicate values ​​will not be updated in the updateYou know the list can convert False value to a Boolean value, which is. 
"" "
"" " 
" "0, [], {}, () 
" "" 
# 4. implemented in code 
info = { 'name': 'Gang egg', 'hobby': 'hammer'} 
# - cycle It prompts the user to acquire a value corresponding to the key value according to user input and output to the dictionary. 
True the while: 
    S = INPUT ( "Please enter the name or Hobby") 
    for I in info: 
        IF S I ==: 
            Print (info [I]) 

# - cycle the user is prompted, according to the key value input by the user to the dictionary and obtaining a value corresponding to the output (if the key does not exist, then get the default "key does not exist", and outputs). 
# Note: regardless of the cycle is aborted (you can write an infinite loop) 

the while True: 
    S = the INPUT ( "Please enter the name or Hobby") 
    v = info.get (S, 'key does not exist') 
    Print (v) 


# 5. use code verification "name" is in the dictionary keys? 
info = { 'name': 'Gang egg', 'hobby': 'hammer', 'Age': '18 is'} 
IF'


# 6. use code verification "alex" whether the value in the dictionary? 
info = { 'name': 'Gang egg', 'hobby': 'hammer', 'Age': '18 is'} 
IF 'Alex' in info.values (): 
    Print ( 'present') 
the else: 
    Print ( 'absent') 

# 7 has the following 
v1 = { 'Wu Pei Qi', 'Jie', 'Bai', 'View goddess'} 
V2 = { 'Jie', 'View goddess'} 
# - Please give v1 and v2 and outputs the intersection 
S1 = v1.intersection (v2) 
Print (S1) 
# - Please give v1 and v2 and the output current, and 
S2 = v1.union (v2) 
Print (S2) 
# - Please give the difference between v1 and v2 and outputs the set 
S3 = v1.difference (v2) 
Print (S3) 
# - Please give v2 and v1 and outputs the difference set 
S4 = v2.difference (v1) 
Print (S4) 


#. 8.Cycle prompts the user, and inputs the content added to the list (if the input is stopped N or n cycles) 
LST = [] 
    S = INPUT ( 'please feel free to enter, update stop input N') 
    IF s.upper () == 'N':
        BREAK 
    the else: 
        lst.append (S) 
Print (LST) 

# 9. The cycle prompts the user, and inputs the contents to the collection (n if the input is stopped or N cycle) 
SE = SET () 
the while True: 
    S = INPUT ( "Please enter the random input to exit the input n ') 
    IF s.lower () ==' n ': 
        BREAK 
    the else: 
        se.add (S) 
Print (SE) 

# 10. The write code to implement 
v1 = {' alex ', 'Wu SIR', 'large Shaw'} 
v2 = [] 
# # cycle prompts the user to input, if the input value is present in v1, v2, is added to the, if v1 does not exist, it is added to the v1. (N or n if the input is stopped cycle) 
the while True: 
    S = INPUT ( 'Enter (exit N input):') 
    IF s.upper () == 'N': 
        BREAK 
    elif S in V1: 
        v2.append ( S) 
    the else: 
Print (V1) 
Print (V2)


# 11. The judge can do the following key value that the dictionary? Collection of elements that do? 
# - a dictionary and can set 
# - -1 dictionary and can set 
# - "" dictionary and can set 
# - None dictionaries and can set 
# - [1, 2] dictionary can not be set 
# - (1, ) dictionary and can set 
# - {11, 22, 33, 4} dictionaries can not, can not set 
# - { 'name': ' wupeiq', 'age': 18} dictionary can not, can not set 



# 12. is = and = difference? 
"" " 
IS: is more variable memory address 
==: property value is relatively variable 
" "" 



# 13.type use and effect? 
# View object type, easier for developers to know what type of variable 

# 14.id use and role of? 
# View object memory address where the developer than the same variable points to a memory address 

15. See Code # and write results explain 
# v1 = { 'k1': 'v1', 'k2': [1, 2, . 3]} 
# = {V2 'K1': 'V1', 'K2': [. 1, 2, 
# Print (result2) result: False 
# reasons: result1 comparison is the memory address elements result2 collection of relatively equal to the set every reassigned opened a new memory space 




# 16. Look to write code results and explain 
# V1 = { 'K1': 'V1', 'K2': [. 1, 2,. 3]} 
# V1 = V2 
# RESULT1 V1 = V2 == 
# result2 IS V1 = V2 
# Print (RESULT1) results: true 
# Print (result2) results: true 
reasons: v2 = v1 corresponds to two variables point to the same memory address, () verification ID 


# 17. a look at the code and write the results explain 
# v1 = { 'k1 ':' V1 ',' K2 ': [. 1, 2,. 3]} 
# V2 = V1 
# V1 [' K1 '] =' wupeiqi ' 
# Print (V2) results: v2 [' k1 '] = ' wupeiqi ' 
# reasons: variable points to a memory address of a variable other operating variables change 


# 18 to see the results and explain the reasons for writing the code 
# v1 = 'life is short, I used Python' 
# v2 = [1, 2, 3, 4,v1] 
# v1 = "Life is short, with wool Python" 
# Print (v2) 
"" "
The reason:Look at the code and write the results explain 
# info = [1, 2, 3]
1. Modify the string equivalent of re-assignment, because the string is hashed can not be modified, it will only open up a new memory address 
after 2.v1 string reassigned outside the list v1 and v2 v1 operating in memory address is not the same address 
"" " 


# 19 to see the results and to write the code explain 
# info = [. 1, 2,. 3] 
# = {UserInfo 'Account': info, 'NUM': info, 'Money' : info} 
# info.append (9) 
# Print (userinfo) 
# info = "question how much" 
# # Print (userinfo) 
"" " 
reasons: 
1. the above title is the same as when the info reassigned after the userinfo info memory address and a memory address is not the same 
. "" " 



# 20 to see the results and to write the code explain 
# info = [. 1, 2,. 3] 
# = UserInfo [info, info, info, info, info] 
# info [ 0] = 'is not only more difficult but also special what does' 
# Print (info, UserInfo) result: 0 index will become the first string, list and did not re-open space, just revised its list of elements, or the original address 



# 21 to see the results and explain the reasons for writing the code
UserInfo = # [info, info, info, info, info] 
# UserInfo [2] [0] = 'shut' 
# Print (info, UserInfo) Results: The original address has not changed but the elements in the list of changes 



# 22. See explain the results and to write code 
# info = [. 1, 2,. 3] 
# user_list = [] 
# Range for in Item (10): 
# user_list.append (info) 
# 
# info [. 1] = "Who is Python learn the "? 
# 
# Print (user_list) 
" "" 
reasons: 
1.user_list added info for loop through a list of a total of 10 times, noted that user_list elements in the list is the info 
2. when the info element properties change of , the corresponding info user_list also changed, because the memory address user_list info element in 
the list of info memory address is the same. 
"" " 

# 23 to see the results and to write the code explain 
# Data} = { 
# for I Range in (10): 
# Data [ 'User'] = I 
# # Print (Data) 
""" 
The results: 9
Reason:  
1. initialize data dictionary is empty
2. cycled 10 times, data repeatedly updated user key 10 times so that the index value i = 9 0 from the start 
to print out the index 3. The user key 9 
"" " 

# 24 to see the results and to write the code explain. 
DATA_LIST = # [] 
# for I in Range (10): 
# = {} Data 
# Data [ 'User'] = I 
# data_list.append (Data) 
# Print (DATA_LIST) 
"" " 
Cause: 
1 cycle of 10 1 Ci = 9, the index value starting from 0. 
2 data = {} loop remain empty, not assigned. 
3. after the end of the cycle, the i is assigned to the Data [ 'User'] 
4.data_list listing additional Data 
. 5 . nested list is printed dictionary [{ 'User':}. 9] 
"" "

  

Guess you like

Origin www.cnblogs.com/jinyan-huang/p/11373060.html