Contents of this section
Basic syntax and characteristics of functions
Parameters and local variables
return value
Nested function
Recursion
Anonymous function
Introduction to functional programming
Higher order function
Built-in function
main effect:
>>> a ={1,2,3,4}>>> b ={3,4,5,6}>>> a
{1,2,3,4}>>> type(a)<class'set'>>>> a.symmetric_difference(b){1,2,5,6}>>> b.symmetric_difference(a){1,2,5,6}>>>>>>>>> a.difference(b){1,2}>>> a.union(b){1,2,3,4,5,6}>>> a.issu
a.issubset( a.issuperset(>>> a.issubset(b)
False
Read-only list, only count, index 2 methods
Function: If some data does not want to be modified by others, it can be saved as a tuple, such as a list of ID cards
Why is the query speed faster? Because it is a hash type, what is a hash?
The hash algorithm maps arbitrary-length binary values to shorter fixed-length binary values, and this small binary value is called a hash value. The hash value is a unique and extremely compact numerical representation of a piece of data. If you hash a piece of plaintext and even change only one letter of that paragraph, subsequent hashes will produce different values. It is computationally impossible to find two different inputs hashed as the same value, so the hash value of the data can check the integrity of the data. Generally used for fast search and encryption algorithms
The dict will turn all the keys into a hash table, and then sort the table, so that when you use data[key] to look up a key in the data dictionary, python will hash the key into a number first, and then take this The number does not appear in the hash table. If there is, get the index of the key in the hash table, get the index and get the value of the memory address of the value corresponding to the key.
There is still no answer above. Why is it faster to find a data than a list, right? Haha, wait for my announcement in class.
Let me talk about python2
Let's talk about python3
Scenarios with more coding applications should be crawlers. Many websites on the Internet use very complicated coding formats. Although the overall trend has become utf-8, it is still very complicated now, so you need to perform various coding when crawling web pages. Conversion, but life is getting better, looking forward to a world without transcoding.
Finally, the coding is a piece of fucking shit, noboby likes it.
Now the boss asks you to write a monitoring program to monitor the system status of the server. When the usage of cpu\memory\disk and other indicators exceed the threshold, an email alert will be sent. You have emptied all the knowledge and wrote the following code
while True:
if cpu utilization>90%:
# Send email reminder
Connect to mailbox server
send email
Close the connection
if hard disk use space>90%:
# Send email reminder
Connect to mailbox server
send email
Close the connection
if memory usage>80%:
# Send email reminder
Connect to mailbox server
send email
Close the connection
The above code realizes the function, but even the neighbor Pharaoh also saw the clues. Pharaoh touched your son's face affectionately and said that you have too much repetitive code, and you have to rewrite a paragraph every time you call the police. The code for sending emails is too low. There are two problems with doing this:
Do you think what Lao Wang said is right. You don’t want to write repetitive code, but you don’t know how to do it. Lao Wang seems to have seen your mind. At this time, he picked up your son and said with a smile. It’s actually very simple. Extract the repeated code, put it in a public place, and give it a name. Whoever wants to use this code in the future can just call it by this name, as follows
def send mail(content)
# Send email reminder
Connect to mailbox server
send email
Close the connection
while True:
if cpu utilization>90%:send email('CPU alarm')if hard disk use space>90%:send email('Hard disk alarm')if memory usage>80%:send email('Memory alarm')
When you look at the code written by Lao Wang, it is magnificent and majestic. There is a restrained arrogance in the code. You think, Lao Wang is really unusual, and suddenly I became more interested in his background. Ask Lao Wang , How do you know these fancy games? The old Wang kissed your son, stroked the non-existent beard, and said faintly, "Old man, when I was young, I studied under the King of the Silver Horn in Jingxi Shahe." When you hear the words "The King of Silver Horn", I couldn’t help but shuddered and thought, “It’s really nb, no wonder the code is written so 6, this "Silver Horn King" was a well-known name in the rivers and lakes back then, but it’s a pity that he was excessively indulgent in the later period. He died in 2016, really. It's a pity that only his brother is left alone to guard the country that the two brothers had fought together. At this moment, you look at the figure of Lao Wang leaving, and it feels like your son is more and more like him. . .
The word function comes from mathematics, but the concept of "function" in programming is very different from the concept of function in mathematics. The specific difference, as we will talk about later, is that the function in programming also has many different names in English. . It is called subroutine in BASIC, procedure and function in Pascal, function only in C, and method in Java.
characteristic:
def sayhi():#Function name
print("Hello, I'm nobody!")sayhi() #Call functions
Can take parameters
# The following code
a,b =5,8
c = a**b
print(c)
# Change to write with function
def calc(x,y):
res = x**y
return res #Return function execution result
c =calc(a,b)# The result is assigned to the c variable
print(c)
**The formal parameter ** variable allocates the memory unit only when it is called, and immediately releases the allocated memory unit at the end of the call. Therefore, the formal parameters are only valid inside the function. After the function call ends and returns to the main calling function, the formal parameter variable can no longer be used
Actual parameters can be constants, variables, expressions, functions, etc. No matter what type of quantity the actual parameters are, they must have certain values when calling a function in order to transfer these values to the formal parameters. Therefore, you should use assignment, input and other methods in advance to obtain a certain value for the parameter
Default parameters
Look at the code below
def stu_register(name,age,country,course):print("----Registered student information------")print("Name:",name)print("age:",age)print("Country of Citizenship:",country)print("course:",course)stu_register("Wang Shan Pao",22,"CN","python_devops")stu_register("Zhang Jiaochun",21,"CN","linux")stu_register("Liu Laogen",25,"CN","linux")
I found that the country parameter is basically "CN", just like when we register a user on the website, if you don’t fill in information like nationality, the default will be China. This is achieved through the default parameters, turning country into the default parameter very simple
def stu_register(name,age,course,country="CN"):
In this way, this parameter is not specified when calling, and the default is CN. If specified, the value you specify is used.
In addition, you may have noticed that after turning country into the default parameter, I also moved its position to the back. Why?
Under normal circumstances, pass parameters to a function in order. You can use key parameters if you don't want to be in order. Just specify the parameter name. ***But remember that the key parameter must be placed after the positional parameter. ***
stu_register(age=22,name='alex',course="python",)
Non-fixed parameters
If your function is not sure how many parameters the user wants to pass in when it is defined, you can use non-fixed parameters
def stu_register(name,age,*args): # *args will turn multiple incoming parameters into a tuple form
print(name,age,args)stu_register("Alex",22)
# Output
# Alex 22() #Behind this()Is args,Just because there is no value,So empty
stu_register("Jack",32,"CN","Python")
# Output
# Jack 32('CN','Python')
There can also be a **kwargs
def stu_register(name,age,*args,**kwargs): # *kwargs will convert multiple incoming parameters into a dict form
print(name,age,args,kwargs)stu_register("Alex",22)
# Output
# Alex 22(){}#Behind this{}Kwargs,Just because there is no value,So empty
stu_register("Jack",32,"CN","Python",sex="Male",province="ShanDong")
# Output
# Jack 32('CN','Python'){'province':'ShanDong','sex':'Male'}
name ="Alex Li"
def change_name(name):print("before change:",name)
name ="Golden Horn,A man with Tesla"print("after change", name)change_name(name)print("Did you change the name outside??",name)
Output
before change: Alex Li
after change Golden Horn King,A man with Tesla
Did you change the name outside?? Alex Li
Global and local variables
The variables defined in the subprogram are called local variables, and the variables defined at the beginning of the program are called global variables.
The global variable scope is the entire program, and the local variable scope is the subroutine that defines the variable.
When the global variable has the same name as the local variable:
In the subroutine where local variables are defined, local variables work; global variables work elsewhere.
To get the execution result of the function, you can use the return statement to return the result
note:
What the above title means is that functions can also be used as functions? of course
name ="Alex"
def change_name():
name ="Alex2"
def change_name2():
name ="Alex3"print("Layer 3 printing",name)change_name2() #Call inner function
print("Layer 2 printing",name)change_name()print("Outermost printing",name)
At this point, what will be the effect of calling change_name2() in the outermost layer?
Yes, something went wrong, why?
The usage of nested functions will be better, but what use is it? The next class will be announced. . .
Inside the function, you can call other functions. If a function calls itself internally, the function is recursive.
def calc(n):print(n)ifint(n/2)==0:return n
returncalc(int(n/2))calc(10)
Output:
10521
Recursive characteristics:
Must have a clear end condition
Each time you enter a deeper recursion, the problem size should be reduced compared to the last recursion
The recursion efficiency is not high. Too many levels of recursion will cause the stack to overflow (in computers, function calls are implemented through the data structure of the stack. Whenever a function call is entered, the stack will add a stack frame. When the function returns, the stack will be reduced by one stack frame. Since the size of the stack is not infinite, too many recursive calls will cause the stack to overflow)
Practical application case of recursive function, binary search
data =[1,3,6,7,9,12,14,16,17,18,20,21,22,23,30,32,33,35]
def binary_search(dataset,find_num):print(dataset)iflen(dataset)>1:
mid =int(len(dataset)/2)if dataset[mid]== find_num: #find it
print("Find the number",dataset[mid])
elif dataset[mid]> find_num :#The number found is to the left of mid
print("\033[31;1m find the number in mid[%s]left\033[0m"% dataset[mid])returnbinary_search(dataset[0:mid], find_num)else:#The number you are looking for is to the right of mid
print("\033[32;1m find the number in mid[%s]Right\033[0m"% dataset[mid])returnbinary_search(dataset[mid+1:],find_num)else:if dataset[0]== find_num: #find it
print("Found the number",dataset[0])else:print("No points,The number looking for[%s]Not in the list"% find_num)binary_search(data,66)
Anonymous function means no need to specify the function explicitly
# This code
def calc(n):return n**n
print(calc(10))
# Change to anonymous function
calc = lambda n:n**n
print(calc(10))
You might say that it is not convenient to use this thing. . . . Haha, if it is used in this way, there is really no yarn improvement, but the anonymous function is mainly used in conjunction with other functions, as follows
res =map(lambda x:x**2,[1,5,7,4,8])for i in res:print(i)
Output
1
25
49
16
64
Function is a kind of encapsulation supported by the built-in Python. We can decompose complex tasks into simple tasks by dividing large pieces of code into functions and calling functions layer by layer. This decomposition can be called process-oriented Program design. Function is the basic unit of process-oriented programming.
**The term function in functional programming does not refer to a function in a computer (actually a Subroutine), but a function in exponential science, that is, the mapping of independent variables. That is to say, the value of a function is only determined by the value of the function parameter and does not depend on other states. For example, the sqrt(x) function calculates the square root of x. As long as x does not change, the value will remain the same no matter when it is called or how many times it is called. **
Python provides partial support for functional programming. Since Python allows the use of variables, Python is not a purely functional programming language.
Simply put, "functional programming" is a ["programming paradigm"] (http://en.wikipedia.org/wiki/Programming_paradigm) (programming paradigm), which is the methodology of how to write programs.
The main idea is to write the calculation process as a series of nested function calls as much as possible. For example, now there is such a mathematical expression:
(1 + 2) * 3 - 4
In traditional procedural programming, you might write:
var a = 1 + 2;
var b = a * 3;
var c = b - 4;
Functional programming requires the use of functions, we can define the operation process as different functions, and then write the following:
var result = subtract(multiply(add(1,2), 3), 4);
The following evolution of this code can become like this
add(1,2).multiply(3).subtract(4)
This is basically the expression of natural language. Look at the following code again, everyone should be able to understand its meaning at a glance:
merge([1,2],[3,4]).sort().search("2")
Therefore, the code of functional programming is easier to understand.
If you want to learn functional programming well, don't play py, play Erlang, Haskell, well, I can only do so much. . .
Variables can point to functions, and the parameters of a function can receive variables, then one function can receive another function as a parameter, and this kind of function is called a higher-order function.
def add(x,y,f):returnf(x)+f(y)
res =add(3,-6,abs)print(res)
Detailed built-in parameters https://docs.python.org/3/library/functions.html?highlight=built#ascii
# compile
f =open("Function recursion.py")
data =compile(f.read(),'','exec')exec(data)
# print
msg ="Back to the original starting point"
f =open("tofile","w")print(msg,"Your green face in memory",sep="|",end="",file=f)
# # slice
# a =range(20)
# pattern =slice(3,8,2)
# for i in a[pattern]: #Equal to a[3:8:2]
# print(i)
#
#
# memoryview
# usage:
#>>> memoryview(b'abcd')
#< memory at 0x104069648>
# When slicing and assigning data, there is no need to copy the original list data again, and the original data memory can be directly mapped.
import time
for n in(100000,200000,300000,400000):
data = b'x'*n
start = time.time()
b = data
while b:
b = b[1:]print('bytes', n, time.time()-start)for n in(100000,200000,300000,400000):
data = b'x'*n
start = time.time()
b =memoryview(data)while b:
b = b[1:]print('memoryview', n, time.time()-start)
Several built-in method usage reminders
There are the following employee information tables
Of course, you can express this table like this in File Storage
1, Alex Li,22,13651054608,IT,2013-04-01
Now it is necessary to add, delete, modify and check the employee information file
Fuzzy query can be performed, and the syntax supports at least the following 3 types:
select name,age from staff_table where age > 22
select * from staff_table where dept = "IT"
select * from staff_table where enroll_date like "2013"
After printing the information found, the number of pieces found will be displayed at the end
Create new employee records, use phone as the unique key, staff_id needs to be incremented
Can delete the specified employee information record, enter the employee id to delete
The employee information can be modified, the syntax is as follows:
UPDATE staff_table SET dept="Market" WHERE where dept = "IT"
** Note: For the above requirements, to make full use of functions, please do your best to reduce duplication of code! **
Recommended Posts