Calculator:
NUMX = "3 * 5/-2-(8*3/(20+3/2-5) + 4/(3-2) *-3)"
A collection of the following values [11,22,33,44,55,66,77,88,99,90 ...], saving all values greater than 66 to the first key in the dictionary, will be less than 66Value is saved to the value of the second key. That is: {'K1': Greater than 66,'K2': Less than 66}v1= [11,22,33,44,55,66,77,88,99,90]dicnum= {"K1":[],"K2":[]} forIinchv1:ifint (i) > 66: dicnum["K1"].append (i)Else: dicnum["K2"].append (i)Printdicnum["K1"]Printdicnum["K2"]PrintDicnum
v2 = [11,22,33,44,55,66,77,88,99,90]dicnum= {} forIinchV2:ifint (i) > 66: if "K1" inchdicnum:dicnum["K1"].append (i)Else: dicnum["K1"] =[i]Else: if "K2" inchdicnum:dicnum["K2"].append (i)Else: dicnum["K2"] =[i]Printdicnum["K1"]Printdicnum["K2"]Collection
- Default dictionary (sets the default format for dictionaries) Defaultdic
- Ordered dictionary Orderdic
- Counter counter
- Queue-bidirectional queue
# default dictionary mydict = collections.defaultdict (list) mydict["K1"].append (1 )print mydict["K1"]
# ordered dictionary orderdict
An ordered dictionary maintains a list in a dictionary.
# Counter Counter = [11,22,33,44,5544,3322,111= (11,22,33,44,222,5555,2222" 2343252632642362hfidsafhsifoesagoewagewaglsanldsa;lsf;sahflaf;saf;la"= collections. Counter (str1)print c2
Queue
- Queue-one-way Queue FIFO Magazine
Import= queue.queue () q.put (2) q.put (3) q.put (23°c)print Q.get ()
Generator
Practice:<br> like the following tables: [6, 13, 22, one] please follow the rules calculation:Compare the values of the large value on the right side, that is: [All, 6,] 0>22 and 6 Compare, put the big value on the right side, namely: [6, 22, one, one]and 99 compare, put the big value on the right side, namely: [Five, 6, one, one]99 and 42 Compare, put the big value in On the right, that is: [6,13, one, and 6 comparison, the big value on the right side, that is: [6, five, one, one, ...]
Li = [6, p, p, p] for in range (Len (LI)-1): A in range ( M+1, Len (LI)): if li[m]> li[n]: = Li[n] = Li[m] = tempprint li
Function
Code blocks, broken down by function,
The difference between functional programming and process-oriented programming:
- Function: A function code is encapsulated in a function, it will not need to be repeated later, only the function can be called
- Object-oriented: classify and encapsulate functions to make development "faster, better and stronger ..."
The definition of a function has the following main points:
- def: A keyword that represents a function
- Function name: the names of functions, which are called later by function name
- Function Body: A series of logical calculations in a function, such as sending a message, calculating the maximum number in [11,22,38,888,2], etc...
- Parameters: Providing data for the function body
- Return value: Once the function has finished executing, it can return data to the caller.
- Built-in functions
- VARs () = = All variables of the current module
__DOC__ Note this document
__name__ the file name, as the current executable, content is __main__
__file__ file path
- All () returns false as long as it is empty
- Any () if one is true, returns True
- Reload () Reload reference file
- def defines a keyword for a function
- Name of function
- function declaration
- function parameters
Formal parameters, parameter parameters, real parameter dynamic parameters
def func (*args): print= [11,22,33,444,332,112]func (*li)
- Accept multiple parameters
- Internal Auto-structuring tuples
- When called, add * to avoid automatically constructing tuples
Dynamic parameters to invoke the dictionary, you need to use * *
def func (* *args) :print= {"echo" : 123,"rep": 234}func (**dic)
Default parameters
Do not use default values
defEmail (ARG):PrintArgif __name__=="__main__": CPU= 100Ram= 50Disk= 500 forIinchRange (1): ifCPU > 90: Alert="CPU Problems"Email (alert)ifDisk > 90: Alert="disk is having problems"Email (alert)ifRam > 80: Alert="Memory Problems"Email (alert)
File operations
obj = open ("index.txt","R") Obj.seek (10 )#定位指针
Print Obj.tell () #输出指针位置
Print Obj.read () #输出内容
print Obj.tell ()
#输出指针位置
R Read
W Write
A append
&+
r+ Reading and writing
w+ Class W
A + Class A
The mode of opening the file is:
- R, read-only mode (default).
- W, write-only mode. "unreadable; not exist; create; delete content;"
- A, append mode. "Readable; not exist" create; "only append content;"
"+" means you can read and write a file at the same time
- r+, can read and write files. "readable; writable; can be added"
- w+, meaningless
- A +, with a
"U" means that the \ r \ n \ r \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ r or r+ mode can be converted automatically when reading
"B" means processing binary files (e.g. FTP send upload ISO image file, Linux can be ignored, Windows process binary file should be labeled)
2015-11-08 Third Day