Python is not a road _ a first-level function, and python is not a road to first-level functions

Source: Internet
Author: User

Python is not a road _ a first-level function, and python is not a road to first-level functions

What is a function?

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.

Python Function Format:

1 def func1 (): # Use def to define a function in python. Function Name: func1 () 2 ''' this is test1''' # function explanation. You can use help (func1) command to view func1 function description 3 print ('test1') # function execution statement 4 return 0 # return Value, the function must have a return value, no return value, it is not a function, it is a process.

Note that a function must have a return value. If there is no return value, it is not a function. It is a process. Return can return multiple values and multiple data types. All values are packaged as elements. If no return is returned, the return value is none.

def fun1():    print('test1')    return 1,abc,[1,23,asd,],{'name':'gally-jiang'}
x = fun1()
print('x')

Code Result (1, abc, [1, 23, asd,], {'name': 'gally-jiang '})

 

def fun1():    print('test1')x = fun1()print('x')

Code result none

Parameters

Parameters:

Real parameter: literal explanation, actual, specific value Parameter

Def func1 (x, y): # x, y is the form parameter x = 5 # x = 5 is the real parameter y = 6 # y = 6 is the real parameter print (x, y) return 1

Location Parameter: each value corresponds to a location of the parameter.

def func1(x,y)    print(x,y)func1(1,2)

Output result 1 and 2.

Default parameter: the parameter of the default value.

Def func1 (x, y = 2): # The default value of y is 2 a = x ** y print (a) return 1.

 

Variable Parameter (tuples): this parameter is used when the number of parameters cannot be confirmed. It is represented by * and the output result is tuple.

Def func1 (* args): print (args) func1 (1, 56, 'gally ', '7 ')
Output result (, 'gally ', '7'), which is a ancestor tuple

Variable Parameter (dictionary): the parameter is represented in a dictionary and is represented.

Def func2 (** kvargs): print (kvargs) func2 (name = 'gally ', age = '23') output result {'name': 'gally ', 'age': 23}. The output result is the dictionary dict.

 

Function declaration parameter sequence: required functions> default functions> variable functions (tuples *)> variable functions (Dictionary **)

------- O & M that won't be developed is not a good cook

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.