Python内置函数(29)——help

Python help()函数详解
本文详细介绍了Python中help()函数的使用方法。该函数可在解释器中启动内置帮助系统,无需参数即可查看所有模块、关键字及主题的帮助信息;提供参数时则会显示该参数对应模块、类或函数的帮助信息。

英文文档:

help([object])

Invoke the built-in help system. (This function is intended for interactive use.) If no argument is given, the interactive help system starts on the interpreter console. If the argument is a string, then the string is looked up as the name of a module, function, class, method, keyword, or documentation topic, and a help page is printed on the console. If the argument is any other kind of object, a help page on the object is generated.

This function is added to the built-in namespace by the site module.

 

说明:

  1. 在解释器交互界面,不传参数调用函数时,将激活内置的帮助系统,并进入帮助系统。在帮助系统内部输入模块、类、函数等名称时,将显示其使用说明,输入quit退出内置帮助系统,并返回交互界面。

>>> help() #不带参数

Welcome to Python 3.5's help utility!

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/3.5/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, symbols, or topics, type
"modules", "keywords", "symbols", or "topics".  Each module also comes
with a one-line summary of what it does; to list the modules whose name
or summary contain a given string such as "spam", type "modules spam".

#进入内置帮助系统  >>> 变成了 help>
help> str #str的帮助信息
Help on class str in module builtins:

class str(object)
 |  str(object='') -> str
 |  str(bytes_or_buffer[, encoding[, errors]]) -> str
 |  
 |  Create a new string object from the given object. If encoding or
 |  errors is specified, then the object must expose a data buffer
 |  that will be decoded using the given encoding and error handler.
 |  Otherwise, returns the result of object.__str__() (if defined)
 |  or repr(object).
 |  encoding defaults to sys.getdefaultencoding().
 |  errors defaults to 'strict'.
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 ................................


help> 1 #不存在的模块名、类名、函数名
No Python documentation found for '1'.
Use help() to get the interactive help utility.
Use help(str) for help on the str class.

help> quit #退出内置帮助系统

You are now leaving help and returning to the Python interpreter.
If you want to ask for help on a particular object directly from the
interpreter, you can type "help(object)".  Executing "help('string')"
has the same effect as typing a particular string at the help> prompt.

# 已退出内置帮助系统,返回交互界面 help> 变成 >>>
>>> 

  2. 在解释器交互界面,传入参数调用函数时,将查找参数是否是模块名、类名、函数名,如果是将显示其使用说明。

>>> help(str) 
Help on class str in module builtins:

class str(object)
 |  str(object='') -> str
 |  str(bytes_or_buffer[, encoding[, errors]]) -> str
 |  
 |  Create a new string object from the given object. If encoding or
 |  errors is specified, then the object must expose a data buffer
 |  that will be decoded using the given encoding and error handler.
 |  Otherwise, returns the result of object.__str__() (if defined)
 |  or repr(object).
 |  encoding defaults to sys.getdefaultencoding().
 |  errors defaults to 'strict'.
 |  
 |  Methods defined here:
 |  
 |  __add__(self, value, /)
 |      Return self+value.
 |  
  ***************************

 

转载于:https://www.cnblogs.com/sesshoumaru/p/6014216.html

### Python 函数结构实训答案 在Python中,函数是一种重要的代码组织方式,通过定义函数可以实现代码的复用和模块化设计。以下是一个典型的Python函数结构示例,结合头歌平台上的实训任务进行说明。 #### 1. 定义一个判断素数的函数 根据引用[^2]中的任务要求,定义一个函数用于判断输入的整数是否为素数。以下是完整的代码实现: ```python def is_prime(number): if number <= 1: return False for i in range(2, int(number ** 0.5) + 1): if number % i == 0: return False return True # 测试函数 num = int(input("请输入一个整数:")) print(is_prime(num)) ``` 上述代码中,`is_prime`函数通过遍历从2到`sqrt(number)`的所有整数来判断是否存在因子。如果存在,则返回`False`;否则返回`True`。 #### 2. 求两个正整数的最小公倍数 根据引用[^3]中的任务要求,编写程序求两个正整数的最小公倍数。以下是完整的代码实现: ```python def _gcd(a, b): while b != 0: a, b = b, a % b return a def lcm(a, b): return abs(a * b) // _gcd(a, b) # 测试函数 num1 = int(input("请输入第一个正整数:")) num2 = int(input("请输入第二个正整数:")) print(lcm(num1, num2)) ``` 上述代码中,`_gcd`函数使用了欧几里得算法求最大公约数,而`lcm`函数利用公式`lcm(a, b) = abs(a * b) // gcd(a, b)`计算最小公倍数。 #### 3. 基本输入输出 根据引用[^1]中的任务要求,完成打印`Hello world`以及整数加减法的基本操作。以下是完整的代码实现: ```python # 打印Hello world print("Hello world") # 整数加减法 num1 = int(input("请输入第一个整数:")) num2 = int(input("请输入第二个整数:")) print(f"{num1} + {num2} = {num1 + num2}") print(f"{num1} - {num2} = {num1 - num2}") # 使用Help命令查询内置函数 help(print) ``` 上述代码实现了基本的输入输出功能,并通过`help()`函数查询了`print()`函数的详细信息。 --- ####
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值