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

 更新时间:2016年03月14日 16:31:17   作者:YoferZhang  
这篇文章主要介绍了Python中函数的调用与定义,是Python入门学习中的基础知识,需要的朋友可以参考下
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

调用函数:

#!/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创建文件夹与文件的快捷方法

    Python创建文件夹与文件的快捷方法

    这篇文章主要给大家介绍了关于Python创建文件夹与文件的快捷方法以及批量创建文件夹的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-12-12
  • python?Prophet时间序列预测工具库使用功能探索

    python?Prophet时间序列预测工具库使用功能探索

    Python?Prophet是一个强大的时间序列预测工具,由Facebook开发,具有易用性和高度可定制性的特点,本文将深入介绍Python?Prophet的基本概念、安装方法以及如何使用它进行时间序列预测,并提供丰富的示例代码来帮助大家入门
    2024-01-01
  • Python爬取数据并写入MySQL数据库的实例

    Python爬取数据并写入MySQL数据库的实例

    今天小编就为大家分享一篇Python爬取数据并写入MySQL数据库的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-06-06
  • 浅析Python的web.py框架中url的设定方法

    浅析Python的web.py框架中url的设定方法

    web.py是Python的一个轻量级Web开发框架,这里我们来浅析Python的web.py框架中url的设定方法,需要的朋友可以参考下
    2016-07-07
  • pycharm中成功运行图片的配置教程

    pycharm中成功运行图片的配置教程

    今天小编就为大家分享一篇pycharm中成功运行图片的配置教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-10-10
  • keras 模型参数,模型保存,中间结果输出操作

    keras 模型参数,模型保存,中间结果输出操作

    这篇文章主要介绍了keras 模型参数,模型保存,中间结果输出操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-07-07
  • 详解利用Pandas求解两个DataFrame的差集,交集,并集

    详解利用Pandas求解两个DataFrame的差集,交集,并集

    这篇文章主要和大家讲解一下如何利用Pandas函数求解两个DataFrame的差集、交集、并集,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-07-07
  • python通过文件头判断文件类型

    python通过文件头判断文件类型

    这篇文章主要介绍了python通过文件头判断文件类型,需要的朋友可以参考下
    2015-10-10
  • python super()函数的基本使用

    python super()函数的基本使用

    这篇文章主要介绍了python super()函数的基本使用,帮助大家更好的理解和使用python,感兴趣的朋友可以了解下
    2020-09-09
  • Python弹球小游戏的项目代码

    Python弹球小游戏的项目代码

    本文主要介绍了Python弹球小游戏的项目代码,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03

最新评论

?


http://www.vxiaotou.com