#!/usr/bin/env python #-*-coding:utf-8-*-function related
def f1 (par,par2,par3= "OK"): #定义函数 with parameter, with parameter default value with return value
Try
Print ("This was function by%s%d%s"% (par, PAR2,PAR3))
Except
Return False
Else
Return True
res = f1 ("WH") #调用函数 not to change default parameters
Print (RES)
Res2 = F1 ("WX", +, "pig") #改变默认参数
Print (Res2)
Res3 = F1 (par2=24,par= "MM", par3= "shit") #调用使用指定参数形式 can be not in the form parameter order
#------------------------------Dynamic Parameters-----------------
def f2 (*args): # * denotes any of the formal parameters
Print (args)
F2 ("WX", "WH", "QQ") #动态传人多个参数
Li = "Wanghao"
F2 (*li) #调用带 * Change each character of a string into an element of a tuple
F2 (LI) #如果不带 * The string is treated as an element in the tuple
def f3 (**args): # two * number parameter must be dictionary key value pair form K=v form
Print (Args,type (args))
F3 (k1=1,k2=2)
Dict1 = {"K1": 3, "K2": 4}
F3 (Kk=dict1) #把当前字典对象当作一个键值对中的Value
F3 (**dict1) #如果调用带两个 * Simply pass each key-value pair of the current dictionary as a parameter to the function
def f4 (*args,**kwargs): #万能参数
Print (args)
Print (Kwargs)
F4 (11,22,33,44,k1= "v1", k2= "v2", k3= "v3") #根据属性自动把参数带入 corresponding parameters 11,22,33,44 bring *args k1....k3 into **kwargs
#---------------------------------------Dynamic parameter End--------------
#函数中传递的参数是引用
#----------------------------Global Variables-----------------
#未定义到函数内部的变量是全局变量 #修改全局变量 Global name #如果全局变量是 list or dictionary, you can add a list element to a function but you cannot reassign a variable #定义全局变量全用大写
Gname= "Alex"
def f5 ():
Print (Gname)
Global Gname #需要修改全局变量 must declare
Gname = "ALEX.W"
def f6 ():
Print (Gname)
F5 ()
F6 ()
Python Basic Learning-function-related