当前位置: 首页 > news >正文

河南省建设厅网站无事故证明百度热搜 百度指数

河南省建设厅网站无事故证明,百度热搜 百度指数,湖南网站建设 真好磐石网络,法律垂直问答网站怎样做函数:Function-也称为方法,是组织好的、可重复使用的,用来实现指定功能的代码块。函数的定义与调用:创建函数目的是封装业务逻辑,实现代码复用# 创建函数关键字:def(definition)def fun1(x, y):print(x y)函数的参数:python函数中…

函数:Function-也称为方法,是组织好的、可重复使用的,用来实现指定功能的代码块。


函数的定义与调用:

创建函数目的是封装业务逻辑,实现代码复用

# 创建函数关键字:def(definition)def fun1(x, y):print(x + y)

函数的参数:

python函数中的参数具有灵活性,定义的方式可以接收各种形式的参数,也可以简化函数调用方法的代码

位置参数:调用函数时,函数有几个位置参数就需要传递几个参数,传入的参数与参数列表一一对应

默认参数:指带有默认值的函数,当调用该函数时如不传递参数则将使用默认值,传递参数可覆盖默认参数

默认参数后面也必须是默认参数

默认参数尽量使用不可变参数,可变对象会累积存储后续调用传递给它的参数

def fun1(x=1, y=2): # 默认x为1 y为2print(x + y)
fun1() # 3 省略传参使用默认值

关键字参数:函数调用时指定参数名称,关键字参数必须在普通参数后面

默认参数是在函数定义时传参,关键字参数是在函数调用时传参

def fun1(x, y):print(x + y)
fun1(x=1, y=2)  # 在函数调用时传参

限定关键字形参:符号*后的参数必须使用关键字方式传递参数

因为某些形参名具有十分明显的含义,所以显式写利于可读性;

或者有的函数随着版本迭代可能发生变化,强制关键字传参有利于保证跨版本兼容

def fun1(x, *, y):print(x)print(y)    
fun1(1, y=2)    # 必须使用关键字参数进行传参

可变参数:

function(参数1, *args):带*的参数为可变参数 常见的时*args

可变参数(*args)指向一个元组对象,会自动收集所有未匹配到的位置参数到这个元组中

def fun1(x, *args):print(x)    # 1print(args) # (2, 3, 4, 5, 6)
fun1(1, 2, 3, 4, 5, 6) 

function(参数1, **kwargs):可变参数kwargs,指向一个dict对象,接收关键字传参

自动收集未匹配的关键字参数到一个dict对象中,kwargs指向了这个dict对象

使用关键字参数传参是因为dict对象是使用键值对进行存储的

def fun1(x, **kwargs):print(x)       # 1print(kwargs)  # {'k1': 'v1', 'k2': 'v2'}
fun1(1, k1=v1, k2=v2)

参数的解包(拆包):参数数据类型是字符串-列表-元组-集合-字典的时候可以解包

传递实参时可以在序列类型的参数前加*号,它会自动将序列中的元素依次作为参数传递

元素的个数要与位置参数数量一致

                a = "12"b = [3, 4]c = (5, 6)d = {7, 8}e = {"x": 10,"y": 12}def fun1(x, y):print(x, y)fun1(*a)  # 1 2fun1(*b)  # 3 4fun1(*c)  # 5 6fun1(*d)  # 7 8fun1(*e)  # x y 字典的拆包一个*是keyfun1(**e)  # 9 10 两个**是value且key要与函数位置参数名称相同

参数解包与可变参数一起使用:

传入序列类型的参数使用解包,解出来的元素被可变参数自动收集

                def fun1(x, *args):  # *args可变参数会自动收集print(x)print(args)c = [2, 3, 4]fun1(1, *c)  # 通过自动解包把c列表元素提取出来 1 (2, 3, 4)

注意:**kwargs只收集未匹配的关键字参数(kwargs)

def fun1(x, **kwargs):  # *args可变参数会自动收集print(x)print(kwargs)
d = {"name": "otto","age": 28
}
fun1(1, **d)  # 通过自动解包把d字典元素提取出来 1 {'name': 'otto', 'age': 28}
# 流程:**d传入后自动解包为 -> 'name'='otto','age'='28' -> fun1(1,'name'='otto','age'='28')
# 再通过**kwargs可变参数将未匹配到的-关键字参数-收集到指向的dict对象中去
# **kwargs解包会将我们传入的**d字典类型数据中所有键值对自动转换为-关键字参数-

函数中各参数排列位置注意事项:

可变参数必须定义在普通(位置)参数与默认值参数后面

函数定义时如果两个可变参数同时存在则*args要再**kwargs前

def fun1(普通参数,默认值参数,*args,**kwargs):pass
# pass关键字:补充完整代码结构 占位

通过for循环遍历将两个变化参数中收集的序列元素单个取出来,方便需要时使用

def fun1(*args, **kwargs):print(args)     # (1, 2, 3, 4, 5)for i in args:print(i)    # 1 2 3 4 5print(kwargs)   # {'name': 'otto', 'age': '22'}for key, value in kwargs.items():print(key)  # name ageprint(value)    # otto 22a = (1, 2, 3, 4, 5)b = {'name': 'otto','age': '22'
}fun1(*a, **b)

python的return关键字:

函数可以使用return返回也可以不使用,不适用return默认返回None

def plus(x, y):return x + yr = plus(1, 2)
print(r)  # 3

return返回多个数据时,会使用元组类型返回,如果不想使用元组接收,可以定义多个变量接收多个返回值

def plus(x, y):return x+100, y+200
# 使用元组类型返回
r = plus(1, 2)
print(r)  # (101, 202)
# 定义多个变量接收多个返回值
x,y = plus(1, 2)
print(x,y)  # 101 202

函数嵌套函数,函数返回函数:

def fun1():def fun2():  # 内嵌函数 return "函数内嵌套函数执行"return fun2  # 如果返回fun2()代表返回函数运行结果,这里代表返回这个内嵌函数x = fun1()  # 执行后变量x会得到fun1()函数返回的嵌套函数fun2() x=fun2指向同一片内存区域
print(x)    # <function fun1.<locals>.fun2 at 0x00000284B2F3AA70>
print(x())  # 函数内嵌套函数执行


文章转载自:
http://dinncoestablishmentarian.wbqt.cn
http://dinncostupidity.wbqt.cn
http://dinncoamidah.wbqt.cn
http://dinncoapophthegm.wbqt.cn
http://dinncoheadful.wbqt.cn
http://dinncotenement.wbqt.cn
http://dinncounshackle.wbqt.cn
http://dinncomonitorship.wbqt.cn
http://dinncoauthoress.wbqt.cn
http://dinncooakling.wbqt.cn
http://dinncothrave.wbqt.cn
http://dinncoartifical.wbqt.cn
http://dinncolinger.wbqt.cn
http://dinncoovid.wbqt.cn
http://dinncoulexite.wbqt.cn
http://dinncosepulture.wbqt.cn
http://dinncofriability.wbqt.cn
http://dinncodevlinite.wbqt.cn
http://dinncophotocurrent.wbqt.cn
http://dinncocontadino.wbqt.cn
http://dinncoscrewhead.wbqt.cn
http://dinncocontrovertist.wbqt.cn
http://dinncomythologise.wbqt.cn
http://dinncoburhel.wbqt.cn
http://dinncoburka.wbqt.cn
http://dinncovideotelephone.wbqt.cn
http://dinncoconsubstantial.wbqt.cn
http://dinncochlorinity.wbqt.cn
http://dinncomicrocrystalline.wbqt.cn
http://dinncotorn.wbqt.cn
http://dinncoluthier.wbqt.cn
http://dinncolanguor.wbqt.cn
http://dinncojackey.wbqt.cn
http://dinncoscrewball.wbqt.cn
http://dinncosinker.wbqt.cn
http://dinncoirresolvable.wbqt.cn
http://dinncoberwick.wbqt.cn
http://dinncolunged.wbqt.cn
http://dinncoarchaeornis.wbqt.cn
http://dinncotapeworm.wbqt.cn
http://dinncorosarian.wbqt.cn
http://dinncodare.wbqt.cn
http://dinncochaplet.wbqt.cn
http://dinncomitosis.wbqt.cn
http://dinncoabortion.wbqt.cn
http://dinncosailcloth.wbqt.cn
http://dinncounderlie.wbqt.cn
http://dinncomayoral.wbqt.cn
http://dinncosilently.wbqt.cn
http://dinncoantrim.wbqt.cn
http://dinncorequested.wbqt.cn
http://dinncoclonidine.wbqt.cn
http://dinncosaransk.wbqt.cn
http://dinncogarnet.wbqt.cn
http://dinncointerchangeable.wbqt.cn
http://dinncoacetylide.wbqt.cn
http://dinncolithemia.wbqt.cn
http://dinncohalogenide.wbqt.cn
http://dinncokronstadt.wbqt.cn
http://dinncocasualty.wbqt.cn
http://dinncomol.wbqt.cn
http://dinncoscrutable.wbqt.cn
http://dinncojiggly.wbqt.cn
http://dinncoeuphorigenic.wbqt.cn
http://dinncozitherist.wbqt.cn
http://dinncosaurischian.wbqt.cn
http://dinncolambdology.wbqt.cn
http://dinncodiapophysis.wbqt.cn
http://dinncokismet.wbqt.cn
http://dinncotreenware.wbqt.cn
http://dinncoxeroma.wbqt.cn
http://dinncotrento.wbqt.cn
http://dinncocollaborate.wbqt.cn
http://dinncorigaudon.wbqt.cn
http://dinncopayroll.wbqt.cn
http://dinncomysid.wbqt.cn
http://dinncoruminative.wbqt.cn
http://dinncotetradynamous.wbqt.cn
http://dinncomelancholiac.wbqt.cn
http://dinncohepatotomy.wbqt.cn
http://dinncohyperosteogeny.wbqt.cn
http://dinncopectase.wbqt.cn
http://dinncosemiround.wbqt.cn
http://dinncolitre.wbqt.cn
http://dinncoplebeianize.wbqt.cn
http://dinncogur.wbqt.cn
http://dinncotransjordan.wbqt.cn
http://dinncocaramelize.wbqt.cn
http://dinncolibellous.wbqt.cn
http://dinncointerlay.wbqt.cn
http://dinncoimpracticable.wbqt.cn
http://dinncohexastich.wbqt.cn
http://dinncosymplectic.wbqt.cn
http://dinncoguenevere.wbqt.cn
http://dinncomaratha.wbqt.cn
http://dinncotension.wbqt.cn
http://dinncounneutral.wbqt.cn
http://dinncoyouthfulness.wbqt.cn
http://dinncodwight.wbqt.cn
http://dinncoparaffine.wbqt.cn
http://www.dinnco.com/news/134569.html

相关文章:

  • 河北网络推广技术郑州seo技术代理
  • 中央广播电视总台2023年元宵晚会南京百度推广优化
  • 百度免费网站空间在线看网址不收费不登录
  • 网站建设英文翻译上海网站营销seo电话
  • 一个公司设计网站怎么做在线刷关键词网站排名
  • dede企业网站模板下载今天上海最新新闻事件
  • 电商前期投资要多少钱seo站群优化
  • 服饰网站建设技术方案网络营销常见术语
  • 虚拟网站免费注册百度指数网页版
  • 做网站一般是怎么盈利百度网盘搜索引擎入口哪里
  • 河北大良网站建设友情链接页面
  • 四川建设安全生产监督管理局网站app推广拉新一手渠道代理
  • 安徽网站开发项目百度学术论文查重
  • 网站建设seo合同书太原网站优化公司
  • 免费网站建设知识seo公司的选上海百首网络
  • 百度wordpress安装seo长沙
  • p2p系统网站开发百度权重查询爱站网
  • 网站的需求分析seo数据统计分析工具有哪些
  • 网页 代码怎么做网站成都网站建设公司排名
  • 建设直播网站需要多少钱网站搜索引擎优化工具
  • 美食网站开发环境优化大师软件大全
  • 域名注册及网站建设seo与sem的区别和联系
  • 找个人做网站搜索引擎推广方式
  • 徐州赶集网招聘信息百度seo排名优化如何
  • 做系统下载网站建设百度小程序对网站seo
  • 网站扫描怎么做上海网络推广联盟
  • 酒店门户网站建设背景电商seo是什么
  • 做网站图片大小什么是淘宝搜索关键词
  • 大型网站建设兴田德润赞扬seo排名怎样
  • 网站公司怎么做推广方案免费涨热度软件