Python3.5 getting started learning record-function, python3.5 Function

Source: Internet
Author: User

Python3.5 getting started learning record-function, python3.5 Function
Python Functions

Functions are organized and reusable, and used to implement a single, or associated function code segment.

Functions can improve the app's vigilance and reuse of code. You already know that Python provides many built-in functions, such as print (). But you can also create a function by yourself, which is called a user-defined function.

Define a function

You can define a function that you want to function. The following are simple rules:

  • Function Code BlockDefStarts with a keyword, followed by a Function Identifier name and parentheses().
  • Any input parameters and independent variables must be placed in the middle of parentheses. Parentheses can be used to define parameters.
  • The first line of a function can selectively use the document string-used to store the function description.
  • The function content starts with a colon and is indented.
  • Return [expression]The end function selectively returns a value to the caller. Return without expression is equivalent to return None.

Instance

def sayHello(name):   return "Hello:{0}".format(name)

This is a very simple Python function. It accepts a parameter and returns it.

Function call

Defining a function only gives the function a name, specifying the parameters contained in the function and the code block structure.

After the basic structure of this function is complete, you can call another function or execute it directly from a Python prompt.

The following example calls the sayHello () function:

# Define function def sayHello (name): return "Hello: {0}". format (name) # Call function print (sayHello ("jiming "))

Output result:

MyList = [1, 2, 3] print ("myList value before modification:", myList) def changeMyList (list): myList [0:] = [3, 2, 1] print ("myList value after Function Modification:", myList) changeMyList (myList) print ("myList value after Function Modification:", myList)

In the above Code, we change the value of myList in the function to 3, 2, 1, and the code execution result is:

# Define function def sayHello (name1, name2): return "Hello: {0} and {1 }". format (name1, name2) print (sayHello (name2 = "jiming2", name1 = "jiming1 "))

The input sequence of the above Code name1 and name2 is inconsistent with the definition of the function, but we use the keyword name1 \ name2 to limit, so the function can still correctly read the corresponding parameter

3. Default Parameters

When a function is called, if the default parameter value is not passed in, it is considered as the default value. This is the same as the default parameter of C #, for example:

# Define function def sayHello (name1, name2 = "jiming2"): return "Hello: {0} and {1 }". format (name1, name2) print (sayHello ("jiming1 "))

The output result is:

# Define function def sayHello (name1 = "jiming1", name2): return "Hello: {0} and {1 }". format (name1, name2) print (sayHello ("jiming2 "))

The compiler reports the following error: SyntaxError: non-default argument follows default argument

That is to say, we put necessary parameters behind the default parameters, which will produce ambiguity.

4. Indefinite Parameters

You may need a function to process more parameters than when it was originally declared. These parameters are called indefinite parameters. Unlike the preceding two parameters, they are not declared. The basic syntax is as follows:

Def functionname ([formal_args,] * var_args_tuple): "function _ document string" function_suite return [expression]

Instance

# Define function def sayHello (sayPerson, * allPersons): for p in allPersons: print ("{0} say: 'Hello: {1 }'". format (sayPerson, p) sayHello ("jiming", "friend1", "friend2", "friend3 ")

Output result:

Sum = lambda arg1, arg2: arg1 + arg2; # Call the sum function print ". The value after adding", sum (10, 20) print "is: ", sum (20, 20)Variable Scope

All variables of a program are not accessible anywhere. The access permission determines where the variable is assigned.

The scope of a variable determines which specific variable name you can access in the program. The scopes of the two most basic variables are as follows:

  • Global Variables
  • Local variable
Global and local variables

Variables defined in a function have a local scope and global scopes defined outside the function.

Local variables can only be accessed within the declared function, while global variables can be accessed within the entire program. When a function is called, all variable names declared in the function will be added to the scope.

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.