2、python函数和获取帮助

本文详细介绍了Python中的函数使用,包括如何获取函数帮助、定义函数、设置默认参数,以及函数作为参数的应用。重点讲解了`help()`函数的使用,函数定义的语法,无返回值函数,以及高阶函数的概念。
摘要由CSDN通过智能技术生成

调用函数、定义自己的函数以及使用Python的内置文档

你已经见过并使用了printabs等函数。但是Python还有许多其他函数,而定义自己的函数是Python编程的一个重要部分。

在这个课程中,你将学习更多关于使用和定义函数的知识。

1.获取帮助

在前面的教程中,你见过abs函数,但是如果你忘记了它是做什么的呢?

help()函数可能是你学会的最重要的Python函数之一。如果你记得如何使用help(),你就掌握了理解大多数其他函数的关键。

下面是一个示例:

In [1]:

help(round)
Help on built-in function round in module builtins:

round(number, ndigits=None)
    Round a number to a given precision in decimal digits.
    
    The return value is an integer if ndigits is omitted or None.  Otherwise
    the return value has the same type as the number.  ndigits may be negative.

help()显示两个内容:

  1. 函数的头部 round(number, ndigits=None)。在这个例子中,告诉我们round()接受一个我们可以描述为number的参数。另外,我们可以选择地提供一个单独的参数,可以描述为ndigits
  2. 函数的简要英文描述。

**常见错误:**当你查找一个函数时,记得传递函数本身的名字,而不是调用该函数的结果。

如果我们在对round()函数的调用上调用help会发生什么呢?查看下面单元格的输出以了解答案。

In [2]:

help(round(-2.01))

output

Help on int object:

class int(object)
 |  int([x]) -> integer
 |  int(x, base=10) -> integer
 |  
 |  Convert a number or string to an integer, or return 0 if no arguments
 |  are given.  If x is a number, return x.__int__().  For floating point
 |  numbers, this truncates towards zero.
 |  
 |  If x is not a number or if base is given, then x must be a string,
 |  bytes, or bytearray instance representing an integer literal in the
 |  given base.  The literal can be preceded by '+' or '-' and be surrounded
 |  by whitespace.  The base defaults to 10.  Valid bases are 0 and 2-36.
 |  Base 0 means to interpret the base from the string as an integer literal.
 |  >>> int('0b100', base=0)
 |  4
 |  
 |  Methods defined here:
 |  
 |  __abs__(self, /)
 |      abs(self)
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
 |  __and__(self, value, /)
 |      Return self&value.
 |  
 |  __bool__(self, /)
 |      self != 0
 |  
 |  __ceil__(...)
 |      Ceiling of an Integral returns itself.
 |  
 |  __divmod__(self, value, /)
 |      Return divmod(self, value).
 |  
 |  __eq__(self, value, /)
 |      Return self==value.
 |  
 |  __float__(self, /)
 |      float(self)
 |  
 |  __floor__(...)
 |      Flooring an Integral returns itself.
 |  
 |  __floordiv__(self, value, /)
 |      Return self//value.
 |  
 |  __format__(self, format_spec, /)
 |      Default object formatter.
 |  
 |  __ge__(self, value, /)
 |      Return self>=value.
 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  __getnewargs__(self, /)
 |  
 |  __gt__(self, value, /)
 |      Return self>value.
 |  
 |  __hash__(self, /)
 |      Return hash(self).
 |  
 |  __index__(self, 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法蒋同学

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值