“This is the third day of my participation in the August More Text Challenge.

preface

I have followed my course for some time, I believe you will have a question? How much longer do you have to finish the course? Now this stage, be completed python zero introductory one-third, and at this stage can insist on down, that you all have a want to change their impulsive, yes, learning programming itself is very need patience, but insist on down, later will become beautiful, good, is a digression to above, So let’s start today’s lesson!

What is a function

A function is a piece of code that performs a specific task. By defining a piece of code as a function and giving it a function name, the program can call the code many times as needed.

It doesn’t matter if you don’t understand the meaning of the above expression, LET me give you an example: Opens at every time we WeChat circle of friends, isn’t it automatically refresh the contents of a circle of friends, friends will WeChat update dynamically, presented to the front of you, then the content of “friends” is a call to the database data, encapsulate the code inside the function, so the content of the circle of friends “auto-refresh” movement is called the function name, Then each time to perform the update operation, wechat will call the function name, and you can perform the dynamic code update to the moments of friends.

After seeing the above example, do you have a question? What if I had to rewrite the code every time I updated my moments? In fact, it is not necessary, because if the same code, only need to write the code once, when to update, just use the function name to call it.

A function must be defined before it can be used. The syntax for defining a function is as follows:

defThe function name (The parameter list) :// A function consisting of zero to more executable statements [return[Return value]]Copy the code

Note the following: 1. Function name. From the point of view of syntax, function name should be a valid identifier. For program readability, function names should consist of one or more meaningful words, each in lower case, separated by a underlined line

Parameter list is used to define the parameters that the function can receive. The parameter list consists of multiple parameter names separated by commas (,). Once the parameter list is specified in the function definition, the corresponding parameter values must be passed in when the function is called.

Of a tangible parameter:

def my_max(x, y) :
	z = x if x > y else y
	return z


a = my_max(8.6)
print(a) Output results:8
Copy the code

Having no parameters, for example:

def my_name() :
	print("Your name is Sun Wukong") my_name() Output result: Your name sun WukongCopy the code

Note: Using a return statement in a function body can explicitly return a value. The value returned by a return statement can be either a valued variable or an expression.

Document functions (this can help you if you’re working on a project that someone else is developing and how to quickly figure out which methods work)

We can use the help() function to view the help information for a given function:

def my_max(x, y) :
	The function my_max(x, y) returns the larger number between x and y.

	z = x if x > y else y
	return z

help(my_max)
printHelp on function my_max (my_max.__doc__inModule __main__: my_max(x, y) Returns the larger number between two arguments my_max(x, y) returns the larger number between two argumentsCopy the code

Multiple return values

If a program requires multiple return values, it can either wrap the values in a list and return them, or it can return multiple values directly. If a Python function returns multiple values directly, Python automatically wraps the multiple return values into tuples. For example:

def sum_and_avg(list) :
	sum = 0
	count = 0
	for e in list:
		if isinstance(e, int) or isinstance(e, float):
			count += 1
			sum += e
	return sum.sum / count



mylist = [20.15.2.8.'a'.35.5.9, -1.8]
tp =sum_and_avg(mylist)
printOutput result: (76.9.12.816666666666668)
Copy the code

Recursive function (this stage, just know)

Calling itself from within a function is called function recursion, and function recursion involves an implicit loop that repeats a piece of code over and over again without loop control. Ex. :

def fn(n) :
	if n == 0 :
	   return 1
	elif n == 1 :
	   return 4
	else :
		return 2 * fn(n - 1) + fn(n - 2) So this is the recursion

print("The result of fn(10) is:", fn(10))
Copy the code

Best solution: for fn(10), equal to 2 * FN (9) + fn(8), where fn(9) is equal to 2FN (8) + fn(7)… And so on and so forth, eventually you get fn(2) is equal to 2fn(1) + fn(0), so that fn(2) is computable, so that the recursive implicit loop has an end, and then you go all the way back, and you get fn(10).

Look closely at the recursive process above. When a function calls itself repeatedly, there must be a time when the return value of the function is certain, that is, it does not call itself again. Otherwise, the recursion becomes infinite recursion, similar to an infinite loop, so the most important rule when defining a recursive function is that the recursion must go in a given direction.

After-class supervision

Recently, some of my classmates chatted privately in the background and said that they were ready to study every day, but when they went to the computer desk, they couldn’t lift their enthusiasm. What should they do? For students in this situation, I decided to spend some time to supervise and tutor your study every day. If you need it, please scan the picture below and click “Contact author” to register.

conclusion

This is a bit of a logical lesson, but you’ll understand it as I go along. Ok, that’s all for today, and next time we’ll talk about parameters of functions, so stay tuned.