码迷,mamicode.com
首页 > 编程语言 > 详细

python内置方法

时间:2019-05-01 22:31:28      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:信息   bool   切片   process   python   not   make   打印   list   

1.abs取绝对值
>>> abs(9.8)
9.8
>>> abs(-9.8)
9.8

2.dic()变为字典类型

>>> dict({"key":"value"})
{‘key‘: ‘value‘}

3.help()显示帮助信息

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

class map(object)
 |  map(func, *iterables) --> map object
 |
 |  Make an iterator that computes the function using arguments from
-- More  --

4.min取数据中的最小值,max取数据中的最大值

print(min([3, 4, 2]))
print(min("wqeqwe"))
print(min((3, 6, 4)))
print(max([3, 4, 2]))
print(max("wqeqwe"))
print(max((3, 6, 4)))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
2
e
3
4
w
6

Process finished with exit code 0

5.all()所有的为true,才返回true。空的列表返回true

print(all([1, 2, 0]))  # 列表中的0是False,所以返回False
print(all([1, 2, 5]))  # 列表中的所有值都是True,所以返回True
print(all([]))  # 空的列表,all()返回true
print(help(all))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
False
True
True
Help on built-in function all in module builtins:

all(iterable, /)
    Return True if bool(x) is True for all values x in the iterable.

    If the iterable is empty, return True.

None

Process finished with exit code 0

6.any()任意一个为true就返回true。空的列表返回false

any()列表中的任意一个为True,就返回True

print(any([1, 2, 0]))  
print(any([1, 2, 5]))  # 列表中的任意一个是True,就返回True
print(any([]))  # 空的列表,any()返回false
print(help(any))
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
True
True
False
Help on built-in function any in module builtins:

any(iterable, /)
    Return True if bool(x) is True for any x in the iterable.

    If the iterable is empty, return False.

None

Process finished with exit code 0

7.dir()打印当前程序的所有变量

print(dir())  # 打印当前程序的所有变量
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
[‘__annotations__‘, ‘__builtins__‘, ‘__cached__‘, ‘__doc__‘, ‘__file__‘, ‘__loader__‘, ‘__name__‘, ‘__package__‘, ‘__spec__‘]

Process finished with exit code 0

8.hex()把十进制数转换为16进制数

hex()转换为16进制
print(hex(16))  
E:\PythonProject\python-test\venvP3\Scripts\python.exe E:/PythonProject/python-test/BasicGrammer/test.py
0x10

Process finished with exit code 0

9.slice()切片

>>> l = [2,3,4,5,6,7]
>>> s = slice(1,5,2)
>>> l(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: ‘list‘ object is not callable
>>> l[s]
[3, 5]

10.divmod()求商和余数

>>> divmod(10,3)
(3, 1)
>>> divmod(10,2)
(5, 0)
>>>

1.abs

python内置方法

标签:信息   bool   切片   process   python   not   make   打印   list   

原文地址:https://blog.51cto.com/10983441/2388232

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!