function 11/1
Function: Refers to the collection of a set of statements through a name (function name) to encapsulate, to execute this function, just call its function name is okay.
Def sayhi (): Name of function
Print ("Hello, I ' m Hequan")
Sayhi () #调用
return value of the function
Def calc (x, y):
res = X**y
return res #返回函数执行结果
Default returns None
11/2
Parameters
def mail ( formal parameter ):
ret = mail ( actual parameter )
IF RET:
Print ("Send Success")
Def Mail (a1,a2=11): The default parameter must be placed last
Def Mail (A1,A2):
Mail (a2=123,a1=321) Specifies parameters
Dynamic parameters
Def show (*arg): # * Convert parameters to Cheng Yuanju
Print (Agr,type (ARG))
Show (11,22,33,44)
Def show (**arg): # # * * Convert parameters to Dictionaries
Print (arg, type (ARG))
Show (n1=11, n2=22, n3=33, n4=44)
Def show (*kwargs,**arg):
Print (arg, type (ARG))
Print (*kwargs,type (Kwargs))
Show (11,22,33,44,n1=11, n2=22, n3=33, n4=44)
{' N3 ':, ' N4 ':, ' N1 ': One, ' N2 ': ' <class ' dict ' >
<class ' tuple ' >
Show (*A,**B)
11/3
S1 = "{0} is {1}" {Name}{acter}
xx = S1.format (' Hequan ', ' 2b ')
Print (XX)
Hequan is 2b
# lambda expression simple function
he = Lambda a:a+1
Print (He (1))
Built-in functions
Open function file processing
R Read Only W write a append
R+ can read and write w+ write A + a
u automatically convert to \ n
b means processing a binary file
Close File
Tell () #查看当前指针位置
Seek () # sets the pointer position
Read
Truncate () The byte before the pointer position, leaving only the preceding, and saving to the file.
Write
Formal parameters
Actual parameters
Default parameters
Key parameters are behind the position parameter
List of non-fixed parameter *args **kwargs tuples
Local variables
Global variables
return value--return
The function stops executing and returns the result as soon as it encounters a return statement, and return marks the end of the function.
If there is no return, the return value is none.
Nested functions
Recursive
anonymous functions
Map can be used to perform the same action on each element of a traversal structure
map(lambda x: x**2, [1, 2, 3, 4]) # [1, 4, 9, 16]
Map (lambda x, y:x + y, [1, 2, 3], [5, 6, 7]) # [6, 8, ten]
Reduce is a sequential operation of two input parameters on the elements of the ergodic structure, and each result is saved as the next
The first input parameter of the operation has not yet traversed the element as a second input parameter.
The result is a string of values that can be traversed, reduced to an object:
reduce(lambda x, y: x + y, [1, 2, 3, 4]) # ((1+2)+3)+4=10
Filter filters a traversal structure based on conditions
filter(lambda x: x % 2, [1, 2, 3, 4, 5]) # 筛选奇数,[1, 3, 5]
List generation
[X**2 for x in [1,2,3,4,5]]
The ZIP function can associate multiple lists.
[Sum (x) for x in Zip ([1, 2, 3], [5, 6, 7])] # [6, 8, 10]
Iter_odd = (x for x in [1, 2, 3, 4, 5] if x% 2)
Print (Type (iter_odd)) # <type ' Generator ' >
Square_dict = {x:x**2 for x in range (5)} # {0:0, 1:1, 2:4, 3:9, 4:16}
File operations
The first argument to open () is the file name, and the second parameter is the pattern. There are typically four types of file patterns, read (R), write (W), append (a), and read and write (r+). If you want to read by binary data, use the file mode with B (WB, r+b ... )。
With open (' Xx.txt ', ' R ') as F:
lines = F.readlines ()
For line in lines:
Name, age = Line.rstrip (). Split (', ')
Print (' {} is {} years old '. Format (name,age))
Import Pickle
lines = [
"I ' m like a dog chasing cars.",
"I wouldn ' t know-do if I caught one ...",
"I ' d just do things."
]
With open (' Lines.pkl ', ' WB ') as F:
Pickle.dump (LINES,F)
With open (' lines.pkl ', ' RB ') as F:
Lines_back = Pickle.load (f)
Print (Lines_back)
This article is from the "what-all" blog, please be sure to keep this source http://hequan.blog.51cto.com/5701886/1886270
Python notes three functions