function
1, Object-oriented Programming 2, process-oriented programming # There is no return called Process 3, functional programming # return returned is called a function
how to pass a parameter
1, can define the way of the default parameters, location calls the way, and the location of the
def Run (x=1,y=2) Print
2, the keyword call, regardless of location, will output 2,1 def test1 (x, y ): Print (x) Print (y) test1 (y=1,x=2)
PS: Key parameters cannot be in the middle
3, parameter group parameters (single *) accept the n positional parameters, cannot accept the keyword parameters, will eventually convert Cheng Yuanju def Test1 (*args): print (Args,type (args)) for in (args) : print (data,type (data)) test1 (1,3,4,5) def test1 (x, *args): Print (x) print (args) test1 (1,2,3,4,5,6 )
4, Parameter group parameters (double *) received is a dictionary, the key must be passed in the Key=value format is the dictionary format
* *Kwargs parameter group must place def test1 (* *Kwargs) :print (Kwargs) test1 (Name= " Alex ", age=", sex="nan")
Little Practice
defTest1 (x,y,z=1,*args,**Kwargs):Print(x)Print(y)Print(z)Print(args)Print(kwargs["name"]) test1 (1,2,3,4,5,6,name="Alex", age=" -", sex="nan") Output12Alex (4, 5, 6){'name':'Alex','Sex':'nan',' Age':' -'}
local variables and global variablesA variable that takes effect inside a function is called a global variable, and a variable that takes effect globally is called a global variable.
third, recursive function
features1. There must be a clear end condition 2. Each entry to the next recursive layer should be reduced by 3 compared to the previous layer. Recursive hierarchy is not high, easy to cause memory overflow
Iv. Higher-order functionsA variable can point to a function, which can receive a variable, and a function can receive another function as a parameter, a function called a higher order function.
def Add (x,y,f): return f (x) + f (y) = Add (3,-6, ABS)print(res)
eval can turn str into a dictionary
For example, in python3 with the input function, the incoming is the STR type, so that can be used User_input=eval (uesr_input) to turn into a dictionary again processing very well case{'Bakend':'www.old.org','Record':{'Server':'100.1.7.9','Weight': 20,'Maxconn': 30}}
Practice how to replace the modified character in a file because it is appended to the end of the file with a + r+, when it is necessary to modify the contents of a file only 2 methods 1, write to the memory of the modification, the modification is completed with Truncat empty the file and then write back 2, directly w+ create another file, Write something to another file, and then Os.rename os.remove Replace the file Case1 in-memory modification
def run ():
f = open ("Tmp1.txt", "A +") #这里F打开文件后指针就已经去了末尾
F.seek (0) #将指针指会开始位置, otherwise f.read can't read.
m = F.read ()
Print (m)
F.seek (0) #读完后指针又去了末尾, put him back in the starting position.
F.truncate (0) #截断文件, 0 indicates truncation from start position
s = M.replace ("Test\nabc", "Xxxxxxxxxt") #这里替换一个字符串
F.write (s) #写入字符串
Run ()
The content of Tmp1.txt is
Test
Abc
123
A + read-write way to open the file, if the file does not exist, create a file
the method of writing refers to appending with open ("Test.txt","A +") as F:f.write ("123\n")
The mode of reading refers to the with open ("Test.txt","A +") as F:f.seek (0) d=F.read () f.write ("123\n")Print(d)
The biggest difference between r+ and A + is that
r+ If you read and write first, the end is appended, if the direct write is overwrite
read first and write with open ("test.in"'r+') as F: F.readline () f.write ("addition") hello1ok2byebye3addition
The way of r+
R+ Way is read-write mode open
Read
With open ("Test.txt", "r+") as F:
Print (F.tell ()) #这里输出的位置是0
Print (F.read ()) #因为是0开始的所以这里会输出所有内容
Print (F.tell ()) #这里输出的是文件末尾
The way to write is to overwrite the original content, starting from 0 to write
With open ("Test2.txt", "r+") as F:
Print (F.tell ()) #还是从0开始
F.write ("aaaaaaaass\n") #但是文件写入的方式是覆盖原有内容
Tell () How to judge a pointer
This assumes that the contents of the Test2.txt file are
123
With open ("test2.txt","r+") as F: Print (F.tell ()) d=f.read () print (len (d), F.tell ())
The output is as follows, that is, for this is the end of the null character, assuming you have a space to enter the count, the last Len length +1 is the position of your pointer
05 7
Python_ Function _ File