神剑山庄资源网 Design By www.hcban.com
调用函数:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- # 函数调用 > abs(100) 100 > abs(-110) 110 > abs(12.34) 12.34 > abs(1, 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: abs() takes exactly one argument (2 given) > abs('a') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: bad operand type for abs(): 'str' > max(1, 2) 2 > max(2, 3, 1, -5) 3 > int('123') 123 > int(12.34) 12 > str(1.23) '1.23' > str(100) '100' > bool(1) True > bool('') False > a = abs # 变量a指向abs函数,相当于引用 > a(-1) # 所以也可以通过a调用abs函数 1 > n1 = 255 > n2 = 1000 > print(hex(n1)) 0xff > print(hex(n2)) 0x3e8
定义函数:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- #函数定义 def myAbs(x): if x >= 0: return x else: return -x a = 10 myAbs(a) def nop(): # 空函数 pass
pass语句什么都不做 。
实际上pass可以用来作为占位符,比如现在还没想好怎么写函数代码,就可以先写一个pass,让代码运行起来。
if age >= 18: pass #缺少了pass,代码就会有语法错误 > if age >= 18: ... File "<stdin>", line 2 ^ IndentationError: expected an indented block > myAbs(1, 2) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: myAbs() takes 1 positional argument but 2 were given > myAbs('A') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 2, in myAbs TypeError: unorderable types: str() >= int() > abs('A') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: bad operand type for abs(): 'str' def myAbs(x): if not isinstance(x, (int, float)): raise TypeError('bad operand type') if x >= 0: return x else: return -x > myAbs('A') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in myAbs TypeError: bad operand type
返回两个值?
import math def move(x, y, step, angle = 0): nx = x + step * math.cos(angle) ny = y - step * math.sin(angle) return nx, ny > x, y = move(100, 100, 60, math.pi / 6) > print(x, y) 151.96152422706632 70.0
其实上面只是一种假象,Python函数返回的仍然是单一值 。
> r = move(100, 100, 60, math.pi / 6) > print(r) (151.96152422706632, 70.0)
实际上返回的是一个tuple!
但是,语法上,返回一个tuple可以省略括号, 而多个变量可以同时接受一个tuple,按位置赋给对应的值。
所以,Python的函数返回多值实际就是返回一个tuple,但是写起来更方便。
函数执行完毕也没有return语句时,自动return None。
练习 :
import math def quadratic(a, b, c): x1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a) x2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a) return x1, x2 x1, x2 = quadratic(2, 5, 1) print(x1, x2) > import math > def quadratic(a, b, c): ... x1 = (-b + math.sqrt(b * b - 4 * a * c)) / (2 * a) ... x2 = (-b - math.sqrt(b * b - 4 * a * c)) / (2 * a) ... return x1, x2 ... > x1, x2 = quadratic(2, 5, 1) > print(x1, x2) -0.21922359359558485 -2.2807764064044154
标签:
Python,函数,调用,定义
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无实例讲解Python中函数的调用与定义的评论...
P70系列延期,华为新旗舰将在下月发布
3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。
而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?
根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。
更新日志
2024年10月03日
2024年10月03日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】