摘要:本文主要向大家介绍了Python语言之流程控制,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
本文主要向大家介绍了Python语言之流程控制,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
如果需要修改迭代中的数据,建议先赋值一个副本(e.g:序列,切片复制的副本作为迭代)
?123456>>> words=['dbibiubiubi']>>> for w in words[:]:... words.append('haha')... print(words)...['dbibiubiubi', 'haha']
需要修改一系列数字,使用range函数很方便,该函数可以产生一个等差序列,正、负都可以
?12345678910111213>>> for i in range(0,8,3):... print(i)...036>>> for i in range(0,-9,-3):... print(i)...0-3-6>>>
pass语句什么都不做,当语法上需要语句时可以使用,但它不做任何操作
?1234>>> def myFunc(a):... pass...>>>
函数名可以被解释器识别为用户定义/内置函数的类型
函数没有任何return,会返回一个None
?123456>>> def myFunc():... pass...>>> print(myFunc())None>>>
in关键字,测试序列是否包含某个特定的值
?12345>>> 7 in [4.5,7]True>>> '5' in '34345'True>>>
函数的参数默认值时在‘函数定义范围内’上进行评估的
?12345678>>> test=5>>> def myFunc(a=test):... print(a)...>>> test=7>>> myFunc()5>>>
当函数的参数默认值是可变对象例如列表,在不同时候调用函数,会出现共享参数值情况,此时,建议把缺省值默认为None
?1234567891011>>> def myFunc(value=[]):... value.append(6)... print(value)...>>> myFunc()[6]>>> myFunc()[6, 6]>>> myFunc()[6, 6, 6]>>>
?12345678910111213>>> def myFunc(value=None):... if value==None:... value=[]... value.append(6)... print(value)...>>> myFunc()[6]>>> myFunc()[6]>>> myFunc()[6]>>>
在可变参数*args之后出现的参数都是关键字参数
?12345678910>>> def myFunc(*args,test=1):... print('args:',args)... print('test:',test)...>>> myFunc(3,4,54,'enen')args: (3, 4, 54, 'enen')test: 1>>> myFunc(3,4,54,test='enen')args: (3, 4, 54)test: enen
列表参数可以使用*,字典参数可以使用**
?12345678910>>> def myFunc(*args,**dicts):... print('args:',args)... print('dicts',dicts)...>>> a=[3,4,5,56]>>> b={'A':'b','B':'b'}>>> myFunc(*a,**b)args: (3, 4, 5, 56)dicts {'A': 'b', 'B': 'b'}>>>
Lambda表达式可以作为函数的语法糖(e.g:作为函数的嵌套函数返回值、作为参数)
?12345678>>> def make_incrementor(n):... return lambda x: x + n...>>> f = make_incrementor(42)>>> f(0)42>>> f(1)43
?1234>>> pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]>>> pairs.sort(key=lambda pair: pair[1])>>> pairs[(4, 'four'), (1, 'one'), (3, 'three'), (2, 'two')]
函数注释查看(e.g:查看文档注释,查看参数期待类型)
?1234567891011>>> def myFunc():... """hahahhahah... hehehehehehe... """... pass...>>> print(myFunc.__doc__)hahahhahah hehehehehehe >>>
?123456>>> def f(ham: str, eggs: str = 'eggs',apple:int=9):... pass...>>> print(f.__annotations__){'ham':
本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号