Python函数的形参和实参详解
在这篇文章玩蛇网将给大家介绍Python函数的两种类型参数,一种是函数定义里的形参,一种是调用函数时传入的实参。
经常在使用一些内置函数的时候,我们需要传入参数,比如:调用math.sin时,需要传入一个整型数字作为实参,还有的函数需要多个参数,像math.pow就需要2个参数,一个是基数(base)和指数(exponent)。
在函数的内部,实参会被赋值给形参,下面的例子是一个用户自定义的函数,接收一个实参:
#coding=utf-8 def print_twice(bruce): print bruce print bruce #python 函数返回值可以用return
这个函数的作用是:在调用的时候会把实参的值,给形参bruce,并将其输出2次。
这个函数对任何可以print输出的值都可用。
>>> print_twice('Spam') Spam Spam >>> print_twice(17) 17 17 >>> pirnt_twice(math.pi) 3.14159265359 3.14159265359
内置函数的组合规则,在用户自定义函数上也同样可用,所以我们可以对print_twice使用任何表达式作为实参:
>>> print_twice('Sapm' *4) Spam Sapm Spam Sapm Spam Sapm Spam Sapm >>> import math >>> print_twice(math.cos(math.pi)) -1.0 -1.0
作为实参的表达式,会在函数调用之前先执行,所以在上面例子里面,表达式’Spam’*4和math.cos(math.pi)都只执行一次。
你也可以使用变量作为实参:
>>> michael = "Eric, the half a bee." >>> print_twice(michael) Eric, the half abee. Eric, the half abee.
作为实参传入到函数的变量名称和函数定义里形参的名字没有关系。函数里面只关心形参的值,而不去关心它在调用前叫什么名字,在print_twice函数内部,大家都叫bruce.
玩蛇网文章,转载请注明出处和文章网址:https://www.iplaypy.com/jinjie/jj150.html
相关文章 Recommend
- • 2019年3月最新消息: Python 3.4.10 现已推出
- • [上海]招Python量化系统开发工程师
- • 优集品网络科技有限公司招Python中/高级工程师
- • 爱因互动科技发展有限公司招募Python开发攻城狮
- • mozio招聘Python/Django工程师
- • Kavout金融科技公司招Python研发工程师
- • Python数组逆向输出,编程练习题实例四十
- • Python数组插入排序,编程练习题实例三十九
- • Python矩阵for循环应用,编程练习题实例三十八
- • Python操作Redis数据库方面的问题
- • 请python高手帮我看看这段python代码中函数setter的
- • Python什么方法可以快速将两个队列变成字典
您现在的位置: 玩蛇网首页 > Python进阶教程 > 正文内容
我要分享到:
PYTHON基础入门必备教程 Must Know PYTHON Tutorials
- • python 解释器
- • python idle
- • python dir函数
- • python 数据类型
- • python type函数
- • python 字符串
- • python 整型数字
- • python 列表
- • python 元组
- • python 字典
- • python 集合
- • python 变量
- • python print
- • python 函数
- • python 类定义
- • python import
- • python help
- • python open
- • python 异常处理
- • python 注释
- • python continue
- • python pass
- • python return
- • python global
- • python assert
- • python if语句
- • python break
- • python for循环
- • python while循环
- • python else/elif
- • lambda匿名函数
PYTHON进阶提高必备教程 Must Know PYTHON Modules
- • os 模块
- • sys 模块
- • re 正则表达式
- • time 日期时间
- • pickle 持久化
- • random 随机
- • csv 模块
- • logging 日志
- • socket网络通信
- • json模块
- • urlparse 解析URL
- • urllib 模块
- • urllib2 模块
- • robotparser 解析
- • Cookie 模块
- • smtplib 邮件
- • Base64 编码
- • xmlrpclib客户端
- • string 文本
- • Queue 线程安全
- • math数学计算
- • linecache缓存
- • threading多线程
- • sqlite3数据库
- • gzip压缩解压
最新内容 NEWS
- • Python列表添加 insert() 插入元素方法
- • Python列表删除 pop() 方法返回指定对象
- • Python else与elif语句语法讲解
- • Python IOError错误异常原因
- • Python列表排序 reverse、sort、sorted 操作方法
- • 使用 from import方法导入Python模块
- • Python break语句 跳出循环
- • Python return语句 函数返回值
- • Python import语句导入模块语法
- • python assert 断言详细用法格式
图文精华 RECOMMEND
-
Python列表添加 insert() 插入元素方
-
Python列表删除 pop() 方法返回指定
-
Python else与elif语句语法讲解
-
Python列表排序 reverse、sort、sorte
-
Python break语句 跳出循环
-
Python return语句 函数返回值
热点文章 HOT
- Python语言的一些基本常用语句
- Python 字典items返回列表,iteritems返回迭代
- Python函数的形参和实参详解
- Python is同一性运算符和==相等运算符区别
- Python for循环控制语句一般格式及方法
- Python 序列的概念及基本操作方法
- python assert 断言详细用法格式
- python if控制流语句 语法笔记
- Python 函数的关键字参数和位置参数
- Python条件语句的嵌套使用