Python---functions

Source: Internet
Author: User

function learning, the main points are summarized as follows:

1. Function default can not return a value, no return value is returned to none
2. A function can have multiple return values, and the type can be different, but in fact multiple return values are collectively a tuple
3. Key parameters cannot be written in front of position parameters
1 #-*-coding:utf-8-*-2 #LC3 4 Import Time5 #function:6 defLogger ():#Defining log Functions7Time_format ='%y-%m-%d%x'        #defining the time format8Time_current = Time.strftime (Time_format)#Crawl Current Time9With open ("Log_file","a") as F:TenF.write ("%s This is end\n"%time_current) One  A deffunc1 (): -     """To declare the function"""           #explain what this function does. -     Print("This is function1")#function Work module, logic the logger () -     return0#function return value -  - #procedure: #过程是没有返回值的函数, in Python, the procedure returns none by default + defFunc2 (): -     """function 2""" +     Print("This is function2") A logger () at     return1,{"name":"Lvcheng"," Age":" -"}#multiple values can be returned -  -x = Func1 ()#calling Functions -y =Func2 () - Print(x) - Print(y) in  - defFunc3 (x, Y, z):#x, y as formal parameter, same as positional parameter to     Print(x, Y, z) +     returnx, y -  theFunc3 (a)#positional parameter invocation, position needs to correspond to parameter one by one *FUNC3 (1,2,z=3) $FUNC3 (Y=2,z=3,x=1)#Keyword invocation, key parameter, independent of formal parameter orderPanax Notoginseng  - #Default Parameters the defFunc4 (x,y=3):#y is the default parameter +     Print(x) A     Print(y) theFUNC4 (1) +Func4 (1,5) - #default parameters can not be passed, and if there is a pass, the value is passed $ #use 1, default installation time, 2, link database port number, etc. $  - #parameter groups, implementation of non-fixed functions for arguments - defFunc5 (*args):#* is the keyword, the way the tuple is passed the     Print(args) - Wuyi #*args accepts n positional arguments, transforms the Narimoto group in a way the  -Test1 = Func5 (1,2,3,4,5)#1,2,3,4,5 is passed to args as a tuple (1,2,3,4,5) WuTest1 = Func5 (*[1,2,3,4]) -  About #parameter groups, which are implemented in a dictionary way $  - defFunc6 (* *Kwargs): -     Print(Kwargs) -     Print(kwargs['name'])#take the value in the dictionary A     Print(kwargs[' Age']) + #**kwargs receives n keyword arguments, which are converted to dictionaries in a way that passes the  -  $Test2 = Func6 (name="LC", age=9)#The keyword argument must be passed theTest2 = Func6 (**{"name":"LC"," Age": 10}) the  the  the defFunc7 (Name,age=19,**kwargs):#physical parameters, with default parameters, parameter groups -     Print(name) in     Print(age) the     Print(Kwargs) the  AboutTest3 = Func7 ("LC", 21,sex="M", hobby="Girl") theTest3 = Func7 ("LC", sex="M", hobby="Girl", age = 32) the  the defFunc8 (name,age=19,*args,**Kwargs): +     Print(name) -     Print(age) the     Print(args)Bayi     Print(Kwargs) the  theFunc8 ("LC", 2,3,4,sex="M", hobby=" Money")

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.