The basic learning function of Python

Source: Internet
Author: User

1. Definition of function:
def function name (parameter):
function body
return value

Example:
def fuction1 (name):
If name = ' Alex ':
Print (' Success ')
Else
Print (' Failure ')


2. Return value:
Tell the result of the function execution, success or not;
Def F1 (age):

If Age >18:
Return ("adult")
Else
Return ("Flying adult")
3. Parameters
3.1: no parameter function:

def alarm ():

Login mailbox;
Send alarm information;
3.2 Parameter function:

3.2.1: Common parameter function:
#定义函数f1, name is the formal parameter of the F1 function
def f1 (name):
Print (name)
#执行函数, HJW is the actual parameter of the F1 function, called the real argument
F1 (' HJW ')

3.2.2: Default parameter function:

#定义函数f1, Name,age is the formal parameter of the F1 function, and age is also called the default argument, and the default argument must be placed at the end of the argument list
Def f1 (Name,age =20):
Print (Name,age)
#执行函数, HJW is the actual parameter of the F1 function, and age uses the default parameter: 20
F1 (' HJW ')
#执行函数, HJW is the actual parameter of the F1 function, age uses actual parameters: 25
F1 (' HJW ', 25)

3.2.3: Dynamic parameter 1:
#默认参数是按照元祖传入
def func (*args):

Print (args)


# execution Mode One
Func (1,24,6,88)

Execution results: (1, 24, 6, 88)

#执行方式二
Li = [11,22,22,55,55,4,54]
Func (*li)
Execution results: (11, 22, 22, 55, 55, 4, 54)
#执行方式三:
Func (LI)

Execution results: ([11, 22, 22, 55, 55, 4, 54],)


3.2.4: Dynamic Parameter 2:
#默认参数是按照字典, so the arguments are passed in the same way as the key-value pairs;
def f1 (**kwargs):
Print (Kwargs,type (Kwargs))

#执行

F1 (k1=123,k2=456)

#执行结果:

{' K1 ': 123, ' K2 ': 456} <class ' Dict ' >

The basic learning function of Python

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.