python--005-function parameters, variables

Source: Internet
Author: User

• Parameters of the function
·· Parameters
1. Form parameter: parameter--form parameter
Parameters in parentheses when creating a function
2. Actual parameter: argument--argument
Arguments passed in parentheses when a function is called

·· function document
1. Write the string directly
2. Note with # Mark Line
3. You can output content directly with print (' content ')

#--Coding:utf-8--
#这个函数求两个数值之和
def add (num1,num2):
' Return the NUM1 and num2 ' #这行字符串是一个注释
Print (' Two-value and ')
Return num1+num2

Print Add (+)

Read the function document:
1. Name of the function. Doc Two underscore attribute name doc inside store text with comment
Int.Doc
2.help (name of function)
Help (str)

·· Keyword parameters

Defines the parameters of the keyword

1. Position parameter:
Direct parameter (relative to position--one by one)
2. Keyword parameters

    def myFunction(num1,num2):    print num1,num2    myFunction(num2=‘world‘,num1=‘hello‘)    1、位置参数与关键字参数混用的时候,位置参数必须在关键字            参数的前面            def myFunction(a,b,c):            print a,b,c            myFunction(a=‘hello‘,‘world‘,‘python‘)            返回错误        2、不要给同一个形参多次赋值          def myFunction(a,b,c):          print a,b,c          myFunction(a=‘hello‘,b=‘world‘,b=‘python‘)            返回错误

··· Default parameters

是定义了默认值的参数 def myFunction(a=‘hello‘,b=‘world‘): print a,b myFunction(‘hhe‘,‘dsb‘)    返回hehe das1、默认参数与位置参数混用的时候,位置参数必须在默认参数之前,位置参数要前面放    def myFunction(a=‘hello‘,b=‘world‘):    print a,b    myFunction(,‘dsb‘)            返回错误     2、默认参数只能在函数定义阶段赋值一次(仅仅一次)      def myFunction(a=‘hello‘,b=‘world‘):       print a,b       myFunction()        关键字参数在函数调用时候定义,默认参数在函数定义的时候赋值。

···· Variable length parameters (collect parameters)

1. Precede the parameters with *
Pack all parameters to the meta-ancestor for output

                    def fun(*a):                    print a                    fun(‘hehe‘,‘hello‘)

2. Add * * on the parameter above
Package all parameters into a dictionary

······· Named keyword parameters

Parameters that follow the variable-length parameter should use the named keyword argument

    def fun(*a,b):    print a    fun(‘hehe‘,‘hello‘,b=‘world‘)    def fun(b,*a):    print a    fun(‘hehe‘,‘hello‘,‘world‘)

1. Variable length parameter must be after position parameter

The parameter of the print function is the variable-length parameter, and the output element ancestor

API documentation How to view Help or F1

Functions and procedures

Functional function
Process procedure

Python has only functions, no process

···· return value

即便没有在函数中写return语句,函数也会返回一个结果,这个结果为None,为空数据类型。如果要返回多个值,可以直接书写,多个值之间用逗号隔开,返回值为一个元祖。···函数变量的作用域变量有作用域(变量的可见性)

1, local variables: the local variable

        1、定义在函数内部的变量        2、先赋值,后使用        3、作用范围从定义开始到包含代码块结束

2. Global variables variable

        1、定义在函数的外部        2、先赋值后使用        3、作用范围:整个文件,可以被多个函数调用        4、如果局部变量与全局变量发生冲突,以局部变量为准            def fun1(para1):            print para1            def fun2(para1):            print para1            a=10            def fun3(para1):                        a=20                        print para1            fun1(a)            fun2(a)            fun3(a)

When accessing global variables inside a function, create an identical local variable inside the function, the original local variable is unchanged
a=10
Def fun3 ():
A=20
Print a
FUN3 ()
Print a

3, global variables

  a=10        def fun3():                        global a                        a=20                        print a        fun3()        print a        结果返回两个20        定义全局变量(如果在函数中修改global定义的变量,全局变量跟着改变)

• Inline functions
functions defined inside functions: inline functions (intrinsic functions)

The entire scope of the inner function, within the outer function (from the beginning of the definition to the end of the block containing it)

    1、外部函数的局部变量与内部函数的局部变量发生命名冲突,以内部函数的局部变量优先    2.外部函数不能访问内部函数的局部变量    3.在内部函数中访问外部函数的局部变量时,不能进行a+=1,这样的操作,原因就是a+1中的局部变量a没有赋值。

#lambda表达式
Python allows anonymous functions to be created using lambda expressions

··· anonymous functions
Functions with no function name:
Lambda expression:
1. Keywords: lambda
2, create the form: The parameters of the lambda function: the implementation of the function
3, Lambda statement constructs a function object, returns the information of a function object, only needs to assign the value

  fun=lambda a:a+1      print fun(3)

4. If multiple parameters are passed in

    fun=lambda a,b,c:a+b+c    print fun(2,3,4,)··作用 1、只需要使用一次,更加精简,不要单独创建函数2、简化代码,提高代码的可读性

Nesting and recursion of functions

·· Nesting of functions: calling other functions in one function

Solve a two-dimensional equation

                ax^2+bx+c=0                x=frac{-b\pm{\sqrt{b^2-4ac}}}{2a}    def jieFangCheng(a,b,c):#先判断方程有没有解            if qiuJie(a,b,c):#判断qiuJie()返回的真还是假    #继续解方程求出所有的解                delta = b*b-4*a*c                x1=(-b+delta**0.5)/2*a                x2=(-b-delta**0.5)/2*a                print x1,x2        else:                print ‘无解‘     def qiuJie(a,b,c):            delta=b*b-4*a*c            if delta>=0:                    return True            else:                    return False        jieFangCheng(1,1,1)        jieFangCheng(1,1,-1)

·· Recursive

Recursion: Call Yourself yourself

        def factorial(n):                    if n==1:                                return 1                    return n*factorial(n-1)            print(factorial(4))

Note: Prevent infinite recursion from looping differently
Hanoi

    def han(a,b,c,n):                if n==1:                        print a+‘---‘+b                else:                        han(a,c,b,(n-1))                        print a+‘----‘+b                        han(c,b,a,(n-1))    han(‘A‘,‘B‘,‘C‘,3)

python--005-function parameters, variables

Related Article

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.