Python小白--函数基础

    科技2022-09-08  107

    # # 计算程序运行的时间 # import time # import datetime # starttime = datetime.datetime.now() # time.sleep(5) # endtime = datetime.datetime.now() # print((endtime - starttime).seconds ) ''' 函数def def function(): 函数体 ''' def add_function(a, b): c = a + b print(c) def reduce_funcion(x, y): z = x -y print(z) def multipli_function(a1,a2,a3): A = a1*a2*a3 print(A) # 斐波那契数列 f(n)=f(n-1)+f(n-2) n>=2 def fibs(n): result = [0,1] for i in range(n-2): result.append(result[-2] + result[-1]) return result def fibs_function(n): for i in range(0,n): if(n==1): return 0 if(n==2): return 1 if(n>2): return fibs_function(n-1) + fibs_function(n-2) # 输出了第10个斐波那契数列 ''' def add_function(a, b): 这里是函数的开始。 在声明要建立一个函数的时候,一定要使用def(def 就是英文define的前三个字母) 意思就是告知计算机,这里要声明一个函数;add_function是这个函数名称. add_function(2,3) 这才是真正调用前面建立的函数,并且传入两个参数:a=2,b=3。 仔细观察传入参数的方法,就是把2放在a那个位置,3放在b那个位置(所以说,变量就是占位符). ''' ''' 注意 在python中,变量无类型,只有对象才有类型,这句话应该说成:x,y并没有严格规定其所引用的对象类型. # add_function("qiwsir",4) # error :can only concatenate str (not "int") to str # 只能将str(而不是“ int”)连接到str add_function(a,b)的意义完全取决于对象的类型 在python中,将这种依赖关系,称之为多态。对于python中的多态问题, python中为对象编写接口,而不是为数据类型 ''' # 函数参数 # Python语言中的所有参数(参数)都将通过引用传递。如果在函数中更改参数所指的内容,则更改也会反映在调用函数的外部 def changeme1( mylist ): "This changes a passed list into this function" print ("Values inside the function before change: ", mylist) mylist[2]=50 print ("Values inside the function after change: ", mylist) return def changeme2( mylist ): "This changes a passed list into this function" mylist = [1,2,3,4] # This would assi new reference in mylist print ("Values inside the function: ", mylist) return ''' 函数参数 1.必需参数 2.关键字参数 3.默认参数 4.可变长度参数 ''' # 必需参数 def printStr(str): print(str) return #printStr('hhh') #printStr() ''' hhh Traceback (most recent call last): File "e:/ProgramCode/PythonBasic/def_function.py", line 103, in <module> printStr() TypeError: printStr() missing 1 required positional argument: 'str'\ ''' # 关键字参数 def printinfokey( name, age ): "This prints a passed info into this function" print ("Name: ", name, "Age: ", age) return # Now you can call printinfo function printinfokey( age = 25, name = "Maxsu" ) printinfokey(name = "Minsu", age = 26 ) #请注意,参数的顺序并不重要 # 默认参数 def printinfomr( name, age = 25 ): "This prints a passed info into this function" print ("Name: ", name, "Age ", age) return # age 默认参数 # Now you can call printinfo function printinfomr( age = 22, name = "Maxsu" ) printinfomr( name = "Minsu" ) # 可变长度参数 ''' def functionname([formal_args,] *var_args_tuple ): "function_docstring" function_suite return [expression] ''' #在定义函数时,可能需要处理更多参数的函数。这些参数被称为可变长度参数,并且不像要求的和默认的参数 def printinfo( arg1, *vartuple ): "This prints a variable passed arguments" print ("Output is: ", arg1) for var in vartuple: print (var, ) return # Now you can call printinfo function printinfo( 10 ) printinfo( 70, 60, 50 ) total = 0 # This is global variable. # Function definition is here def sum( arg1, arg2 ): # Add both the parameters and return them." total = arg1 + arg2; # Here total is local variable. print ("Inside the function local total : ", total) return total # Now you can call sum function sum( 10, 20 ) print ("Outside the function global total : ", total ) ''' Inside the function local total : 30 Outside the function global total : 0 ''' # 主函数 按照顺序进行代码执行 if __name__ == "__main__": add_function(2, 3) reduce_funcion(1,2) add_function('xyz','abc') multipli_function(1,1,1) multipli_function('123',1,3) fibs_list = fibs(10) print(fibs_list) fibslist = fibs_function(10) print(fibslist) # add_function("qiwsir",4) # error :can only concatenate str (not "int") to str # 只能将str(而不是“ int”)连接到str mylist = [10,20,30] changeme1(mylist) changeme2(mylist) print(mylist) #mylist在被调用结束之后变量就释放了,还是之前定义的值//局部变量 ''' 全局与局部变量在函数体内定义的变量具有局部作用域,外部定义的变量具有全局作用域。 局部变量只能在它们声明的函数内部访问,而全局变量可以通过所有函数在整个程序体中访问。 当调用一个函数时,它内部声明的变量被带入范围 '''
    Processed: 0.013, SQL: 9