Zero Basic Learning Python: Intensive Introduction to Functions

Preface

The text and pictures in this article are from the Internet and are for learning and communication purposes only, and do not have any commercial use. If you have any questions, please contact us for processing.

PS: If you need Python learning materials, you can click on the link below to get it by yourself

Python free learning materials, codes and exchange answers click to join


1. Positional parameters and keyword parameters

 

Although this method is very common, one disadvantage of positional parameters is that you must memorize the meaning of each positional parameter. Mistaking the last parameter as the first parameter when calling the function f() will result in completely different results:

 

Keyword parameter

In order to avoid the confusion caused by positional parameters, you can specify the name of the corresponding parameter when calling the parameter, or even call it in a different order from the function definition:

 

 

You can also mix positional parameters and keyword parameters. First, instantiate the parameters, and then use keyword parameters for the parameters:

 


If there are two parameter forms at the same time, the first thing that should be considered is the positional parameter.

Default parameters

When the caller does not provide the corresponding parameter value, you can specify the default parameter value. This common-sounding feature is actually very useful. Take the previous example as an example:

 

2. Variable parameters

*args

Positional parameters (understand)
it is not clear how many parameters are passed in

 

If you call a function without parameters, nothing will be returned:

 

All parameters passed to the function will be returned as a tuple as output:

 

 

This technique is very useful for writing functions that accept a variable number of parameters like print(). If your function also has limited positional parameters, *args will collect the remaining parameters:

 


When using *, there is no need to call the tuple parameter args, but this is also a common practice in Python.

** kwargs

Keyword parameters
Use two asterisks to collect parameters into a dictionary. The name of the parameter is the key of the dictionary, and the value of the corresponding parameter is the value of the dictionary. The following example defines the function print_kwargs(), and then prints out its keyword parameters:

 

Case:

Case: Assuming that print in python can't be used by passing multiple parameters at once, let us implement the function that print can pass multiple parameters.

 

 

Requirement: Write a changed_print function by yourself to realize the original print function.
Example: the function of the original function

 

 

3. First-class citizen function

In Python, functions are first-class objects. Programming language theorists define "first-class objects" as program entities that meet the following conditions:

  • Created at runtime
  • Can be assigned to variables or elements in data structures
  • Can be passed as a parameter to a function
  • Can be used as the return result of a function

In Python, integers, strings, and dictionaries are all first-class objects-nothing special. The following content will focus on the impact and practical applications of using functions as objects.

For example, if I want to change the name of the previous function, use the new function name to call the old function


Pass functions as parameters.
Case requirements f(1) + f(2) + f(3) + f(4) + f(5)


Let: find f(1) + f(2) + f(3) + f(4) + f(5) + ... +f(N)


The scope of the global variable is the entire program, and the scope of the local variable is the subroutine that defines the variable.

 

@expand

Function recursion (difficulty)

Function recursion (difficulty)
The programming skill of function calling itself is called recursion.
Features

  • Call itself inside the function
  • Other functions can be called inside the function, of course, you can also call yourself inside the function

Code characteristics

  1. The code inside the function is the same, but the processing results are different for different parameters
  2. When the parameters meet a condition, the function is no longer executed
  • This is very important and is usually called a recursive exit, otherwise an endless loop will occur!

Recursive factorial The factorial of
a positive integer is the product of all positive integers less than or equal to that number. The factorial of a natural number n is written as n!.
1. When n=0, n! =0! =1
2. When n is a positive integer greater than 0, n! =1×2×3×…×n

Example: the function call itself

 

Fibonacci sequence

The Fibonacci sequence (Fibonacci sequence), also known as the "rabbit sequence", refers to such a sequence of numbers: 1, 1, 2, 3, 5, 8, 13, 21, 34, ... In mathematics, Fibonacci The Bonacci sequence is defined recursively as follows: F(1)=1, F(2)=1, F(n)=F(n-1)+F(n-2) (n>= 3. n ∈ N*) Each digit is equal to the sum of the first two digits. In modern physics, quasi-crystal structure, chemistry and other fields, the Fibonacci sequence has direct applications.
1. Define a function fib
2. Can receive an integer parameter of num
3. Calculate the result of 1 + 1 + 2 + 3 + 5 + 8 ... num

What is the Nth number

 

 


Hint: Recursion is a programming technique, and it will feel a bit difficult to get in touch with recursion for the first time! It is especially useful when dealing with uncertain loop conditions, such as traversing the entire file directory structure

Guess you like

Origin blog.csdn.net/pythonxuexi123/article/details/114399230