Python review-Review 4 Lessons (December 1)

Source: Internet
Author: User

Task:

Review 4 sessions (December 1)
1.1 Function definitions
1.2 Parameters of the function
1.3 Default parameters for functions
1.4 Variables of the function
1.5 return value of function
1.6 Multi-type pass-through values and redundancy parameters
1.7 Recursive invocation of functions

Notes:

function definition

A function is a group of statements that accomplishes a particular function, which can be used as a unit and give it a name.
The function name can be executed multiple times in different places of the program (this is often called a function call).
Pre-defined Functions
can be used directly
Custom functions
Users write their own

Why use a function
Reduce programming Difficulty
-Usually a complex big problem is broken down into a series of small problems, then the small problem is divided into smaller problems, when the problem is refined enough simple, we can divide and conquer. Each small problem solved, the big problem is solved.
Code reuse
-Avoid repetitive labor and provide efficiency

Definition and invocation of functions
-def function name ([parameter list])://Definition
-Function name ([parameter list])//Call


Parameters of the function
Formal parameters and actual parameters
-When defining a function, the name of the variable in parentheses after the function name is called a "formal parameter," or "formal argument"
-When calling a function, the name of the variable in parentheses after the function name is called "actual parameter", or "argument"
Default parameter (default parameter)
def fun (x, y=100):
Print x, y
Fun ($)
Fun (1)


Variables of the function
Local variables and global variables
-Any variable in Python has a specific scope
-Variables defined in a function are generally only used inside the function, and these variables that can only be used in specific parts of the program are called local variables.
-Variables defined at the top of a file can be called by any function in the file, which can be used as a global variable for the entire program

Global statement
-Global variable Name
Force declaration as a global variable

function return value
-a specified value is returned when the function is called
-Default returns none after function call
-Return value
-The return value can be any type
-function terminates after return execution
-difference between return and print

Multi-type pass-through values and redundancy parameters

Sending tuples and dictionaries to functions
Handling Superfluous arguments
def fun (X,y,*args,**kwargs)


Recursive invocation
def factorial (n):
if n = = 0:
Return 1
Else
return n * Factorial (n-1)

Print factorial (5)


Considerations for recursion
Must have the last default result
if n = = 0
Recursive parameters must converge to the default result:
Factorial (n-1)


Python review-Review 4 Lessons (December 1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.