实例讲解Python中函数的调用与定义


Posted in Python onMarch 14, 2016

调用函数:

#!/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 相关文章推荐
使用go和python递归删除.ds store文件的方法
Jan 22 Python
Python中dictionary items()系列函数的用法实例
Aug 21 Python
详细介绍Python中的偏函数
Apr 27 Python
利用Python实现Windows下的鼠标键盘模拟的实例代码
Jul 13 Python
Python有序字典简单实现方法示例
Sep 28 Python
python中print()函数的“,”与java中System.out.print()函数中的“+”功能详解
Nov 24 Python
Python实现的字典值比较功能示例
Jan 08 Python
python+tkinter编写电脑桌面放大镜程序实例代码
Jan 16 Python
Python+tkinter模拟“记住我”自动登录实例代码
Jan 16 Python
tensorflow 加载部分变量的实例讲解
Jul 27 Python
10招!看骨灰级Pythoner玩转Python的方法
Apr 15 Python
python使用bs4爬取boss直聘静态页面
Oct 10 Python
Python使用multiprocessing实现一个最简单的分布式作业调度系统
Mar 14 #Python
简单讲解Python中的字符串与字符串的输入输出
Mar 13 #Python
深入解析Python中的list列表及其切片和迭代操作
Mar 13 #Python
Python中的列表生成式与生成器学习教程
Mar 13 #Python
jupyter安装小结
Mar 13 #Python
Ubuntu下安装PyV8
Mar 13 #Python
Python连接MySQL并使用fetchall()方法过滤特殊字符
Mar 13 #Python
You might like
全文搜索和替换
2006/10/09 PHP
PHP base64+gzinflate压缩编码和解码代码
2008/10/03 PHP
Smarty模板学习笔记之Smarty简介
2014/05/20 PHP
php抓取并保存网站图片的实现代码
2015/10/28 PHP
删除PHP数组中头部、尾部、任意元素的实现代码
2017/04/10 PHP
PHP实现用户登录的案例代码
2018/05/10 PHP
Yii框架学习笔记之session与cookie简单操作示例
2019/04/30 PHP
IE中直接运行显示当前网页中的图片 推荐
2006/08/31 Javascript
js调用AJAX时Get和post的乱码解决方法
2013/06/04 Javascript
详细分析JavaScript变量类型
2015/07/08 Javascript
jquery简单插件制作(fn.extend)完整实例
2016/05/24 Javascript
详解小程序毫秒级倒计时(适用于拼团秒杀功能)
2019/05/05 Javascript
Vue使用mixin分发组件的可复用功能
2019/09/01 Javascript
JS回调函数简单易懂的入门实例分析
2019/09/29 Javascript
[03:16]DOTA2完美大师赛主赛事首日集锦
2017/11/23 DOTA
python中文乱码的解决方法
2013/11/04 Python
python使用wxpython开发简单记事本的方法
2015/05/20 Python
Python装饰器原理与简单用法实例分析
2018/04/29 Python
Python面向对象之反射/自省机制实例分析
2018/08/24 Python
python处理document文档保留原样式
2019/09/23 Python
pytorch 常用线性函数详解
2020/01/15 Python
Python requests模块基础使用方法实例及高级应用(自动登陆,抓取网页源码)实例详解
2020/02/14 Python
keras得到每层的系数方式
2020/06/15 Python
Python打印不合法的文件名
2020/07/31 Python
N:Philanthropy官网:美国洛杉矶基础款服装
2020/06/09 全球购物
启动一个线程是用run()还是start()
2016/12/25 面试题
材料采购员岗位职责
2013/12/17 职场文书
给全校老师的建议书
2014/03/13 职场文书
小学老师寄语大全
2014/04/04 职场文书
学校课外活动总结
2014/05/08 职场文书
齐云山导游词
2015/02/06 职场文书
语文教师求职信范文
2015/03/20 职场文书
事业单位岗位说明书
2015/10/08 职场文书
财务人员廉洁自律心得体会
2016/01/13 职场文书
2016大学生暑期社会实践心得体会
2016/01/14 职场文书
大学生村官工作心得体会
2016/01/23 职场文书