Python函数
来源:互联网 发布:ubuntu版本查看命令 编辑:程序博客网 时间:2024/06/17 12:37
Python函数
在 Python中, 命名函数时, 在其前面加上 def .
在函数的调用, 与c++有些不同, 具体的不同, 看看代码就一目了然了
关键字的传入
>>> def Fun(name, score):... print(name);... print(score)... >>> Fun("abc", 90)abc90#关键字的传入, 这样传入时不用分顺序, 建议这样做>>> Fun(score = 90, name = "abc")abc90
*score : 收集参数, 可以传入多个参数
在传入参数的关键字前加入 * 表示可以传入多个参数, 并且可以用索引号来访问这些参数.
这个 * 被叫做解包, 当然, 跟苹果的 swift 语言的解包不一样哈
>>> def Fun1(name, *score):... print(name)... print(sum(score))... >>> Fun1("abc", 90, 20, 80, 70)abc260>>> a = [90, 20, 80, 70]>>> Fun1("abc", a)abcTraceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in Fun1TypeError: unsupported operand type(s) for +: 'int' and 'list'>>> Fun1("abc", *a)abc260
*name : 可以用索引, 当然在传入的时候一定要写出传入关键字
所以建议传入参数加上关键字
>>> def Fun2(*name, score):... print(name[1])... print(score)... >>> Fun2("abc", "bcd", "efg", 90)Traceback (most recent call last): File "<stdin>", line 1, in <module>TypeError: Fun2() missing 1 required keyword-only argument: 'score'>>> Fun2("abc", "bcd", "efg", score = 90)bcd90
阅读全文
0 0
- python函数
- python 函数
- python 函数
- Python 函数
- python 函数
- PYTHON函数
- python 函数
- Python函数
- python 函数
- Python 函数
- Python 函数
- Python 函数
- python函数
- python函数
- python 函数
- Python 函数
- Python函数
- Python函数
- 新零售核心是大数据驱动的线上线下融合
- 五、迭代器--迭代器辅助函数
- awk基本手册汇总
- 2017多校训练Contest2: 1006 Funny Function hdu6050
- memcached全面剖析–5. memcached的应用和兼容程序
- Python函数
- HDU 1150 Machine Schedule
- \r\n的意义
- 13.5 动态内存管理类
- ssh免登录
- 简述 OAuth 2.0 的运作流程
- ocr识别验证码
- poj 2387最短路 使用SPFA
- 如何设置ant的jre版本