Xi. python function learning

Source: Internet
Author: User

1. Defining functions

def function name (formal parameter):

function body

return XXX--------The following content is no longer executed

---------------------------------------------------------------------------------------------------------------

2. Execution function

Function name (argument)

---------------------------------------------------------------------------------------------------------------

3. Parameters, arguments (default in order)

---------------------------------------------------------------------------------------------------------------

4. Perform parameter incoming arguments, but not in order

---------------------------------------------------------------------------------------------------------------

5. Functions can have default parameters

---------------------------------------------------------------------------------------------------------------

6. Dynamic Parameters
#动态参数一, the type is Ganso, and the passed parameter is Ganso element
def f1 (*a):
Print (A,type (a))
F1 (123,234,[456123789],{1:2})
#动态参数二, the type is a dictionary, and the passed argument is a dictionary key-value pair
def f1 (**a):
Print (A,type (a))
F1 (k1=123,k2=456)
#万能动态参数-------A * compared to the previous
def f1 (*a,**p):
Print (A,type (a), type (p))
F1 (123,234,[456123789],{1:2},k1=123,k2=456)
---------------------

(123, 234, [456123789], {1:2}), <type ' tuple ' >)
({' K2 ': 456, ' K1 ': 123}, <type ' Dict ' >)
(123, 234, [456123789], {1:2}), <type ' tuple ';, <type ' dict ' >)

---------------------------------------------------------------------------------------------------------------

7. Incoming dictionaries for dynamic parameters, list

def f1 (*args):
Print (Args,type (args))
L1=[11,22,33,44]
F1 (L1)
F1 (*L1)
------------------------

([One, one, one, and], <type ' tuple ' >)
((one, one, one, one), <type ' tuple ' >)

------------------------

def f2 (**args):
Print (Args,type (args))
l1={"K1": "123"}
F2 (L1=L1)
F2 (**L1)
-------------------------

({' L1 ': {' K1 ': ' 123 '}, <type ' Dict ' >)
({' K1 ': ' 123 '}, <type ' Dict ' >)

---------------------------------------------------------------------------------------------------------------

8. Global variables, local variables

p= "Chushujin"
Def func1 ():
#局部变量
A=123
Global P #加上此关键词后, the globals will be modified, otherwise they will not be modified
Print (a)
p= "Zhangyu"
Def FUNC2 ():
Print (P)
Func1 ()
Func2 ()
----------------------

123
Zhangyu

Xi. python function learning

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.