pythonr-内置函数

all
print (all([1,-5,3]))
print (all([0,-5,3])) 如果有0 就不为真,非0就是为真
打印结果
True
Flase all
print(any([0,0,1])) #有一个为真,他就为真
print(any([])) #为空,他就是假
打印结果
True
Flase bin
print (bin(1)) #转换成2进制
print (bin(2))
print (bin(255)) 打印结果
0b1
0b10
0b11111111 bool
print (bool(1))
print(bool(0))
print(bool([])) #空列表为flase
print(bool([1])) 打印结果
True
False
False
True bytearray #可以修改字符串
a=bytes("abcde",encoding="utf-8") #字符串不可以修改
print(a.capitalize(),a) b=bytearray("abcde",encoding="utf-8") #修改字符串
print(b[0])
b[0]=50
print(b) 打印结果
b'Abcde' b'abcde'
97
bytearray(b'2bcde') def kezi():pass
print(callable(kezi)) 可以调用函数
print(callable([])) 空为flase 打印结果
True
False print (chr(98)) #须要输入数字,返回ASCII
print(ord('b'))须要输入字符,返回数字 打印结果
b
98

dir
#查询出它可以使用的方法有哪些

a={1,2,3}
print(dir(a))

打印结果
['__and__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__',
'__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__iand__', '__init__',
'__ior__', '__isub__', '__iter__', '__ixor__', '__le__', '__len__', '__lt__', '__ne__',
'__new__', '__or__', '__rand__', '__reduce__', '__reduce_ex__', '__repr__', '__ror__',
'__rsub__', '__rxor__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__',
'__xor__', 'add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection',
'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference',
'symmetric_difference_update', 'union', 'update']

divmod
返回商和余数
print(divmod(5,3))
打印结果
(1, 2)

eval #简单的数字进行转换
exec #复杂的转换

匿名函数

def kezi(n):
print(n)
kezi(3)

打印结果
3

lambda
(lambda n:print(n))(4) 只能做三元运算,复杂了不行 lambda
kezi2=lambda n:print(n)
kezi2(5)
打印结果
4
5
kezi3=lambda n:3 if n<5 else n
print(kezi3(4))
打印结果
3

filter #过滤 与lambda 配合使用匿名函数
res=filter(lambda n:n>5 ,range(15))
for i in res:
print(i)

打印结果

rs=map(lambda n: n*3,range(10)) 与这个相同rs=[i*2 for i in rang(10)]
for i in rs:
print(i)

打印结果
0
3
6
9
12
15
18
21
24
27

import functools
rs=functools.reduce(lambda x,y: x+y,range(5)) #累加
print(rs)

打印结果
10

b=set ([1,2,33,4444,555,34,43,33])
print (b)

c=frozenset([1,2,33,4444,555,34,43,33]) #冻结
print(c)
打印结果

{1, 2, 34, 33, 555, 43, 4444}
frozenset({1, 2, 34, 33, 555, 43, 4444})

print(globals()) 返回整个程序的所有字典的key-value

hash
print (hash("abc"))    #这个值不会变,可以利用这个方法完成字典的查询,如折半算法查询,高效查询
print (hash("abc"))

打印结果
-6788241100703459257
-6788241100703459257

print(hex(200)) 返回16进制结果
打印结果
0xc8

def test():
local_var=333
print (locals())
print(globals())
test()
print(globals())
print(globals().get('local_var'))

打印结果
{'__package__': None, 'test': <function test at 0x000001E5FA070400>, '__cached__': None, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'G:/Users/Administrator/PycharmProjects/untitled/11-20/内置函数.py', '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001E5FA00B780>, '__spec__': None, '__name__': '__main__', '__doc__': None}
{'__package__': None, 'test': <function test at 0x000001E5FA070400>, '__cached__': None, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'G:/Users/Administrator/PycharmProjects/untitled/11-20/内置函数.py', '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001E5FA00B780>, '__spec__': None, '__name__': '__main__', '__doc__': None}
{'__package__': None, 'test': <function test at 0x000001E5FA070400>, '__cached__': None, '__builtins__': <module 'builtins' (built-in)>, '__file__': 'G:/Users/Administrator/PycharmProjects/untitled/11-20/内置函数.py', '__loader__': <_frozen_importlib_external.SourceFileLoader object at 0x000001E5FA00B780>, '__spec__': None, '__name__': '__main__', '__doc__': None}
None

print(oct(8))

打印结果
0o3

print(pow(3,5))

打印结果
243

print(round(33.333333)保留小数点位

打印结果
33

sorted

b={6:22,8:33,4:44,2:55} 字典是无序的
print(sorted(b)) key排序
print(sorted(b.items(), key=lambda x:x[1])) value排序
print(sorted(b.items()))
print(b)
打印结果
[2, 4, 6, 8]
[(2, 55), (4, 44), (6, 22), (8, 33)]
[(6, 22), (8, 33), (4, 44), (2, 55)]
{8: 33, 2: 55, 4: 44, 6: 22}

zip

a=[1,2,3,4,5]
b=["a","b","c","d","e"]
print(zip(a,b)) #3以上成迭代了
for i in zip(a,b):
print(i)

打印结果
<zip object at 0x00000205CE994AC8>
(1, 'a')
(2, 'b')
(3, 'c')
(4, 'd')
(5, 'e')

pythonr-内置函数的更多相关文章

  1. Entity Framework 6 Recipes 2nd Edition(11-12)译 -> 定义内置函数

    11-12. 定义内置函数 问题 想要定义一个在eSQL 和LINQ 查询里使用的内置函数. 解决方案 我们要在数据库中使用IsNull 函数,但是EF没有为eSQL 或LINQ发布这个函数. 假设我 ...

  2. Oracle内置函数:时间函数,转换函数,字符串函数,数值函数,替换函数

    dual单行单列的隐藏表,看不见 但是可以用,经常用来调内置函数.不用新建表 时间函数 sysdate 系统当前时间 add_months 作用:对日期的月份进行加减 写法:add_months(日期 ...

  3. python内置函数

    python内置函数 官方文档:点击 在这里我只列举一些常见的内置函数用法 1.abs()[求数字的绝对值] >>> abs(-13) 13 2.all() 判断所有集合元素都为真的 ...

  4. DAY5 python内置函数+验证码实例

    内置函数 用验证码作为实例 字符串和字节的转换 字符串到字节 字节到字符串

  5. python之常用内置函数

    python内置函数,可以通过python的帮助文档 Build-in Functions,在终端交互下可以通过命令查看 >>> dir("__builtins__&quo ...

  6. freemarker内置函数和用法

    原文链接:http://www.iteye.com/topic/908500 在我们应用Freemarker 过程中,经常会操作例如字符串,数字,集合等,却不清楚Freemrker 有没有类似于Jav ...

  7. set、def、lambda、内置函数、文件操作

    set : 无序,不重复,可以嵌套 .add (添加元素) .update(接收可迭代对象)---等于批量 添加 .diffrents()两个集合不同差 .sysmmetric difference( ...

  8. SQL Server 内置函数、临时对象、流程控制

    SQL Server 内置函数 日期时间函数 --返回当前系统日期时间 select getdate() as [datetime],sysdatetime() as [datetime2] getd ...

  9. Python-Day3知识点——深浅拷贝、函数基本定义、内置函数

    一.深浅拷贝 import copy #浅拷贝 n1={'k1':'wu','k2':123,'k3':['carl',852]} n2=n1 n3=copy.copy(n1) print(id(n1 ...

  10. 性能测试总结工作总结-基于WebService协议脚本 内置函数手动编写

    LoadRunner基于WebService协议脚本 WebService协议脚本有三种生成方式,一种是直接通过LoadRunner导入URL自动解析生成:一种是使用LoadRunner内置函数手动编 ...

随机推荐

  1. 阿里云OSS文件上传封装

    1.先用composer安装阿里云OSS的PHPSDK 2.配置文件里定义阿里云OSS的秘钥 3.在index控制器里的代码封装 <?php namespace app\index\contro ...

  2. @清晰掉 makefile

    参阅: http://www.cnblogs.com/wang_yb/p/3990952.html

  3. 打开远程桌面时总提示无法打开连接文件default.rdp

    删除C:\Users\Administrator\Documents\default.rdp,再启动远程就好了 http://www.chahushequ.com/read-topic-94-2fa9 ...

  4. Linux下用jar命令更新jar包文件

    jar -uvf SDK_Web_ChartReport.war  view/global/header.jsp echo '样式文件替换进包中'   查看jar包中的文件: jar -tvf SDK ...

  5. 使用foreach进行批量更新

    public void addEmps(@Param("emps")List<Employee> emps); 2映射文件配置 <!-- 批量保存 --> ...

  6. 【linux】杀掉进程命令

    1.找到对应的进程 通过端口查找 lsof -i:端口号 netstat -tunlp | grep 端口   lsof -i:9500   netstat -tunlp | grep 9500 2. ...

  7. docker windows下挂载目录和文件

    我们利用docker启动项目的时候不能直接修改容器中的内容,只能在  run  的时候挂载到本地目录或者文件来进行修改. 例子:(路径可以忽略斜杠和反斜杠,我这边使用windows的路径没有报错.do ...

  8. Django聚合数据

    背景: 有些时候,光靠数据库中已有字段的数据,还不足以满足一些特殊场景的需求,例如显示一个作者的所有书籍数量. 这时候就需要在已有数据基础上,聚合出这些没有的数据. 为查询集生产聚合: Django ...

  9. [Mac Terminal] ___切换到其他路径和目录

    如果你想将当前 command line 会话切换到其他目录,需要用到三个命令:pwd,ls和cd. pwd的含义是“print working directory”,会显示当前目录的绝对路径.ls的 ...

  10. mysql驱动表与被驱动表及join优化

    驱动表与被驱动表 先了解在join连接时哪个表是驱动表,哪个表是被驱动表:1.当使用left join时,左表是驱动表,右表是被驱动表2.当使用right join时,右表时驱动表,左表是驱动表3.当 ...