Functions of Python

Source: Internet
Author: User

Declaring a function

Def checkname ():    a =    if a%2 ==0:        print '%s is even '% (a)        return a    else:        print '%s is odd '% (a)        return Aprint CheckName ()

DECLARE keyword def

Then a space.

And then the function name CheckName.

Then the parameter () #参数可以为空, that is, no parameter

return value #见下面介绍

-----------------------Split Line

Just a function, two places.

1. Parameters

Optional parameters: No parameter, there are 1 fixed parameters, there are many fixed parameters, fixed parameters have default values, not fixed parameters

def checkname () #无参

def checkname (b) #固定1个参数

def checkname (b = ' test ') #固定1个参数, there is a default value, that is, you can provide parameters when using, or do not provide, when not provided with the default value

def checkname (b,c) #固定2个参数

def checkname (*b) #不定长参数, which is the incoming tuple

def checkname (**b) #不定长参数, which is the incoming dictionary

2. Return value

Returns none by default when a return statement is not used

Return a returns a single member

Return (A,B.C) returns multiple members

Variables declared within a function are local variables by default and do not affect variables outside the function

If you want to influence it, you need to declare it as a global variable.

b = Onedef  values ():    global b    # declared as a globals variable    b =values ()    print b# printed here b=15= onedef  values ():     =values () print b# b=11 printed here

In general, it is not recommended to use the global variable

Add function description

def values ():     ' valuetest '    # function Description    b =print values. __doc__    # View Description

 

Functions of Python

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.