python基础-内置函数1
来源:互联网 发布:淘宝网购物氰化钾 编辑:程序博客网 时间:2024/06/05 17:33
- localsglobals
- range
- _iter_
- dir
- dir
- open方法
- hashid
- execeval
- compile
- absdivmodpowround
- summinmax
locals、globals
def func(): a = 1 b = 2 print(locals()) print(globals())func()
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3内置函数.py{'b': 2, 'a': 1}{'__name__': '__main__', '__doc__': None, '__package__': None, '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x00A06390>, '__spec__': None, '__annotations__': {}, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'E:/python/py_pro/3内置函数.py', '__cached__': None, 'func': <function func at 0x009CD660>}
range
for i in range(1,5,2): print(i)
输出如下
E:\python\python_sdk\python.exe E:/python/py_pro/3内置函数.py13
_iter_
ite = range(5).__iter__()for i in ite: print(i)print("--------")ite = range(5).__iter__()print(ite.__next__())print("--------")ite = iter(range(5))print(next(ite))print("--------")for i in ite: print(i)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3内置函数.py01234--------0--------0--------1234Process finished with exit code 0
dir
print(dir(range(5)))#__iter__包含
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3内置函数.py['__bool__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index', 'start', 'step', 'stop']Process finished with exit code 0
dir
a = 1def func():passprint(callable(a)) #不可以调用print(callable(print)) #可以调用print(callable(func)) #可以调用
输出如下;
E:\python\python_sdk\python.exe E:/python/py_pro/3内置函数.pyFalseTrueTrueProcess finished with exit code 0
open方法
hash\id
print(id(1))print(hash('sajghfj;eyrwodnvjnz,.jifupwk')) #算法print(hash(True))print(hash((1,2,3,4)))
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3内置函数.py1625872176-1971409567189902565Process finished with exit code 0
print(1,2,3,4,5,sep='*') #sep是指定多个要打印的内容之间的分隔符
输出如下:
1*2*3*4*5
我们还可以输出到一个文本文件中
f = open('a','w')print("safly",file=f)
还可以这样输出
print(*(1,2,3))print(*{"a":1})print(*[1,3,4])
输出结果如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3内置函数.py1 2 3a1 3 4
exec\eval
print("--------输出str类型代码-------")#,而exec函数主要用于执行语句块exec("print('12345')")eval("print('12345')")print(eval('3>2'))#只不过eval函数只用于执行表达式求值#eval( obj[, globals=globals(), locals=locals()] )#。globals和locals是可选的,分别代表了全局和局部名称空间中的对象,# 其中globals必须是字典,而locals是任意的映射对象。print("-----数字运算-----------")print(exec('1+2+3-4'))print(eval('1+2+3-4'))print("----------------")a = eval("2")print(a)a = eval("(1,2,3)")print(a)a = eval("[1,2,3]")print(a)a = eval("{'a':2}")print(a)#组合方式1a = eval("1,2,3")print(a)#组合方式2a = eval("1,2,3,(1,3),{'a':1}")print(a)x = 12a = eval("3 * x")print(a)#错误# a = eval("2s")# print(a)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3内置函数.py--------输出str类型代码-------1234512345True-----数字运算-----------None2----------------2(1, 2, 3)[1, 2, 3]{'a': 2}(1, 2, 3)(1, 2, 3, (1, 3), {'a': 1})36Process finished with exit code 0
compile
code1 = 'for i in range(0,5): print (i)'compile1 = compile(code1,'','exec')exec(compile1)#简单求值表达式用evalcode2 = '1 + 2 + 3 + 4'compile2 = compile(code2,'','eval')print(eval(compile2))#’single’: 配合单一语句的exec使用code3 = 'name = input("please input your name:")'compile3 = compile(code3,'','single')# name #执行前name变量不存在exec(compile3)print(name)
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3内置函数.py0123410please input your name:saflysaflyProcess finished with exit code 0
abs\divmod\pow\round
print(abs(5))ret = divmod(10,2) #商余print(ret)ret = divmod(3,2)print(ret)print(round(3.14159,2))print(pow(2,3.5)) #幂运算print(pow(3,2))#这个是表示x的y次幂后除以z的余数。print(pow(2,3,2))print(pow(2,3,3)) #x**y%z
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3内置函数.py5(5, 0)(1, 1)3.1411.313708498984761902Process finished with exit code 0
sum、min、max
print(sum([1,2,3,4,5,6],-2))print(sum(range(101)))#sum接收一个可迭代对象#minprint(min([1,4,0,9,6]))#default命名参数用来指定最小值不存在时返回的默认值print(min([1,4,1],default=0))print(min([-9,1,23,5,-2],key=abs))#匿名函数print(min({'z':1,'a':2}))print("--------")t = (-25,1,3,6,8)print(max(t))print(max(t,key = abs))print(max((),default=100))
输出如下:
E:\python\python_sdk\python.exe E:/python/py_pro/3内置函数.py195050011a--------8-25100Process finished with exit code 0
阅读全文
0 0
- python基础-内置函数1
- Python基础内置函数
- python基础-----内置函数
- Python-基础-内置函数小结
- python基础-内置函数2
- python基础31[python内置函数列表]
- python 内置函数 (1)
- Python基础8--内置高阶函数
- python基础-类内置函数、类内置方法
- [Python入门及进阶笔记]Python-基础-内置函数小结
- Python基础 7 ---- Python内置sort和sorted函数
- [Python入门及进阶笔记]Python-基础-内置函数小结
- python函数: 内置函数
- Python基础3(内置标准函数的使用一)
- Python零基础入门之七变量与内置函数
- Python基础(一、变量和 常用内置函数)
- python基础-08-内置函数、作用域、闭包、递归
- python内置函数汇总(1)
- ActiveMq--00--Windows和Linux平台安装使用ActiveMQ
- CSS Selector定位元素
- org.apache.xmlbeans.SchemaTypeLoaderException: XML-BEANS compiled schema: Incompatible minor version
- windows下配置apache+php环境
- [Layer] layui的弹出层用法记录
- python基础-内置函数1
- win7 windows server 2008R2下 https SSL证书安装的搭配(搭配https ssl本地测试环境)
- Spring Cloud Config服务端配置细节(一)
- 递归求阶乘
- ubuntu定时执行python脚本
- Freemarker
- 诺顿环问题的尝试
- Android 面试故事 | 13家面试记录
- Lua基础知识四