Python精简入门学习(十一)

作者: 码市冲冲冲 | 来源:发表于2021-04-12 15:51 被阅读0次

Python精简入门学习之内置函数(上)
-内置函数

在python中有很多的内置函数,当你使用了它,它便会返回与之相对应的结果。常用的函数有 max 最大值、
min 最小值、sum 求和、abs 绝对值等。

-常用函数

# 取绝对值
print(abs(-18))
# round 取近似值
print(round(3.14,1))
# pow 求幂运算
print(3**3)
print(pow(3,3))
# max 求最大值
print(max([2,1,3,8,7,6,456,789]))
print(max(12,21))
# sum 使用
print(sum(range(10),1))
# eval 执行表达式
a,b,c = 1,2,3
print('动态执行的函数={}'.format(eval('a*b+c-20')))
def TestFun():
    print('我执行了吗?')
    pass
eval('TestFun()')   # 可以调用函数的执行

# 类型转换函数
print(bin(18))  # 转换成二进制
print(hex(81))  # 转换成十六进制
# 元组转换成列表
tup = (1,2,3,4)
print(type(tup)) # 打印数据类型
li = list(tup)   # 转换数据类型
print(type(li))
li.append('强制转换成功') 
print(li)
tupList = tuple(li)
print(type(tupList))

# 字典操作 dict()
dic = dict(name = 'DF',age = 17)  #创建一个字典
print(type(dic))
print(dic)

# bytes转换
print(bytes('我爱China',encoding='utf-8'))

相关文章

网友评论

    本文标题:Python精简入门学习(十一)

    本文链接:https://www.haomeiwen.com/subject/lrsxlltx.html