Python入门-内置对象函数

2021/8/18 12:07:27

本文主要是介绍Python入门-内置对象函数,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

1.callable()

查看函数知否可调用,可调用返回True,不可用返回False

print("input函数:", callable(input))
#input函数: True

2.eval()

print(eval("3+3"))
# 6


num = 10
s = "加法计算:{}"
var = dict(num =num, info = s)

res = eval("info.format(num *2)", var)
res   
# '加法计算:20'


l = "[1,2,3]"
t = "(1,2,3)"
d = "{1:'one',2:'two',3:'three'}"
res = eval(l)
res2 = eval(t)
res3 = eval(d)
print(res, res2, res3)
print(type(res,),type(res2), type(res3))
"""
[1, 2, 3] (1, 2, 3) {1: 'one', 2: 'two', 3: 'three'}
<class 'list'> <class 'tuple'> <class 'dict'>
"""

3.exec()

num =10
exec("num += 11")
num  
# 21


s = "for i in range(10):"\
    "print(i,end= ',')"
res = exec(s)
print()
print(res)
"""
0,1,2,3,4,5,6,7,8,9,
None
"""

4.compie()

s = "11 + 22 "
code = compile(s, " ","eval")
res = eval(code)
res
# 33

s = "data = input('请输入:')"
code = compile(s,"", "single")
exec(code)

print("输入数据为:", data)
"""
请输入: 99
输入数据为: 99
"""

s = []
data = "for i in range(2):s.append(input('请输入:'))"
code = compile(data, "", "exec")
exec(code)
exec("print('经常访问的地址:',s)")
"""
请输入: baidu
请输入: google
经常访问的地址: ['baidu', 'google']
"""

 



这篇关于Python入门-内置对象函数的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程