zoukankan      html  css  js  c++  java
  • 内置函数(2)

    22、内置函数(2):
                        1.callable():判断一个对象是不是可调用的。函数式可以调用的,name = 'nihao'不可调用。
                        2.chr()和ord(): 根据的是ascii表 例#chr(65) 的到A  ord('B') 得到66。
                             利用这个可以写一个生成随机验证码的程序:
                                  import random #使用random模块  
                                  ret = []
                                  for i in range(6):
                                       tmp = random.randrange(65,91)
                                       alp = chr(tmp) #随机生成A-Z的字母
                                       ret.append(alp)
                                  res = ''.join(ret)#将列表中的元素拼接生成验证码。
                        3.优化版随机验证码:
    import random
    ret = []
    for i in range(6):
        r = random.randrange(0,5)
        if r == 2 or r == 4:
            num = random.randrange(0,10)
            ret.append(str(num))
        else:
            tmp = random.randrange(65,91)
            alp = chr(tmp)
            ret.append(alp)
    res = ''.join(ret)
    print(res)
    4、eval()和exec()
    >>> eval('7+8') 有返回值,执行表达式,获取结果。
    15
    >>> exec('7+8') 没有返回值,执行python 代码,接受代码或字符串。
    >>>
    5、compile():把字符串编译成python 代码。
    >>> s = 'print(123)'
    >>> r = compile(s,'<string>','exec')
    >>> exec(r)
    123
    >>> eval(r)
    123
  • 相关阅读:
    前端导出excel文件
    promise和async/await的用法
    vue element 导出 分页数据的excel表格
    mac net.core 安装问题总结
    npm报MSBUILD错误的解决办法
    现大前端开发环境配置
    git 常用命令
    NodeJs (一)
    VUE 组件通信、传值
    vue-cli 第一章
  • 原文地址:https://www.cnblogs.com/cfj271636063/p/5737335.html
Copyright © 2011-2022 走看看