zoukankan      html  css  js  c++  java
  • 🐍Python内置函数

    内置函数表

     

    abs() 绝对值

    1 print(abs(-5)) #都变成正数

    all() 判断

    1 #一个为假,就为假  # 0  空 None   Flase
    2 print(all([1,'a',0]))#False
    3 # 列表中所有元素的布尔值为真,最终结果才为真
    4 print(all('')) 
    5 # 传给all的可迭代对象如果为空,最终结果为真

    any() 判断

    1 #但凡有一个为真,就为真
    2 # print(any([0,'',None,1]))
    3 print(any([])) 
    4 # 传给any的可迭代对象如果为空,最终结果为假

    进制转换

    1 print(bin(11))     #10-2
    2 print(oct(11))     #10-8
    3 print(hex(11))     #10-16

    bool() 布尔值

    1 print(bool(0))#0, None,空的布尔值为假

    .encode() 转类型

    1 res = '你好songhaiixng'.encode('utf-8')
    2 print(res) #转成了bytes类型
    3 
    4 res = bytes('你好songhaixing',encoding='utf-8')
    5 print(res) #同上面一样

    callable() 调用

    1 def func():
    2     pass
    3 print(callable(func))
    4 print(callable('ss'.strip))#判断
    5 print(callable('dac'.split))#带括号的都可以调用

    ASCLL表转化

    1 print(chr(90))#按照表十进制转化成字符
    2 print(ord('x'))#。。。。。。转化成数字

    dir() 查看调用对象

    1 print(dir('song'))#查看某个对象可以调用的方式

    divmod() 除 余

    1 print(divmod(10,3))#(3,1)   10除于3余1
    2 print(divmod(10,2))#(5,0)

    eval() 字符串内表达式 运算

    1 #将字符内的表达式拿出来运行一下,并拿到改表达式的计算结果
    2 res = eval('2**3')              #(去掉字符串)
    3 print(res)
    4 res = eval('[1,2,3,4,]')
    5 print(res)
    1 with open('db.txt','r',encoding='utf-8')as f:
    2     f = f.read()     #{'name':'song','pwd':'123'}
    3     res = eval(f)  #把这个字典拿出来了
    4     print(res,type(res))
    5     print(res['pwd'])

    frozenset() 不可变集合 冻结集合

    1 fset = frozenset({1,2,3})
    2 
    3 #可变
    4 s = {1,2,3,4}
    5 s.add(5)
    6 print(s)  #{1, 2, 3, 4, 5} 可变

    globals() 全局作用域中的绑定关系

    1 x = 1111111111111
    2 print(globals())#查看全局作用域中名字于值得绑定关系
    3 print(locals())

    字典的key必须是不可变类型

    1 dic = {[1,2,3]:'a'}

    不可hash的类型list,dict,set, == 可变的类型

    可hash得类型int,float,str,tuple, == 不可变的类型

    1 hash([1,2,3,4,5,6,])#不可hash

    help() 显示帮助信息

    1 def func():
    2     """
    3     帮助信息
    4     :return:
    5     """
    6     pass
    7 print(help(func)) #显示里面的帮助信息
    8 print(help(max))

    len() 长度

    1 len({'x':1,'y':2})#调用 __len__
    2 print({'x':1,'y':2}.__len__)

    iter() 迭代器

    1 res = iter('song')#'song'.__iter__
    2 print(next(res))

    pwo() 平方取余

    1  print(pow(2,3,3))#2的3次方取余

    slice() 切片

    1 res = slice(1, 5, 2)#切片
    2 
    3 l = [1, 2, 3, 4, 5, 6, 7, 8]
    4 print(l[res])     #[2, 4]
    5 
    6 t = (5, 14, 7, 8, 9, 5, 4, 2)
    7 print(t[res])      #(14, 8)

    sum() 求和

    1 print(sum([1,2,3,4,5]))    #15

    vars()

    zip() (拉链函数) 左右对应

    1 left = 'hello'
    2 # right = (1,2,3,)    #[('h', 1), ('e', 2), ('l', 3)]
    3 right = {'x':1,'e':2}    #[('h', 'x'), ('e', 'e')]
    4 res = zip(left,right)
    5 print(list(res))
  • 相关阅读:
    Java 递归算法,遍历文件夹下的所有文件。
    基于appium的移动端自动化测试,密码键盘无法识别问题
    第一个脚印
    最简单ajax,$.post()用法
    关于图片title与alt
    iframe loading 效果
    iframe 跨域的高度自适应
    练习卷动式新闻广告牌
    JS学习笔记《数值与字符串相加篇》
    解决FLASH的层级问题
  • 原文地址:https://www.cnblogs.com/songhaixing/p/11880358.html
Copyright © 2011-2022 走看看