加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

内置函数

发布时间:2020-12-14 04:19:11 所属栏目:大数据 来源:网络整理
导读:和数字相关的 14个 数据类型 bool int float complex 进制转换 bin oct hex 数学运算 abs divmod round pow sum max min # print(bin(10)) # print(oct(10)) # print(hex(10)) # print(abs(-10)) # print(divmod(10,3)) # print(round(3.14159)) # print(rou

 和数字相关的 14个
   数据类型 bool int float complex
   进制转换 bin oct hex
   数学运算 abs divmod round pow sum max min 
# print(bin(10))
# print(oct(10))
# print(hex(10))
# print(abs(-10))
# print(divmod(10,3))
# print(round(3.14159))
# print(round(3.14159,3))
# print(round(2.5))
# print(round(3.5))
# print(pow(2,3))
# print(pow(2,3,3))
# print(sum([1,2,4]))
# print(min(10,12,3))
# print(min([-1,-2,-5],key=abs))
# print(max(10,3))
# print(max([-1,key=abs))
作用域相关 locals globals 2个
num=20
# def func():
#     name=‘alex‘
#     print(globals())
#     print(locals())
# 
# func()
# print(globals())
# print(locals())
其他 12个
 字符串类型代码的执行 eval exec compile
 输入输出 input print
 内存相关 hash id
 文件操作相关 open
 模块相关 __import__
 帮助 help 
 调用相关 callable 
 查看内置属性 dir
# eval()#简单计算 将字符串类型的代码执行并返回结果
# exec()#简单流程控制 将字符串类型的代码执行

# eval(‘print(123)‘)
# exec(‘print(123)‘)

# print(eval(‘1+2+3+4‘))
# print(exec(‘1+2+3+4‘))#None


# code=‘for i in range(10):print(i*"*")‘
# exec(code)
# eval(code)#报错

#compile 将字符串类型的代码编译。代码对象能够通过exec语句来执行或者eval()进行求值
#source 字符串或动态执行的代码块
#filename
#model:流程exec 求值eval 交互single

# code=‘for i in range(10):print(i*"*")‘
# c1=compile(code,‘‘,‘exec‘)
# exec(c1)
# print(type(c1))#code

# code=‘1+2+3+4‘
# c1=compile(code,‘eval‘)
# print(eval(c1))

# code=‘name=input("name:")‘
# c=compile(code,‘single‘)
# exec(c)
# print(name)

#help()
#help([])

#callable()#是否可调用
# a=1
# print(callable(a))#False
# print(callable(print))
# print(callable(globals))
# def func():pass
# print(callable(func))

#__import__()
# import time
# print(time.time())
# t=__import__(‘time‘)
# print(t.time())

# with open(‘a.txt‘,‘r‘,encoding=‘utf-8‘) as f:
#     print(f.readable())
#     print(f.writable())

# print(hash(123))
# print(hash(‘abc‘))
 和数据结构相关的 24个
   序列 
     列表和元组list tuple
     字符串相关的str ord chr ascii repr format bytes bytearray memoryview 
     相关内置函数 reversed slice
   数据集合 
     字典和集合 dict set frozenset 
     相关内置函数 len sorted enumerate all any zip filter map
#reversed# 保留原列表,返回一个反向的迭代器
# li=[1,4,5]
# s=reversed(li)
# print(li)
# print(s)#list_reverseiterator object
# print(next(s))

#slice
# li=[1,5]
# print(li[0:5:2])
# i=slice(0,5,2)
# print(i.start,i.stop,i.step)
# print(li[i])

# format
#不提供format_spec时
# print(format(3.1415936))

#字符串可以提供的参数 指定对齐方式
# print(format(‘test‘,‘<20‘))
# print(format(‘test‘,‘^20‘))
# print(format(‘test‘,‘>20‘))

#整型数值可以提供的参数
# print(format(10,‘b‘))
# print(format(10,‘o‘))
# print(format(10))
# print(format(10,‘d‘))
# print(format(10,‘n‘))#和d一样
# print(format(10,‘x‘))
# print(format(10,‘X‘))
# print(format(100,‘c‘))#转换成unicode字符

#浮点数可以提供的参数
# print(format(3.1415926,‘e‘))#科学计数法
# print(format(3.1415926,‘E‘))
# print(format(3.1415926,‘0.2e‘))
# print(format(3.1415926,‘0.2E‘))
# print(format(3.1415926,‘f‘))#默认保留6位小数
# print(format(3.1415926,‘0.3f‘))
# print(format(3.14e100000,‘F‘))#INF无穷大


# print(format(10,‘c‘))



# bytes
# res=bytes(‘alex‘,encoding=‘utf-8‘)
# print(res)
# print(res[0])
# res[0]=65#报错 不支持修改

# bytearray
# res=bytearray(‘alex‘,encoding=‘utf-8‘)
# print(res)
# res[0]=65#可以修改
# print(res)

#memoryview
# ret = memoryview(bytes(‘你好‘,encoding=‘utf-8‘))
# print(len(ret))
# print(bytes(ret[:3]).decode(‘utf-8‘))
# print(bytes(ret[3:]).decode(‘utf-8‘))

#chr ord
# print(chr(97))
# print(chr(20013))
# print(ord(‘a‘))
# print(ord(‘中‘))
# print(ascii(‘中‘))#‘u4e2d‘
# print(ascii(‘a‘))#‘a‘

# print(‘a‘)
# print(repr(‘a‘))

# print(all([]))#True
# print(all([1,3]))#True
# print(all([‘‘,3]))#False

# print(any([]))#False
# print(any([1,4]))#True
# print(any([‘‘,3]))#True

# li=[1,3]
# s=[‘a‘,‘b‘,‘c‘]
# for i in zip(li,s):
#     print(i)

#filter(过滤)遍历序列中的每个元素,判断为True的留下

# def func(i):
#     return i%2==0

# res=filter(func,[1,5])
# print(res)#filter object
# print(list(res))

#过滤1-100内平方根是整数的数
# import math
# def is_sqr(x):
#     return math.sqrt(x)%1==0
# print(list(filter(is_sqr,range(1,101))))

#map 按条件改变值
# res=map(abs,-4,9,-16])
# print(res)#map object
# print(list(res))

# res=map(lambda x:x**2,3])
# print(list(res))

#sort
# li=[-20,10,-56,36,23]
# print(sorted(li))#生成一个新列表
# print(sorted(li,reverse=True))
# print(sorted(li,key=abs))
# li=[‘abc‘,‘12‘,‘apple‘]
# print(sorted(li,key=len))
迭代器生成器相关 iter next range 3个
面向对象相关 ......

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读