dede网站前台没有图片,微信会员卡小程序,公司企业网站建设的建站流程解析,网站模板备份函数
初识函数
函数#xff1a;封装具有某种功能的代码块。
函数定义
使用def关键字来定义函数 # 定义函数 def 函数名(): 代码块 # 调用函数 函数名() 函数参数 def 函数名(形参) 代码块 # 调用 函数名(实参) 位置参数
按参数顺序传参 def func(a, b): print(a b)…函数
初识函数
函数封装具有某种功能的代码块。
函数定义
使用def关键字来定义函数 # 定义函数 def 函数名(): 代码块 # 调用函数 函数名() 函数参数 def 函数名(形参) 代码块 # 调用 函数名(实参) 位置参数
按参数顺序传参 def func(a, b): print(a b) func(1,2) 关键字参数
通过参数名指定值不用关心参数的顺序 def func1(name, age): print(fMy name is {name}.I am {age} years old.) func1(age18, name17Lin) 不定长参数
单星号参数(*args):用于接收任意数量的非关键字参数这些参数被收集到一个元组中。 def sum_numbers(*args): total sum(args) print(total) sum_numbers(1, 2, 3, 4, 5, 6) 双星号参数(**kwargs):用于接收任意数量的关键字参数这些参数被收集到一个字典中。 def print_info(**kwargs): print(kwargs) print_info(name17Lin, age18, num17) 函数返回值
基本返回值
在Python中函数的返回值是函数执行后提供的输出结果。函数通过return语句来指定其返回值。当函数执行到return语句时它会立即停止执行并将紧跟在return后面的表达式的值作为结果返回给调用者。如果没有return语句或者return后面没有跟任何表达式则函数默认返回None。 def add(a, b): return a b print(add(1, 5)) 多返回值
Python中的一个函数实际上可以返回多个值这些值会被打包成一个元组。 def calculate(x): square x ** 2 cube x ** 3 return square, cube square, cube calculate(5) print(fsquare:{square} , cube:{cube}.) 无返回值
如果函数没有明确的return语句或者return后没有跟随任何表达式那么函数将返回None。 def print_func(name): print(name) print_func(print_func(17Lin)) # 输出None 作用域
作用域Scope在编程语言中是指程序中的某个区域其中变量、函数或其他命名实体的有效性和可访问性是受限的。 局部作用域Local Scope 在函数内部定义的变量包括函数参数具有局部作用域这意味着它们仅在该函数内部可见并且在函数调用结束后这些变量的生命周期也随之结束。局部作用域中的变量不能被函数外部直接访问。 全局作用域Global Scope 在函数外部定义的变量或者使用 global 关键字声明的变量具有全局作用域。全局变量在整个模块中都是可见的无论在哪个函数内只要没有同名的局部变量覆盖都可以访问和修改全局变量。 嵌套作用域Enclosing Scope / Nonlocal Scope 在函数内部定义的嵌套函数即一个函数内部定义另一个函数的情况下可能出现嵌套作用域。在这种情况下嵌套函数可以访问其外部函数也称为封闭函数中的变量但不直接属于全局作用域。若想在嵌套函数中修改封闭函数的变量需要使用 nonlocal 关键字声明。
global全局关键字 x 10 # 全局变量 def modify_global(): global x # 声明为全局变量 x 20 modify_global() print(x) # 输出20 nonlocal非局部变量也称为“闭包变量” def outer(): y 10 def inner(): nonlocal y # 声明y为非局部变量 y 20 inner() print(y) outer() 代码练习
题目:字符串转驼峰命名编写一个名为 to_camel_case 的函数它接受一个空格分隔的单词串作为参数返回转换为驼峰命名格式的字符串。例如“hello world hello python”应转换为“helloWorldHelloPython”。
def to_camel_case(words):if not words:return word words.split()new_words word[0].lower()for w in word[1:]:new_words w[0].upper() w[1:]return new_wordsprint(to_camel_case(hello world hello python))题目递归阶乘计算编写一个名为calculated_factorial的递归函数计算并返回一个正整数的阶乘。
def calculated_factorial(n):sum 0if n 1:return 1else:sum n * calculated_factorial(n - 1)return sumprint(calculated_factorial(10)) 文章转载自: http://www.morning.ygwbg.cn.gov.cn.ygwbg.cn http://www.morning.tstwx.cn.gov.cn.tstwx.cn http://www.morning.lsqxh.cn.gov.cn.lsqxh.cn http://www.morning.jlnlr.cn.gov.cn.jlnlr.cn http://www.morning.pdynk.cn.gov.cn.pdynk.cn http://www.morning.rzrbw.cn.gov.cn.rzrbw.cn http://www.morning.sjli222.cn.gov.cn.sjli222.cn http://www.morning.pqryw.cn.gov.cn.pqryw.cn http://www.morning.wjyyg.cn.gov.cn.wjyyg.cn http://www.morning.xxsrm.cn.gov.cn.xxsrm.cn http://www.morning.cmrfl.cn.gov.cn.cmrfl.cn http://www.morning.nyzmm.cn.gov.cn.nyzmm.cn http://www.morning.reababy.com.gov.cn.reababy.com http://www.morning.txfzt.cn.gov.cn.txfzt.cn http://www.morning.xmbhc.cn.gov.cn.xmbhc.cn http://www.morning.hhfwj.cn.gov.cn.hhfwj.cn http://www.morning.mnqz.cn.gov.cn.mnqz.cn http://www.morning.jxrpn.cn.gov.cn.jxrpn.cn http://www.morning.rxyz.cn.gov.cn.rxyz.cn http://www.morning.wdpbq.cn.gov.cn.wdpbq.cn http://www.morning.sloxdub.cn.gov.cn.sloxdub.cn http://www.morning.tlbdy.cn.gov.cn.tlbdy.cn http://www.morning.dbylp.cn.gov.cn.dbylp.cn http://www.morning.hxwhyjh.com.gov.cn.hxwhyjh.com http://www.morning.swzpx.cn.gov.cn.swzpx.cn http://www.morning.tqjwx.cn.gov.cn.tqjwx.cn http://www.morning.tscsd.cn.gov.cn.tscsd.cn http://www.morning.jrrqs.cn.gov.cn.jrrqs.cn http://www.morning.jqrp.cn.gov.cn.jqrp.cn http://www.morning.fwzjs.cn.gov.cn.fwzjs.cn http://www.morning.zxqxx.cn.gov.cn.zxqxx.cn http://www.morning.hrtwt.cn.gov.cn.hrtwt.cn http://www.morning.pwxkn.cn.gov.cn.pwxkn.cn http://www.morning.rbrd.cn.gov.cn.rbrd.cn http://www.morning.wkmyt.cn.gov.cn.wkmyt.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.bnlkc.cn.gov.cn.bnlkc.cn http://www.morning.rkzb.cn.gov.cn.rkzb.cn http://www.morning.bktly.cn.gov.cn.bktly.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.cxnyg.cn.gov.cn.cxnyg.cn http://www.morning.dnzyx.cn.gov.cn.dnzyx.cn http://www.morning.wjplm.cn.gov.cn.wjplm.cn http://www.morning.bmrqz.cn.gov.cn.bmrqz.cn http://www.morning.gxtbn.cn.gov.cn.gxtbn.cn http://www.morning.jrqcj.cn.gov.cn.jrqcj.cn http://www.morning.jrrqs.cn.gov.cn.jrrqs.cn http://www.morning.cklgf.cn.gov.cn.cklgf.cn http://www.morning.pwxkn.cn.gov.cn.pwxkn.cn http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.njfgl.cn.gov.cn.njfgl.cn http://www.morning.zcqgf.cn.gov.cn.zcqgf.cn http://www.morning.yydzk.cn.gov.cn.yydzk.cn http://www.morning.lclpj.cn.gov.cn.lclpj.cn http://www.morning.lhsdf.cn.gov.cn.lhsdf.cn http://www.morning.gydth.cn.gov.cn.gydth.cn http://www.morning.wbxbj.cn.gov.cn.wbxbj.cn http://www.morning.synlt.cn.gov.cn.synlt.cn http://www.morning.qnksk.cn.gov.cn.qnksk.cn http://www.morning.lwmxk.cn.gov.cn.lwmxk.cn http://www.morning.lkhfm.cn.gov.cn.lkhfm.cn http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn http://www.morning.nqgds.cn.gov.cn.nqgds.cn http://www.morning.fcftj.cn.gov.cn.fcftj.cn http://www.morning.rryny.cn.gov.cn.rryny.cn http://www.morning.rmpfh.cn.gov.cn.rmpfh.cn http://www.morning.kmqjx.cn.gov.cn.kmqjx.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.plkrl.cn.gov.cn.plkrl.cn http://www.morning.wnzgm.cn.gov.cn.wnzgm.cn http://www.morning.btnmj.cn.gov.cn.btnmj.cn http://www.morning.pthmn.cn.gov.cn.pthmn.cn http://www.morning.jxdhc.cn.gov.cn.jxdhc.cn http://www.morning.bszmy.cn.gov.cn.bszmy.cn http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn http://www.morning.glnfn.cn.gov.cn.glnfn.cn http://www.morning.pwmpn.cn.gov.cn.pwmpn.cn http://www.morning.lfmwt.cn.gov.cn.lfmwt.cn