博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Python_内置函数2_44
阅读量:4540 次
发布时间:2019-06-08

本文共 2826 字,大约阅读时间需要 9 分钟。

字符串类型代码执行:

exec('print(123)')eval('print(123)')print(eval('1*2+3+4'))   # 有返回值print(exec('1+2+3+4'))   #没有返回值# exec和eval都可以执行 字符串类型的代码# eval有返回值  —— 有结果的简单计算# exec没有返回值   —— 简单流程控制# eval只能用在你明确知道你要执行的代码是什么

 

code = '''for i in range(10):    print(i*'*')'''exec(code)

 

code1 = 'for i in range(0,10): print (i)'compile1 = compile(code1,'','exec')exec(compile1)code2 = '1 + 2 + 3 + 4'compile2 = compile(code2,'','eval')print(eval(compile2))

 

code3 = 'name = input("please input your name:")'compile3 = compile(code3,'','single')exec(compile3) #执行时显示交互命令,提示输入print(name)# name #执行后name变量有值# "'pythoner'"

 

 

 

 

eval() 将字符串类型的代码执行并返回结果

print(eval('1+2+3+4'))

exec()将自字符串类型的代码执行

print(exec("1+2+3+4"))exec("print('hello,world')")
code = '''import os print(os.path.abspath('.'))'''code = '''print(123)a = 20print(a)'''a = 10exec(code,{'print':print},)print(a)

 

compile  将字符串类型的代码编译。代码对象能够通过exec语句来执行或者eval()进行求值。

参数说明:   

1. 参数source:字符串或者AST(Abstract Syntax Trees)对象。即需要动态执行的代码段。  

2. 参数 filename:代码文件名称,如果不是从文件读取代码则传递一些可辨认的值。当传入了source参数时,filename参数传入空字符即可。  

3. 参数model:指定编译代码的种类,可以指定为 ‘exec’,’eval’,’single’。当source中包含流程语句时,model应指定为‘exec’;当source中只包含一个简单的求值表达式,model应指定为‘eval’;当source中包含了交互式命令语句,model应指定为'single'。

 

>>> #流程语句使用exec>>> code1 = 'for i in range(0,10): print (i)'>>> compile1 = compile(code1,'','exec')>>> exec (compile1)>>> #简单求值表达式用eval>>> code2 = '1 + 2 + 3 + 4'>>> compile2 = compile(code2,'','eval')>>> eval(compile2)>>> #交互语句用single>>> code3 = 'name = input("please input your name:")'>>> compile3 = compile(code3,'','single')>>> name #执行前name变量不存在Traceback (most recent call last):  File "
", line 1, in
nameNameError: name 'name' is not defined>>> exec(compile3) #执行时显示交互命令,提示输入please input your name:'pythoner'>>> name #执行后name变量有值"'pythoner'"

 

# 复数 —— complex

# 实数 : 有理数 #         无理数 # 虚数 :虚无缥缈的数 # 5 + 12j  === 复合的数 === 复数 # 6 + 15j

浮点数:

# 浮点数(有限循环小数,无限循环小数)  != 小数 :有限循环小数,无限循环小数,无限不循环小数# 浮点数    #354.123 = 3.54123*10**2 = 35.4123 * 10# f = 1.781326913750135970# print(f)

 

进制转换:

print(bin(10))print(oct(10))print(hex(10))# 0b1010# 0o12# 0xa

 

print(abs(-5))print(abs(5))   #求绝对值

 

print(divmod(7,2))   # div除法 mod取余  (3, 1)print(divmod(9,5))   # 除余 (1, 4)

 

print(round(3.14159,3))  #保留小数位数print(pow(2,3))   #pow幂运算  == 2**3   8print(pow(3,2))  # 9print(pow(2,3,3)) #幂运算之后再取余  2print(pow(3,2,1))  # 0

 

sum:

sum(iterable, start)

iterable start 只能通过位置传参,不允许关键字传参

ret = sum([1,2,3,4,5,6])print(ret)

 

ret = sum([1,2,3,4,5,6,10],10)print(ret)ret = sum([1,2,3,4,5,6,10],)print(ret)

 

 

min max:

print(min([1,2,3,4])) #1print(min(1,2,3,4))   #1print(min(1,2,3,-4))  #-4print(min(1,2,3,-4,key = abs)) #1 变为绝对值后求最小值print(max([1,2,3,4])) #4print(max(1,2,3,4))   #4print(max(1,2,3,-4))  #3print(max(1,2,3,-4,key = abs))     #-4  变为绝对值后求最大值

 

 

 

 

 

转载于:https://www.cnblogs.com/LXL616/p/10686304.html

你可能感兴趣的文章
验证码
查看>>
Django缓存配置
查看>>
Ubuntu下安装 Mysql
查看>>
LeetCode--Reverse Integer
查看>>
PHP_APC+Ajax实现的监视进度条的文件上传
查看>>
计算机网络课堂笔记3.29
查看>>
word2vec----CBOW
查看>>
衰减学习率真的有用吗?
查看>>
ORACLE 建库过程总结
查看>>
Ogre1.8.1 Basic Tutorial 6 - The Ogre Startup Sequence
查看>>
构建ASP.NET MVC4+EF5+EasyUI+Unity2.x注入的后台管理系统(36)-文章发布系统③-kindeditor使用...
查看>>
c# Winform 开发分屏显示应用程序
查看>>
canvas刮奖
查看>>
添加源ubuntu_x64 安装 Adobe Reader
查看>>
给datalist加自动编号(解决博客的第XX楼)
查看>>
BZOJ3282: Tree (LCT模板)
查看>>
ES6中变量的解构赋值
查看>>
数据绑定控件Reperter
查看>>
【codeforces】【比赛题解】#937 CF Round #467 (Div. 2)
查看>>
Yii DataProvider
查看>>