5-11 function assignment parameters, variable parameters

Source: Internet
Author: User

Function

Def sayhi ():

print ' Hi '

Def Cheng (A, B):

Return a*b

Print Cheng (3,4)

Local Variables : function internal definition, release after function call ends

Global Variables : both inside and outside the function can be called, global

>>> def hanshu2 ():
... The global x #x为Global Variables , and X adds 2 to the inside of the function, and the globally variable x also changes the
... x=x+2
... print X

Assignment parameters

def (x=1,y) #语法错误, an assignment parameter cannot be followed by an unassigned parameter, that is, the assignment argument is placed behind

#练习

>>> def compare (a,b,c): #找出最大值函数
... if a>=b:
... if A>=c:return a
... Else:return c
.. else:
... if B>=c:return b
... Else:return c
...

Document string

>>> def Wendang ():
... "Test doc function" #文档字符串
... print ' Hi '
...
>>> Wendang ()
Hi
>>> wendang.__doc__
' Test doc function '
>>>

# Encoding:utf-8 def He (x,*y):                      #* Used to pass any of the non-named parameters, these parameters will be a Tuple in the form of Access     to the "' The exercise of the flight "    ' =    0for in y:        = Total + i*x    return  Totalprint He (3,4)print He (3,4,5)

#Encoding:utf-8defCanshu (** y): # * * is used to process the passing of any parameter with a name, which is accessed using dict PrintU'the length of the input dictionary is:%s'%(len (y))PrintyPrintY.keys ()Printy.values () Canshu (name ='VP', age='m', sex='f' )

5-11 function assignment parameters, variable parameters

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.