简单介绍python部分内置函数的使用方法

内置函数:Python安装后就自带的函数,python内部已定义
官方文档-------https://docs.python.org/3/library/functions.html

1.数学运算
max()得出最大值
min() 最小值
sum()求和
sum([5, 40, 2])
min(1, 3, 4)
max(1, 5)
2.类型转换
int()、float()、str()、list()、dict()、typle()、bool()等
数据类型之间的转换,不是所有类型的数据之间都能进行转换,需要注意不同类型的数据要求(或数据格式)是否符合

3.序列操作
range()
zip()函数    打包:两个长度相同的列表/元组,不成对多余的元素会去除
             每个元素都是成对的元组/列表,可转化为字典类型的数据
all()
any()
a = [1, 2, 3,4]
b = ['a', 'b', 'c', 'd']
for e in zip(a, b):
    print(e)
print(list(zip(a, b)))      # 转换列表,打印内容[(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]

name = [(1, 'a'), (2, 'b'), (3, 'c'), (4, 'd')]
print(dict(name))   # 成对的列表、元组,转换成字典{1: 'a', 2: 'b', 3: 'c', 4: 'd'}


list1 = list(zip([1, 2], [3, 4], [5, 6]))       # list1为[(1, 3, 5), (2, 4, 6)]
print(list1)

dict1 = dict(zip([1, 2, 3], [1, 2, 3]))     # dict1为{1: 1, 2: 2, 3: 3}
print(dict1)

*zip()函数

a = [
    ['id', 'title', 'steps'],
     [1, 'abc', '1,2,3'],
     [2, 'abc', '1,2,3']
]

# 变成可读性更高的列表嵌套字典格式[{},{},{}]
title = a[0]
# data1 = a[1]
# data2 = a[2]
new_list = []
for row in a[1::]:
    row_dict = dict(zip(title, row))
    new_list.append(row_dict)
print(new_list)
# [{'id': 1, 'title': 'abc', 'steps': '1,2,3'}, {'id': 2, 'title': 'abc', 'steps': '1,2,3'}]


# 列表推导式的表达方式
c = [dict(zip(title, row)) for row in a[1::]]
其他:
eval() 把字符串左右两边的引号去除 如字符串内是运算,执行字符串表达式返回运算的结果-----拆字符串的包
enumerate()枚举
add() 
clear()
pop等 
a = "1+4"
print(a)
b = eval('1 + 4')        # 去除引号,运算1+4 ,结果赋值变量b
print(b)

a = ['a', 'b', 'c', 'd']
for e in enumerate(a):
    print(e)
# 逐次打印出内容(0, 'a')(1, 'b')(2, 'c')(3, 'd')

for idx, value in enumerate(a):    # 使用两个变量分别接收(元组)
    print(idx, value)

你可能感兴趣的:(python,开发语言)