def name1(): #def defines the function name is the function name
'''file comment''' # Note that this step is a good habit, usually write
print ( 'in the name1' )
return 0
def name2():
print ( 'in the name2' )
#If no return is added, the result of printing is none
x=name1()
y=name2()
print('from name1 return is %s'%x)
print('from name2 return is %s'%y)
import time #Import module
def logger(): #The logger defines a logger as the following with can be used multiple times
#It is more convenient to use when there are multiple data
time_format= '%Y-%m-%d %X' # time_forma defines your time format Y year m month d day X hours minutes seconds
time_current=time.strftime(time_format )
with open ( 'a.txt' , 'a+' ) as a:
a.write('%s end action 哈哈 \n'%time_current )
def test1():
print('test1 starting action...')
logger()
def test2():
print('text2 starting action...')
logger()
def test3():
print('text3 starting action...')
logger()
test1()
test2 ()
test3()
Reuse of logger code
def name1():
print('in the name1')
def name2():
print('in the name2')
return 0
def name3():
print('in the name3')
return 1,'han',['bin','long'],{1:2}
x=name1()
y=name2()
z=name3()
print (x)
print (y)
print (z) #finally returns a composite tuple
def name1(x,y):
print(x)
print(y)
name1( 1 , 3 ) #If it is not written in ( ), an error will be reported. The number of parameters
#def in ( ) should correspond to the number in ( ) in the last name1
def name2(x,y):
print (x )
print (y)
x= 1
y= 2
name2( y = 2 , x = 1 ) # has nothing to do with the position of the formal parameter
# Note () can be written like this (3, y=2) but not (y=2, 3) no matter how many Quantity y=? to last
def name(x,y=2):
print(x)
print(y)
name(1)
#默认参数特点:调用函数的时候,默认参数非必须传递
#用途:1.默认安装2.连接数据库
#*args:接受N个位置参数,转换成元组形式
# def test(*args):
# print(args)
# test(1,2,3,4,5,5)
# test(*[1,2,4,5,5])
# test(1,1,1,1,1,1)# args=tuple([1,2,3,4,5])
# def test1(x,*args):
# print(x)
# print(args)
#
# test1(1,2,3,4,5,6,7)
#**kwargs:接受N个关键字参数,转换成字典的方式
# def test2(**kwargs):
# print(kwargs)
# print(kwargs['name'])
# print(kwargs['age'])
# print(kwargs['sex'])
# test2(name='alex',age=8,sex='F')
# test2(**{'name':'alex','age':8,'sex':'F'})
# def test3(name,**kwargs):
# print(name)
# print(kwargs)
# test3('alex',age=18,sex='m')
# def test4(name,age=18,**kwargs):
# print(name)
# print(age)
# print(kwargs)
# test4('alex',age=34,sex='m',hobby='tesla')
def test4(name,age=18,*args,**kwargs):
print(name)
print(age)
print(args)
print(kwargs)
logger("TEST4")
def logger(source):
print("from %s" % source)
test4('alex',age=34,sex='m',hobby='tesla')
#局部变量 只改变局部的值
def change_name(name):
global school #globe局部变量
school = "Mage Linux"
print("before change",name,school)
name ="Alex li" #这个函数就是这个变量的作用域
age =23
print("after change",name)
name=('hanbinlong')
change_name(name)
print(name)
def calc(n):
print(n)
if int(n/2) >0:
return calc( int(n/2) )
print("->",n)
calc(10)
递归特性:
1 必须有一个明确的结束条件
2每次进入一个更深的递归中 问题模式的数量要有一定的减少
def add(a,b,f): return f(a)+f(b) res = add(3,-6,abs) print(res)