Create function: record function: Document string. __doc__ is a function property. Built-in Help function: use it in the interactive interpreter to get information about the function, including its document string.
parametric Magic: Assigning a new value to a parameter within a function does not change the value of any external variable, and in Try_to_change, the parameter n gets the new value, but it does not affect the name variable. n is actually a completely different variable that works in a similar way as below. The result is obvious. When variable n changes, the variable name does not change. Similarly, variables outside the function are not affected when parameters are re-bound (assigned) inside the function. (Parameters are stored in a local scope.) )
strings (and numbers and tuples) are immutable. That cannot be modified (that is, it can only be overwritten with new values). So they do not need to introduce more when they do the parameters. But consider what happens when you use a mutable data structure such as a list as a parameter: In this case, the parameter is changed. When two variables refer to a list at the same time, they do refer to a list at the same time. It's that simple. If you want to avoid this situation, you can copy a copy of the list. When slicing in a sequence, the returned slice is always a copy. Therefore, if you copy the entire list of slices, you will get a copy (equal but not the same).
1. Using a function to change a data structure (such as a list or dictionary) is a good way to abstract a program.
2, in some languages, it is common to rebind parameters and make these changes affect variables outside of the function. But this is not possible in Python: the function can only modify the parameter object itself.
keyword parameters and default values:1, Position parameters
2, keyword parameters: The main function is to be clear about the role of each parameter, the keyword parameter is the most powerful place in the function can be given the default value of the parameter.
Collect parameters: The user can give the function multiple parameters (*params), the asterisk means ' collect the remaining positional parameters ', print the result as a tuple, and a tuple of length 1 has a comma. If no element is provided for collection, the params is an empty tuple. However, you cannot handle keyword parameters. We need another ' collect ' operation-' * * ' that can handle keyword parameters.
reversal procedure: asterisks are only useful when defining functions (allowing the use of an indefinite number of arguments) or calling (' splitting ' a dictionary or sequence).
Practice Using Parameters: Practice and Mastery
Scope : In addition to the global scope, each function call creates a new scope. The Foo () function here changes (re-binds) the variable x, but at the end of the day, X does not change. This is because when Foo is called, the new namespace is created, which acts on the block of code within Foo. The assignment statement x=42 only works in the internal scope (local namespace), so it does not affect the X in the outer (global) scope. Variables within a function are called local variables (global variables can be accessed directly inside the function, and global variables are used with caution).
Reading global variables is generally not a problem. However, 1, if the name of a local variable or parameter is the same as the global variable name you want to access, you cannot access it directly. Global variables are masked by local variables. If you do, you can use the GLOBALS function to get the global variable value. 2. Re-bind global variables.
Function nesting: One function is in the other, and the outer function returns the inner function. This means that the function itself is returned, but it is not called. Importantly, the returned function can also access the scope where its definition is located. Each time the outer function is called, its internal functions are re-bound, and the factor variable has a new value each time.
recursion: Two classics: factorial and power.
The mathematical definition of factorial: 1, 1, the factorial is 1,
2, factorial of the factorial =n-1 of the number n greater than 1 * n
The mathematical definition of power: 1, for any number x, Power (x,0) is 1
2. For any number greater than 0, power (x,n) is the result of x multiplied by (x,n-1)
Two-point lookup: bisect module.
Basic Python Tutorial (6)