Python basics 5

Contents of this section

  1. Basic syntax and characteristics of functions

  2. Parameters and local variables

  3. return value

Nested function

  1. Recursion

  2. Anonymous function

  3. Introduction to functional programming

  4. Higher order function

  5. Built-in function

Review the past and know the new

1. set####

main effect:

  1. Deduplication
  2. Relationship test, intersection \ difference set \ union \ reverse (symmetric) difference set
>>> 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

2. Tuple

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.

4. Character Encoding####

Let me talk about python2

  1. The default encoding in py2 is ascii
  2. The encoding declaration at the beginning of the file tells the program that explains the code in what encoding format to read this code into the memory, because in the memory, this code is actually stored in bytes binary format, but even if it is a binary stream, It can also be converted into a binary stream according to different encoding formats, do you understand?
  3. If you declare #_coding:utf-8 in the header of the file, you can write Chinese. If you don’t declare it, if python press ascii when processing this code, it will obviously make an error. After adding this declaration, the code inside It's all in utf-8 format
  4. In the case of #_coding:utf-8, you are declaring the variable. If you write name=u "大健康", then this character is in unicode format, without this u, then the string you declare is utf -8 format
  5. How to convert utf-8 to gbk, utf8 first decode to unicode, then encode to gbk

Let's talk about python3

  1. The default file encoding in py3 is utf-8, so you can write Chinese directly, and there is no need to declare the encoding in the file header, which is beautiful.
  2. The variable you declare is unicode encoding by default, not utf-8, because the default is unicode (unlike in py2, you have to add u before the variable if you want to declare unicode directly), at this time you want to convert to gbk If it is, you can directly your_str.encode("gbk")
  3. But in py3, when you are in your_str.encode("gbk"), it feels like you have added an action, that is, the encoded data becomes bytes, and I wipe it. What is the situation, because in py3, str And bytes have made a clear distinction. You can understand that bytes is a binary stream. You would say that what I see is not a binary system like 010101. That's because python is in order to allow you to manipulate data. The memory level helps you make a layer of encapsulation, otherwise it will let you directly see a bunch of binary bases. Can you see which character corresponds to which binary base? what? Convert it yourself, come on, you even have to work hard to add and subtract numbers with more than 2 digits, so you don't have to worry about it.
  4. Then you say, there seems to be bytes in py2, yes, but bytes in py2 are just aliases for str (str in python2 is bytes, str in py3 is unicode), and it doesn’t show you like py3. There is an extra layer of encapsulation, but it is actually encapsulated inside. Let’s put it this way, whether it’s 2 or 3, from the hard disk to the memory, the data format is 010101 binary to -->b'\xe4\xbd\xa0\xe5\xa5\xbd' bytes type --->according to the specified encoding Text you can understand

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.

1. Basic syntax and features of functions###

Background feed####

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:

  1. Too much code repetition, consistent copy and paste does not meet the temperament of high-end programmers
  2. If you need to modify the code for sending emails in the future, such as adding the group sending function, then you need to modify all the places where this code is used

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. . .

What is a function?

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.

Definition: Function refers to encapsulating a set of statements with a name (function name). To execute this function, you only need to call its function name

characteristic:

  1. Reduce repetitive code
  2. Make the program scalable
  3. Make the program easy to maintain

Syntax definition####

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)

2. Function parameters and local variables###

**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?

key parameter####

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'}

Local variables####

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.

3. return value###

To get the execution result of the function, you can use the return statement to return the result

note:

  1. As long as the function encounters the return statement during the execution process, it will stop execution and return the result. So can also be understood as the return statement representing the end of the function
  2. If return is not specified in the function, the return value of this function is None

Forcibly insert knowledge points: nested functions###

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. . .

4. Recursion

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:

  1. Must have a clear end condition

  2. Each time you enter a deeper recursion, the problem size should be reduced compared to the last recursion

  3. 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)

5. Anonymous function###

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

6. Introduction to Functional Programming###

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.

  1. Definition

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. . .

7. Higher-order functions###

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)

8. Built-in parameters###

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

Homework in this section###

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

  1. Fuzzy query can be performed, and the syntax supports at least the following 3 types:

  2. select name,age from staff_table where age > 22

  3. select * from staff_table where dept = "IT"

  4. select * from staff_table where enroll_date like "2013"

  5. After printing the information found, the number of pieces found will be displayed at the end

  6. Create new employee records, use phone as the unique key, staff_id needs to be incremented

  7. Can delete the specified employee information record, enter the employee id to delete

  8. The employee information can be modified, the syntax is as follows:

  9. 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

Python basics
Python basics 2
Python basics
Python basics 3
Python basics 4
Python basics 5
Python object-oriented basics
Python custom function basics
Basics of Python syntax
Python multithreading
Python CookBook
Python3 dictionary
Python3 module
python (you-get)
Python string
Python descriptor
Python exec
Python notes
Python3 tuple
CentOS + Python3.6+
Python advanced (1)
Python decorator
Python IO
Python multithreading
Python toolchain
Python3 list
Python overview
python introduction
Python analytic
07. Python3 functions
Python multitasking-threads
Python functions
python sys.stdout
python operator
Python entry-3
Centos 7.5 python3.6
Python string
python queue Queue
Learn the basics of python interactive mode
Two days of learning the basics of Python
Centos6 install Python2.7.13
Python answers questions
Python basic syntax (1)
Python exit loop
Ubuntu16 upgrade Python3
Centos7 install Python 3.6.
ubuntu18.04 install python2
Python classic algorithm
Relearn ubuntu --python3
Python string manipulation
Python study notes (1)
python learning route
Python3 basic syntax
Python review one
Functions in python
Python learning-variable types
CentOS install Python 3.6
7 features of Python3.9
Python file operation
ubuntu12.04 install python3
Python design patterns