Basics: https: //www.cnblogs.com/alex3714/articles/5717620.html
a: List:
format: variable = [ "value1", "value2", "value3", .....]
View:
variable [#] --- take single
variable [#: #] --- # to # value -1 remove the
variable [#: - 1] --- # withdrawn to the last
variable [- #: - #] - - countdown display [-2, -1], [-. 3, -2] ...
variable [: #] --- 0 and -1 are omitted
operation:
variable.append ( "value") --- additional
variable.insert (#, "value") --- insert
variable [#] --- change
variable.remove ( "value") || del variable [#] variable.pop (#) ---- delete
variable .index ( "value") --- get subscript
variable.count ( "value") --- statistical value
variable.clear () --- empty variable
variable.reverse () ---- inverted list
variable.sort () --- sorting, special symbols> Digital> uppercase> lowercase
variable.extend (variable1) --- the values to be inside v1 V
variable variable1 = .copy () --- V1 to V *** this copy is shallow copy
variable = variable1 --- copy, belonging to a shared memory address
*** added:
list contains a list in which, when it will appear in the copy each use there a way to copy the content, which is related to memory address storage
format: varaible [#] [#]
using the depth cOPY:
Import copy
variable = copy.deepcopy (variable1)
list of print:
for i in variable: --- full Print
print (I)
for I in variable [0: -1: 2]: --- printing step
two: tuple: that is, after the list, but can not change the value created
format: vraiable = ( "value", "value1", "value2")
Three: string manipulation:
variable.capitalize () ------ capitalized
variable.casefold () -------- all uppercase becomes lowercase
variable.center (10, "-") --- - output 'value ------- -------'
variable.count ( 'value') ---- statistical value occurrences
variable.encode () ---------- the format string encoded into bytes
variable.endswith ( "Li") ---- Li determines whether the end of the string
variable.expandtabs (#) ---- the \ t # spaces converted
variable.find ( 'value ') ----- lookup value, returns its index to find, can not find returns -1, such as print ([name.find ( "name" ):]) --- shows the string can slice
variable.format ( V1 = "value", V2 = "value2", .....) ---- to format: MSG = "My name {name} IS, IS and Age Age {}"
variable.format_map ( 'Vl ':' value ',' V2 ':' value2 ', .....) ---- for formatting (but using the dictionary mode): msg = "my name is {name}, and age is {age} "
variable.isalnum () -------- detects whether the string of letters and numbers, return True
variable.isalpha () -------- detecting plain English characters, returns to false
variable.isdecimal () to check whether the string contains only ------ decimal character *** Note: The definition of a decimal string requiring only the addition 'u' to the prefix string before.
variable.isdigit () --- whether it is an integer
variable.isidentifier () --- judge is not a valid identifier (legal variable name)
variable.islower ---------- is not a lowercase
variable.upper () ---- is not capitalized
variable.isnumeric () ----- is not only a digital
variable.isspace () ---- is not a space
variable.istitle () ---- detect character string all words are spelled the first letter is an uppercase, lowercase letters and other
variable.title () ------ beginning of each string uppercase
variable.isprintable () ---- is not able to print, tty, drive format can not be printed
variable.join ( '', join [ ' 1', '2', '3']) --- *** parameter
variable.ljust (#, "*") --- enough for # Right then filled *
variable.rjust (#, '-'
variable.upper () -------- change the uppercase lowercase
variable.lstrip () ------- characters \ n and spaces foremost removed
variable.strip () ------- - on both sides of the character \ n and remove the spaces
variable.rstrip () ------- the final character \ n and spaces removed
variable.maketrans ( "V1", "V2 ") ---- maps will V1 is V2, symmetric encryption, e.g. str.maketrans = P ( "ABC", "123")
variable.translate (str.maketrans) ---- encryption, print ( "acdbf" .translate ( p)) results: 1cd2f
variable .replace ( "old", "new ", #) --- replacing old as new, as the number # of
variable.rfind ( 'value') --- return to the last position of occurrence of the string (from left to right query)
variable.split ( "separator", #) ----- sliced by specifying the string delimiter is a delimiter separator, # is the number of the partition, similar to the cut command;
variable.startswith ( "value", #, #) ------ used to check if a string is specified at the beginning of the substring, #, # represents the beginning and end of the
variable.swapcase () --------------- change uppercase to lowercase, uppercase to lowercase
variable.zfill (#) --- 0s,
four: Dictionary
Characteristics: dict is disordered, key must be unique,
Format: variable = { "key": "value"}
changes exist, there is added: variable [ "key"] = "value"
Delete:
del variable || variable del [ "Key"]
variable.pop ( "Key")
variable.popitem () --- randomly deleted
View:
variable [ "Key"] ---- unsafe acquisition method
print (variable.get ( "key ")) --- secure method for obtaining
judgment:
Print (" Key "in variable)
Print:
variable.values () ---- show all value
variable.key () ----- displays all the Key
variable .setdefault ( "key1", { " key2": [value]}) ---- go to the dictionary to find, if taken to return, if it fail to create a new value
variable.update () --- points and two dictionaries, there are key updates, no key is created
variable.items () --- a dictionary into a string
variable.fromkeys ( "[key1, key2, ...]", "value") --- initialize a new dictionary
may be multi-level nested: [key1, key2, ...] , [{ "[key1-1 , key1-2, ...] "," value "}," value "]
*** Note: data creation nested using the same memory, a data and
multi-level nested Key:
variable = { "key1-1":
{ "key2-1": [ "value"]},
{ "key2-2": [ "value"]}
},
{
"key1-2":
{ "key2-3": [ "value"]}
}
change:
variable [ "key1"] [ "key2"] [#] = "value"
Dictionary loop:
for I in variable: --- efficient
Print (I, varaible [I])
for K, V in variable.item () - inefficient
print (k, v)
项目1:
product_list=[
('phone',5000),
('book',4000),
('bike',3000),
('watch',2000),
('coffee',1000)
]
shopping_list = []
salary=input("please input a number(quit):")
#judge string or number
if salary.isdigit():
salary=int(salary) #change string to number
while True:
for i in product_list:
print(product_list.index(i),i)
#enumerate(variable) == variable.index()
user_choice = input("choose produce('quit')")
if user_choice.isdigit():
user_choice = int(user_choice)
if user_choice < len(product_list) and user_choice > 0 :
p_item = product_list[user_choice]
if p_item[1] <= salary:
shopping_list.append(p_item)
salary -= p_item[1]
print("ADDED %s into shopping,your current balance is %s" %(p_item,salary))
else:
print("money is no enough")
else:
print("goods is not exist")
elif user_choice == "quit":
print("----shopping list------")
for i in shopping_list:
print(i)
print("you have money is: ",salary)
exit()
else:
print("wrong")
项目2:
date1={
"key1":{
"key2":{
"key3":{
"1":["value1-0","value2-0"],
"2":["value1-1","value2-1"],
"3":["value1-2","value2-2"]
}
}
}
}
while True:
for i in date1:
print(i)
choose1=input("go to:")
if choose1 in date1:
while True:
for i in date1[choose1]:
print("\t",i)
choose2=input("go to:")
if i in date1[choose1]:
while True:
for i in date1[choose1][choose2]:
print("\t\t",i)
choose3=input("go to:")
if choose3 in date1[choose1][choose2]:
while True:
for i in date1[choose1][choose2][choose3]:
print(date1[choose1][choose2][choose3][i])
choose5=input("input:")
choose5 == IF "End":
Print ( "Exit")
BREAK
BREAK
BREAK
BREAK
Additional:
can be achieved with the statement:
early to declare a variable: end_method = True
exit when change: end_method = False
placed at the beginning of the cycle