Python---functions

Source: Internet
Author: User

function Features: Reduce duplication of code, make programs extensible, and make programs easier to maintain

1. Defining Functions
#定义函数def func1 (): "" "Test" "Print (" This is Test ") return 0# defines the procedure #通俗的说过程就是没有返回的函数, but Python returns a none Def func2 (): "" "Test1" "" Print ("This is Test") #调用函数x = func1 () y = Func2 () #打印函数返回值print (x, y) #0 None


2. function return value

#函数返回值作用: Determines the execution of the function.                                    def test1 (): Print ("This was test") #返回nonedef test2 (): Print ("This is Test") return 0 #返回0def test3 (): Print ("This is Test") return 1, "Hello", ["a", "B"],{"a": "B"} #返回 One tuple x = test1 () y = test2 () z = test3 () print (x) print (y) print (z)


3. Function parameters

#1. Positional parameters and keyword parameters def test1 (x, Y, z):     print (×)     print (y)      print (z) test1 (a)          #位置参数, in order about Test1 (y=2,z=3 , x=1)    #关键字参数, location independent test1 (1,z=3,y=2)      #既有位置参数, and the keyword parameter, the positional parameter cannot be preceded by the keyword parameter. def  test2 (x,y=2):     print (x)     print (y) test2 (1)               #如果不指定y, the default value is used, and the new value is specified with the new value test2 (1,3) test2 (1,y=3) #3. Parameter Group def  test3 (*args):        #将传入值转化成一个元组     print (args) Test3 (1,2,3,4,5) test3 (*[1,2,3,4,5]) #输出 #  (1, 2, 3, 4, 5) #  (1, 2, 3,  4, 5) Def test4 (X,*args):     print (x)     print (args) Test4 (1,2,3,4,5) #输出 # 1#  (2, 3, 4, 5) def test5 (**kwargs):          #将 "OffThe key-word parameter "translates into a dictionary     print (Kwargs) test5 (y=1,x=2,z=3) #输出 # {' y ': 1,  ' x ':  2,   ' Z ':  3}def test6 (Name,age=18,**kwargs):     print (name)      print (age)     print (Kwargs) test6 ("Feng", y=1,x=2) #输出 # feng# 18# {' y ':  1,  ' x ':  2}test6 ("Feng", 23,y=1,x=2) #输出 # feng# 23# {' x ': 2,  ' y ':  1 }


4. Local variables and global variables

name1 = "Fengxiaoli" #全局变量, in effect for all functions, if the global variable defines a string or a number, then only local after local modification, Def test (): #对全局还是没生效, if the global variable definition is the dictionary, list, collection, class, then after the local modification to the global also take effect #global name #在函数中声明全局变量, with caution name1 = "CX" #局部变量, only in the function in effect prin T (name1) test () print (name1) # output: # cx# fengxiaoliname2 = ["CX", "Fengxiaoli"]def test2 (): name2[0] = "CX" Print (name2) t Est2 () print (name2) # output: # [' CX ', ' Fengxiaoli ']# [' cx ', ' Fengxiaoli ']


5. Recursive function

#递归特性: There must be a definite end condition. Each time a deeper recursion is reached, the problem size should be reduced compared to the previous recursion. Recursive efficiency not high Def calc (n): print (n) if int (N/2) >0:return (Calc (int (N/2))) Calc (10)


6. High-order function

#变量可以指向函数, the function's parameters can receive the variable, then one function can receive another function as a parameter, which is called the high order function Def test (a,b,f): res = f (a) +f (b) Print (RES) test ( -5,3,abs) #这里的abs是一个求绝对值的函数


7. Anonymous function

Calc = Lambda x:x*3print (Calc (3))


Python---functions

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.